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

package ec2

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

// Associates an application status check with instances or [tags]. Once you create an
// association, health monitoring automatically begins for the specified instances
// or for instances that match the specified tags. The following rules apply:
//
//   - You must specify either TargetTagAssociations or InstanceIds , but not both.
//     Specifying both results in an InvalidParameterCombination error.
//
//   - You must own the application status check. The check must already exist in
//     your account.
//
//   - You must not leave tag keys blank.
//
//   - You can create a maximum of 50 tag associations for each application status
//     check.
//
//   - You can use DisassociateApplicationStatusCheck to remove associations.
//
//   - You can associate [tags]so that the application status check automatically
//     monitors all current and future instances that have the specified tags.
//
// [tags]: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html
func (c *Client) AssociateApplicationStatusCheck(ctx context.Context, params *AssociateApplicationStatusCheckInput, optFns ...func(*Options)) (*AssociateApplicationStatusCheckOutput, error) {
	if params == nil {
		params = &AssociateApplicationStatusCheckInput{}
	}

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

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

type AssociateApplicationStatusCheckInput struct {

	// The ID of the application status check to associate.
	//
	// This member is required.
	ApplicationStatusCheckId *string

	// A unique, case-sensitive identifier that you provide to ensure that the
	// operation completes no more than one time. If you retry a request with the same
	// token, the service ignores the request but does not return an error. For more
	// information, see [Ensuring idempotency].
	//
	// [Ensuring idempotency]: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/Run_Instance_Idempotency.html
	ClientToken *string

	// Checks whether you have the required permissions for the operation, without
	// actually making the request, and provides an error response. If you have the
	// required permissions, the error response is DryRunOperation . Otherwise, it is
	// UnauthorizedOperation .
	DryRun *bool

	// The IDs of the instances to associate with the application status check.
	InstanceIds []string

	// The [tags] to associate the application status check with. Each tag is a key-value
	// pair. When you associate tags, the application status check automatically
	// monitors all instances that have the specified tags.
	//
	// [tags]: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html
	TargetTagAssociations []types.CustomTagKeyValueRequestPair

	noSmithyDocumentSerde
}

type AssociateApplicationStatusCheckOutput struct {

	// The associations that were successfully created.
	SuccessfulResults []types.SuccessfulAssociationResponseObject

	// The associations that failed to be created.
	UnsuccessfulResults []types.UnsuccessfulAssociationResponseObject

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

	noSmithyDocumentSerde
}

func (c *Client) addOperationAssociateApplicationStatusCheckMiddlewares(stack *middleware.Stack, options Options) (err error) {
	err = stack.Serialize.Add(&awsEc2query_serializeOpAssociateApplicationStatusCheck{}, middleware.After)
	if err != nil {
		return err
	}
	err = stack.Deserialize.Add(&awsEc2query_deserializeOpAssociateApplicationStatusCheck{}, middleware.After)
	if 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_opAssociateApplicationStatusCheckMiddleware(stack, options); err != nil {
		return err
	}
	if err = addOpAssociateApplicationStatusCheckValidationMiddleware(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_initializeOpAssociateApplicationStatusCheck struct {
	tokenProvider IdempotencyTokenProvider
}

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

func (m *idempotencyToken_initializeOpAssociateApplicationStatusCheck) 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.(*AssociateApplicationStatusCheckInput)
	if !ok {
		return out, metadata, fmt.Errorf("expected middleware input to be of type *AssociateApplicationStatusCheckInput ")
	}

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