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

package ec2

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

// Cancels the specified Capacity Reservation, releases the reserved capacity, and
// changes the Capacity Reservation's state to cancelled .
//
// You can cancel a Capacity Reservation that is in the following states:
//
//   - assessing
//
//   - scheduled — requires a cancellation quote. Use
//     CreateCapacityReservationCancellationQuote to generate a quote, then pass the
//     quote ID with ApplyCancellationCharges set to commitment-wind-down . The
//     cancellation charge depends on how close the reservation is to its start date.
//
//   - active and there is no commitment duration or the commitment duration has
//     elapsed.
//
//   - active during the commitment duration — requires a cancellation quote. Use
//     CreateCapacityReservationCancellationQuote to generate a quote, then pass the
//     quote ID with ApplyCancellationCharges set to commitment-wind-down . The
//     Capacity Reservation transitions to cancelling while charges are applied.
//
//   - delayed — the commitment duration is waived, so no cancellation charge
//     applies.
//
// You can't modify or cancel a Capacity Block. For more information, see [Capacity Blocks for ML].
//
// Instances running in the reserved capacity continue running until you stop
// them. Stopped instances that target the Capacity Reservation can no longer
// launch. Modify these instances to either target a different Capacity
// Reservation, launch On-Demand Instance capacity, or run in any open Capacity
// Reservation that has matching attributes and sufficient capacity.
//
// [Capacity Blocks for ML]: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-capacity-blocks.html
func (c *Client) CancelCapacityReservation(ctx context.Context, params *CancelCapacityReservationInput, optFns ...func(*Options)) (*CancelCapacityReservationOutput, error) {
	if params == nil {
		params = &CancelCapacityReservationInput{}
	}

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

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

type CancelCapacityReservationInput struct {

	// The ID of the Capacity Reservation to be cancelled.
	//
	// This member is required.
	CapacityReservationId *string

	// Specifies the cancellation charge type to apply when cancelling a future-dated
	// Capacity Reservation during its commitment duration. Possible values include
	// commitment-wind-down , which continues billing for the remaining commitment
	// duration without delivering capacity.
	ApplyCancellationCharges types.ApplyCancellationCharges

	// Checks whether you have the required permissions for the action, 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 ID of the cancellation quote to use for the cancellation. You can generate
	// a cancellation quote by using the CreateCapacityReservationCancellationQuote
	// action. The cancellation quote must be in an active state.
	QuoteId *string

	noSmithyDocumentSerde
}

type CancelCapacityReservationOutput struct {

	// Returns true if the request succeeds; otherwise, it returns an error.
	Return *bool

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

	noSmithyDocumentSerde
}

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

	if err = addlegacyEndpointContextSetter(stack, options); 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); err != nil {
		return err
	}
	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = addCredentialSource(stack, options); err != nil {
		return err
	}
	if err = addOpCancelCapacityReservationValidationMiddleware(stack); err != nil {
		return err
	}
	if err = stack.Initialize.Add(newServiceMetadataMiddleware(options.Region, "CancelCapacityReservation"), middleware.Before); 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
}
