1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package ec2
4
5import (
6	"context"
7	"fmt"
8	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
9	"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
10	"github.com/aws/aws-sdk-go-v2/service/ec2/types"
11	"github.com/aws/smithy-go/middleware"
12	smithyhttp "github.com/aws/smithy-go/transport/http"
13)
14
15// Purchases the Scheduled Instances with the specified schedule. Scheduled
16// Instances enable you to purchase Amazon EC2 compute capacity by the hour for a
17// one-year term. Before you can purchase a Scheduled Instance, you must call
18// DescribeScheduledInstanceAvailability to check for available schedules and
19// obtain a purchase token. After you purchase a Scheduled Instance, you must call
20// RunScheduledInstances during each scheduled time period. After you purchase a
21// Scheduled Instance, you can't cancel, modify, or resell your purchase.
22func (c *Client) PurchaseScheduledInstances(ctx context.Context, params *PurchaseScheduledInstancesInput, optFns ...func(*Options)) (*PurchaseScheduledInstancesOutput, error) {
23	if params == nil {
24		params = &PurchaseScheduledInstancesInput{}
25	}
26
27	result, metadata, err := c.invokeOperation(ctx, "PurchaseScheduledInstances", params, optFns, addOperationPurchaseScheduledInstancesMiddlewares)
28	if err != nil {
29		return nil, err
30	}
31
32	out := result.(*PurchaseScheduledInstancesOutput)
33	out.ResultMetadata = metadata
34	return out, nil
35}
36
37// Contains the parameters for PurchaseScheduledInstances.
38type PurchaseScheduledInstancesInput struct {
39
40	// The purchase requests.
41	//
42	// This member is required.
43	PurchaseRequests []types.PurchaseRequest
44
45	// Unique, case-sensitive identifier that ensures the idempotency of the request.
46	// For more information, see Ensuring Idempotency
47	// (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/Run_Instance_Idempotency.html).
48	ClientToken *string
49
50	// Checks whether you have the required permissions for the action, without
51	// actually making the request, and provides an error response. If you have the
52	// required permissions, the error response is DryRunOperation. Otherwise, it is
53	// UnauthorizedOperation.
54	DryRun bool
55}
56
57// Contains the output of PurchaseScheduledInstances.
58type PurchaseScheduledInstancesOutput struct {
59
60	// Information about the Scheduled Instances.
61	ScheduledInstanceSet []types.ScheduledInstance
62
63	// Metadata pertaining to the operation's result.
64	ResultMetadata middleware.Metadata
65}
66
67func addOperationPurchaseScheduledInstancesMiddlewares(stack *middleware.Stack, options Options) (err error) {
68	err = stack.Serialize.Add(&awsEc2query_serializeOpPurchaseScheduledInstances{}, middleware.After)
69	if err != nil {
70		return err
71	}
72	err = stack.Deserialize.Add(&awsEc2query_deserializeOpPurchaseScheduledInstances{}, middleware.After)
73	if err != nil {
74		return err
75	}
76	if err = addSetLoggerMiddleware(stack, options); err != nil {
77		return err
78	}
79	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
80		return err
81	}
82	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
83		return err
84	}
85	if err = addResolveEndpointMiddleware(stack, options); err != nil {
86		return err
87	}
88	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
89		return err
90	}
91	if err = addRetryMiddlewares(stack, options); err != nil {
92		return err
93	}
94	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
95		return err
96	}
97	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
98		return err
99	}
100	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
101		return err
102	}
103	if err = addClientUserAgent(stack); err != nil {
104		return err
105	}
106	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
107		return err
108	}
109	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
110		return err
111	}
112	if err = addIdempotencyToken_opPurchaseScheduledInstancesMiddleware(stack, options); err != nil {
113		return err
114	}
115	if err = addOpPurchaseScheduledInstancesValidationMiddleware(stack); err != nil {
116		return err
117	}
118	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opPurchaseScheduledInstances(options.Region), middleware.Before); err != nil {
119		return err
120	}
121	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
122		return err
123	}
124	if err = addResponseErrorMiddleware(stack); err != nil {
125		return err
126	}
127	if err = addRequestResponseLogging(stack, options); err != nil {
128		return err
129	}
130	return nil
131}
132
133type idempotencyToken_initializeOpPurchaseScheduledInstances struct {
134	tokenProvider IdempotencyTokenProvider
135}
136
137func (*idempotencyToken_initializeOpPurchaseScheduledInstances) ID() string {
138	return "OperationIdempotencyTokenAutoFill"
139}
140
141func (m *idempotencyToken_initializeOpPurchaseScheduledInstances) HandleInitialize(ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler) (
142	out middleware.InitializeOutput, metadata middleware.Metadata, err error,
143) {
144	if m.tokenProvider == nil {
145		return next.HandleInitialize(ctx, in)
146	}
147
148	input, ok := in.Parameters.(*PurchaseScheduledInstancesInput)
149	if !ok {
150		return out, metadata, fmt.Errorf("expected middleware input to be of type *PurchaseScheduledInstancesInput ")
151	}
152
153	if input.ClientToken == nil {
154		t, err := m.tokenProvider.GetIdempotencyToken()
155		if err != nil {
156			return out, metadata, err
157		}
158		input.ClientToken = &t
159	}
160	return next.HandleInitialize(ctx, in)
161}
162func addIdempotencyToken_opPurchaseScheduledInstancesMiddleware(stack *middleware.Stack, cfg Options) error {
163	return stack.Initialize.Add(&idempotencyToken_initializeOpPurchaseScheduledInstances{tokenProvider: cfg.IdempotencyTokenProvider}, middleware.Before)
164}
165
166func newServiceMetadataMiddleware_opPurchaseScheduledInstances(region string) *awsmiddleware.RegisterServiceMetadata {
167	return &awsmiddleware.RegisterServiceMetadata{
168		Region:        region,
169		ServiceID:     ServiceID,
170		SigningName:   "ec2",
171		OperationName: "PurchaseScheduledInstances",
172	}
173}
174