// Code generated by smithy-go-codegen DO NOT EDIT.

package eks

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/service/eks/types"
	"github.com/aws/smithy-go/middleware"
)

// Cancels an in-progress update to an Amazon EKS cluster on a best-effort basis.
// Cancellation is only performed if the update can be cancelled. Currently, this
// is supported for VersionRollback update types on EKS Auto Mode clusters when
// nodes are rolling back.
//
// A successful cancellation stops the node rollback. After cancellation, nodes
// converge to the current cluster version honoring configured disruption controls.
// If the control plane rollback has already begun, the cancellation request fails.
func (c *Client) CancelUpdate(ctx context.Context, params *CancelUpdateInput, optFns ...func(*Options)) (*CancelUpdateOutput, error) {
	if params == nil {
		params = &CancelUpdateInput{}
	}

	result, metadata, err := c.invokeOperation(ctx, "CancelUpdate", params, optFns, c.addOperationCancelUpdateMiddlewares)
	if err != nil {
		return nil, err
	}

	out := result.(*CancelUpdateOutput)
	out.ResultMetadata = metadata
	return out, nil
}

type CancelUpdateInput struct {

	// The name of the Amazon EKS cluster associated with the update.
	//
	// This member is required.
	Name *string

	// The ID of the update to cancel.
	//
	// This member is required.
	UpdateId *string

	// A unique, case-sensitive identifier that you provide to ensure the idempotency
	// of the request.
	ClientRequestToken *string

	noSmithyDocumentSerde
}

type CancelUpdateOutput struct {

	// The full description of the specified update.
	Update *types.Update

	// Metadata pertaining to the operation's result.
	ResultMetadata middleware.Metadata

	noSmithyDocumentSerde
}

func (c *Client) addOperationCancelUpdateMiddlewares(stack *middleware.Stack, options Options) (err error) {
	err = stack.Serialize.Add(&awsRestjson1_serializeOpCancelUpdate{}, middleware.After)
	if err != nil {
		return err
	}
	err = stack.Deserialize.Add(&awsRestjson1_deserializeOpCancelUpdate{}, middleware.After)
	if err != nil {
		return err
	}

	if err = addComputeContentLength(stack); err != nil {
		return err
	}
	if err = addResolveEndpointMiddleware(stack, options); err != nil {
		return err
	}
	if err = addComputePayloadSHA256(stack); err != nil {
		return err
	}
	if err = addRecordResponseTiming(stack, options); err != nil {
		return err
	}
	if err = addCredentialSource(stack, options); err != nil {
		return err
	}
	if err = addIdempotencyToken_opCancelUpdateMiddleware(stack, options); err != nil {
		return err
	}
	if err = addOpCancelUpdateValidationMiddleware(stack); err != nil {
		return err
	}
	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
		return err
	}
	if err = addResponseErrorMiddleware(stack); err != nil {
		return err
	}
	if err = addRequestResponseLogging(stack, options); err != nil {
		return err
	}
	if err = addDisableHTTPSMiddleware(stack, options); err != nil {
		return err
	}
	if err = addInterceptors(stack, options); err != nil {
		return err
	}
	return nil
}

type idempotencyToken_initializeOpCancelUpdate struct {
	tokenProvider IdempotencyTokenProvider
}

func (*idempotencyToken_initializeOpCancelUpdate) ID() string {
	return "OperationIdempotencyTokenAutoFill"
}

func (m *idempotencyToken_initializeOpCancelUpdate) HandleInitialize(ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler) (
	out middleware.InitializeOutput, metadata middleware.Metadata, err error,
) {
	if m.tokenProvider == nil {
		return next.HandleInitialize(ctx, in)
	}

	input, ok := in.Parameters.(*CancelUpdateInput)
	if !ok {
		return out, metadata, fmt.Errorf("expected middleware input to be of type *CancelUpdateInput ")
	}

	if input.ClientRequestToken == nil {
		t, err := m.tokenProvider.GetIdempotencyToken()
		if err != nil {
			return out, metadata, err
		}
		input.ClientRequestToken = &t
	}
	return next.HandleInitialize(ctx, in)
}
func addIdempotencyToken_opCancelUpdateMiddleware(stack *middleware.Stack, cfg Options) error {
	return stack.Initialize.Add(&idempotencyToken_initializeOpCancelUpdate{tokenProvider: cfg.IdempotencyTokenProvider}, middleware.Before)
}
