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	presignedurlcust "github.com/aws/aws-sdk-go-v2/service/internal/presigned-url"
12	"github.com/aws/smithy-go/middleware"
13	smithyhttp "github.com/aws/smithy-go/transport/http"
14)
15
16// Copies a point-in-time snapshot of an EBS volume and stores it in Amazon S3. You
17// can copy the snapshot within the same Region or from one Region to another. You
18// can use the snapshot to create EBS volumes or Amazon Machine Images (AMIs).
19// Copies of encrypted EBS snapshots remain encrypted. Copies of unencrypted
20// snapshots remain unencrypted, unless you enable encryption for the snapshot copy
21// operation. By default, encrypted snapshot copies use the default AWS Key
22// Management Service (AWS KMS) customer master key (CMK); however, you can specify
23// a different CMK. To copy an encrypted snapshot that has been shared from another
24// account, you must have permissions for the CMK used to encrypt the snapshot.
25// Snapshots created by copying another snapshot have an arbitrary volume ID that
26// should not be used for any purpose. For more information, see Copying an Amazon
27// EBS snapshot
28// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-copy-snapshot.html) in
29// the Amazon Elastic Compute Cloud User Guide.
30func (c *Client) CopySnapshot(ctx context.Context, params *CopySnapshotInput, optFns ...func(*Options)) (*CopySnapshotOutput, error) {
31	if params == nil {
32		params = &CopySnapshotInput{}
33	}
34
35	result, metadata, err := c.invokeOperation(ctx, "CopySnapshot", params, optFns, addOperationCopySnapshotMiddlewares)
36	if err != nil {
37		return nil, err
38	}
39
40	out := result.(*CopySnapshotOutput)
41	out.ResultMetadata = metadata
42	return out, nil
43}
44
45type CopySnapshotInput struct {
46
47	// The ID of the Region that contains the snapshot to be copied.
48	//
49	// This member is required.
50	SourceRegion *string
51
52	// The ID of the EBS snapshot to copy.
53	//
54	// This member is required.
55	SourceSnapshotId *string
56
57	// A description for the EBS snapshot.
58	Description *string
59
60	// Checks whether you have the required permissions for the action, without
61	// actually making the request, and provides an error response. If you have the
62	// required permissions, the error response is DryRunOperation. Otherwise, it is
63	// UnauthorizedOperation.
64	DryRun bool
65
66	// To encrypt a copy of an unencrypted snapshot if encryption by default is not
67	// enabled, enable encryption using this parameter. Otherwise, omit this parameter.
68	// Encrypted snapshots are encrypted, even if you omit this parameter and
69	// encryption by default is not enabled. You cannot set this parameter to false.
70	// For more information, see Amazon EBS encryption
71	// (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html) in the
72	// Amazon Elastic Compute Cloud User Guide.
73	Encrypted bool
74
75	// The identifier of the AWS Key Management Service (AWS KMS) customer master key
76	// (CMK) to use for Amazon EBS encryption. If this parameter is not specified, your
77	// AWS managed CMK for EBS is used. If KmsKeyId is specified, the encrypted state
78	// must be true. You can specify the CMK using any of the following:
79	//
80	// * Key ID. For
81	// example, 1234abcd-12ab-34cd-56ef-1234567890ab.
82	//
83	// * Key alias. For example,
84	// alias/ExampleAlias.
85	//
86	// * Key ARN. For example,
87	// arn:aws:kms:us-east-1:012345678910:key/1234abcd-12ab-34cd-56ef-1234567890ab.
88	//
89	// *
90	// Alias ARN. For example,
91	// arn:aws:kms:us-east-1:012345678910:alias/ExampleAlias.
92	//
93	// AWS authenticates the
94	// CMK asynchronously. Therefore, if you specify an ID, alias, or ARN that is not
95	// valid, the action can appear to complete, but eventually fails.
96	KmsKeyId *string
97
98	// When you copy an encrypted source snapshot using the Amazon EC2 Query API, you
99	// must supply a pre-signed URL. This parameter is optional for unencrypted
100	// snapshots. For more information, see Query requests
101	// (https://docs.aws.amazon.com/AWSEC2/latest/APIReference/Query-Requests.html).
102	// The PresignedUrl should use the snapshot source endpoint, the CopySnapshot
103	// action, and include the SourceRegion, SourceSnapshotId, and DestinationRegion
104	// parameters. The PresignedUrl must be signed using AWS Signature Version 4.
105	// Because EBS snapshots are stored in Amazon S3, the signing algorithm for this
106	// parameter uses the same logic that is described in Authenticating Requests:
107	// Using Query Parameters (AWS Signature Version 4)
108	// (https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html)
109	// in the Amazon Simple Storage Service API Reference. An invalid or improperly
110	// signed PresignedUrl will cause the copy operation to fail asynchronously, and
111	// the snapshot will move to an error state.
112	PresignedUrl *string
113
114	// The tags to apply to the new snapshot.
115	TagSpecifications []types.TagSpecification
116
117	// Used by the SDK's PresignURL autofill customization to specify the region the of
118	// the client's request.
119	destinationRegion *string
120}
121
122type CopySnapshotOutput struct {
123
124	// The ID of the new snapshot.
125	SnapshotId *string
126
127	// Any tags applied to the new snapshot.
128	Tags []types.Tag
129
130	// Metadata pertaining to the operation's result.
131	ResultMetadata middleware.Metadata
132}
133
134func addOperationCopySnapshotMiddlewares(stack *middleware.Stack, options Options) (err error) {
135	err = stack.Serialize.Add(&awsEc2query_serializeOpCopySnapshot{}, middleware.After)
136	if err != nil {
137		return err
138	}
139	err = stack.Deserialize.Add(&awsEc2query_deserializeOpCopySnapshot{}, middleware.After)
140	if err != nil {
141		return err
142	}
143	if err = addSetLoggerMiddleware(stack, options); err != nil {
144		return err
145	}
146	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
147		return err
148	}
149	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
150		return err
151	}
152	if err = addResolveEndpointMiddleware(stack, options); err != nil {
153		return err
154	}
155	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
156		return err
157	}
158	if err = addRetryMiddlewares(stack, options); err != nil {
159		return err
160	}
161	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
162		return err
163	}
164	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
165		return err
166	}
167	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
168		return err
169	}
170	if err = addClientUserAgent(stack); err != nil {
171		return err
172	}
173	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
174		return err
175	}
176	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
177		return err
178	}
179	if err = addCopySnapshotPresignURLMiddleware(stack, options); err != nil {
180		return err
181	}
182	if err = addOpCopySnapshotValidationMiddleware(stack); err != nil {
183		return err
184	}
185	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opCopySnapshot(options.Region), middleware.Before); err != nil {
186		return err
187	}
188	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
189		return err
190	}
191	if err = addResponseErrorMiddleware(stack); err != nil {
192		return err
193	}
194	if err = addRequestResponseLogging(stack, options); err != nil {
195		return err
196	}
197	return nil
198}
199
200func copyCopySnapshotInputForPresign(params interface{}) (interface{}, error) {
201	input, ok := params.(*CopySnapshotInput)
202	if !ok {
203		return nil, fmt.Errorf("expect *CopySnapshotInput type, got %T", params)
204	}
205	cpy := *input
206	return &cpy, nil
207}
208func getCopySnapshotPresignedUrl(params interface{}) (string, bool, error) {
209	input, ok := params.(*CopySnapshotInput)
210	if !ok {
211		return ``, false, fmt.Errorf("expect *CopySnapshotInput type, got %T", params)
212	}
213	if input.PresignedUrl == nil || len(*input.PresignedUrl) == 0 {
214		return ``, false, nil
215	}
216	return *input.PresignedUrl, true, nil
217}
218func getCopySnapshotSourceRegion(params interface{}) (string, bool, error) {
219	input, ok := params.(*CopySnapshotInput)
220	if !ok {
221		return ``, false, fmt.Errorf("expect *CopySnapshotInput type, got %T", params)
222	}
223	if input.SourceRegion == nil || len(*input.SourceRegion) == 0 {
224		return ``, false, nil
225	}
226	return *input.SourceRegion, true, nil
227}
228func setCopySnapshotPresignedUrl(params interface{}, value string) error {
229	input, ok := params.(*CopySnapshotInput)
230	if !ok {
231		return fmt.Errorf("expect *CopySnapshotInput type, got %T", params)
232	}
233	input.PresignedUrl = &value
234	return nil
235}
236func setCopySnapshotdestinationRegion(params interface{}, value string) error {
237	input, ok := params.(*CopySnapshotInput)
238	if !ok {
239		return fmt.Errorf("expect *CopySnapshotInput type, got %T", params)
240	}
241	input.destinationRegion = &value
242	return nil
243}
244func addCopySnapshotPresignURLMiddleware(stack *middleware.Stack, options Options) error {
245	return presignedurlcust.AddMiddleware(stack, presignedurlcust.Options{
246		Accessor: presignedurlcust.ParameterAccessor{
247			GetPresignedURL: getCopySnapshotPresignedUrl,
248
249			GetSourceRegion: getCopySnapshotSourceRegion,
250
251			CopyInput: copyCopySnapshotInputForPresign,
252
253			SetDestinationRegion: setCopySnapshotdestinationRegion,
254
255			SetPresignedURL: setCopySnapshotPresignedUrl,
256		},
257		Presigner: &presignAutoFillCopySnapshotClient{client: NewPresignClient(New(options))},
258	})
259}
260
261type presignAutoFillCopySnapshotClient struct {
262	client *PresignClient
263}
264
265// PresignURL is a middleware accessor that satisfies URLPresigner interface.
266func (c *presignAutoFillCopySnapshotClient) PresignURL(ctx context.Context, srcRegion string, params interface{}) (*v4.PresignedHTTPRequest, error) {
267	input, ok := params.(*CopySnapshotInput)
268	if !ok {
269		return nil, fmt.Errorf("expect *CopySnapshotInput type, got %T", params)
270	}
271	optFn := func(o *Options) {
272		o.Region = srcRegion
273		o.APIOptions = append(o.APIOptions, presignedurlcust.RemoveMiddleware)
274	}
275	presignOptFn := WithPresignClientFromClientOptions(optFn)
276	return c.client.PresignCopySnapshot(ctx, input, presignOptFn)
277}
278
279func newServiceMetadataMiddleware_opCopySnapshot(region string) *awsmiddleware.RegisterServiceMetadata {
280	return &awsmiddleware.RegisterServiceMetadata{
281		Region:        region,
282		ServiceID:     ServiceID,
283		SigningName:   "ec2",
284		OperationName: "CopySnapshot",
285	}
286}
287
288// PresignCopySnapshot is used to generate a presigned HTTP Request which contains
289// presigned URL, signed headers and HTTP method used.
290func (c *PresignClient) PresignCopySnapshot(ctx context.Context, params *CopySnapshotInput, optFns ...func(*PresignOptions)) (*v4.PresignedHTTPRequest, error) {
291	if params == nil {
292		params = &CopySnapshotInput{}
293	}
294	options := c.options.copy()
295	for _, fn := range optFns {
296		fn(&options)
297	}
298	clientOptFns := append(options.ClientOptions, withNopHTTPClientAPIOption)
299
300	result, _, err := c.client.invokeOperation(ctx, "CopySnapshot", params, clientOptFns,
301		addOperationCopySnapshotMiddlewares,
302		presignConverter(options).convertToPresignMiddleware,
303	)
304	if err != nil {
305		return nil, err
306	}
307
308	out := result.(*v4.PresignedHTTPRequest)
309	return out, nil
310}
311