@@ -6,23 +6,19 @@ package ec2
6
6
import (
7
7
"bytes"
8
8
"context"
9
- "errors"
10
9
"fmt"
11
10
"log"
12
11
"strings"
13
12
"time"
14
13
15
14
"github.com/aws/aws-sdk-go-v2/aws"
16
15
"github.com/aws/aws-sdk-go-v2/service/ec2"
17
- awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types"
18
16
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
19
17
"github.com/hashicorp/terraform-plugin-log/tflog"
20
18
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
21
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
22
19
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
23
20
"github.com/hashicorp/terraform-provider-aws/internal/conns"
24
21
"github.com/hashicorp/terraform-provider-aws/internal/create"
25
- "github.com/hashicorp/terraform-provider-aws/internal/enum"
26
22
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
27
23
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
28
24
"github.com/hashicorp/terraform-provider-aws/names"
@@ -208,48 +204,6 @@ func volumeAttachmentID(name, volumeID, instanceID string) string {
208
204
return fmt .Sprintf ("vai-%d" , create .StringHashcode (buf .String ()))
209
205
}
210
206
211
- func findVolumeAttachment (ctx context.Context , conn * ec2.Client , volumeID , instanceID , deviceName string ) (* awstypes.VolumeAttachment , error ) {
212
- input := ec2.DescribeVolumesInput {
213
- Filters : newAttributeFilterList (map [string ]string {
214
- "attachment.device" : deviceName ,
215
- "attachment.instance-id" : instanceID ,
216
- }),
217
- VolumeIds : []string {volumeID },
218
- }
219
-
220
- output , err := findEBSVolume (ctx , conn , & input )
221
-
222
- if err != nil {
223
- return nil , err
224
- }
225
-
226
- if state := output .State ; state == awstypes .VolumeStateAvailable || state == awstypes .VolumeStateDeleted {
227
- return nil , & retry.NotFoundError {
228
- Message : string (state ),
229
- LastRequest : input ,
230
- }
231
- }
232
-
233
- // Eventual consistency check.
234
- if aws .ToString (output .VolumeId ) != volumeID {
235
- return nil , & retry.NotFoundError {
236
- LastRequest : input ,
237
- }
238
- }
239
-
240
- for _ , v := range output .Attachments {
241
- if v .State == awstypes .VolumeAttachmentStateDetached {
242
- continue
243
- }
244
-
245
- if aws .ToString (v .Device ) == deviceName && aws .ToString (v .InstanceId ) == instanceID {
246
- return & v , nil
247
- }
248
- }
249
-
250
- return nil , & retry.NotFoundError {}
251
- }
252
-
253
207
func stopVolumeAttachmentInstance (ctx context.Context , conn * ec2.Client , id string , force bool , timeout time.Duration ) error {
254
208
tflog .Info (ctx , "Stopping EC2 Instance" , map [string ]any {
255
209
"ec2_instance_id" : id ,
@@ -271,92 +225,3 @@ func stopVolumeAttachmentInstance(ctx context.Context, conn *ec2.Client, id stri
271
225
272
226
return nil
273
227
}
274
-
275
- func waitVolumeAttachmentInstanceStopped (ctx context.Context , conn * ec2.Client , id string , timeout time.Duration ) (* awstypes.Instance , error ) {
276
- stateConf := & retry.StateChangeConf {
277
- Pending : enum .Slice (
278
- awstypes .InstanceStateNamePending ,
279
- awstypes .InstanceStateNameRunning ,
280
- awstypes .InstanceStateNameShuttingDown ,
281
- awstypes .InstanceStateNameStopping ,
282
- ),
283
- Target : enum .Slice (awstypes .InstanceStateNameStopped ),
284
- Refresh : statusVolumeAttachmentInstanceState (ctx , conn , id ),
285
- Timeout : timeout ,
286
- Delay : 10 * time .Second ,
287
- MinTimeout : 3 * time .Second ,
288
- }
289
-
290
- outputRaw , err := stateConf .WaitForStateContext (ctx )
291
-
292
- if output , ok := outputRaw .(* awstypes.Instance ); ok {
293
- if stateReason := output .StateReason ; stateReason != nil {
294
- tfresource .SetLastError (err , errors .New (aws .ToString (stateReason .Message )))
295
- }
296
-
297
- return output , err
298
- }
299
-
300
- return nil , err
301
- }
302
-
303
- func waitVolumeAttachmentInstanceReady (ctx context.Context , conn * ec2.Client , id string , timeout time.Duration ) (* awstypes.Instance , error ) {
304
- stateConf := & retry.StateChangeConf {
305
- Pending : enum .Slice (awstypes .InstanceStateNamePending , awstypes .InstanceStateNameStopping ),
306
- Target : enum .Slice (awstypes .InstanceStateNameRunning , awstypes .InstanceStateNameStopped ),
307
- Refresh : statusVolumeAttachmentInstanceState (ctx , conn , id ),
308
- Timeout : timeout ,
309
- Delay : 10 * time .Second ,
310
- MinTimeout : 3 * time .Second ,
311
- }
312
-
313
- outputRaw , err := stateConf .WaitForStateContext (ctx )
314
-
315
- if output , ok := outputRaw .(* awstypes.Instance ); ok {
316
- if stateReason := output .StateReason ; stateReason != nil {
317
- tfresource .SetLastError (err , errors .New (aws .ToString (stateReason .Message )))
318
- }
319
-
320
- return output , err
321
- }
322
-
323
- return nil , err
324
- }
325
-
326
- func waitVolumeAttachmentDeleted (ctx context.Context , conn * ec2.Client , volumeID , instanceID , deviceName string , timeout time.Duration ) (* awstypes.VolumeAttachment , error ) {
327
- stateConf := & retry.StateChangeConf {
328
- Pending : enum .Slice (awstypes .VolumeAttachmentStateDetaching ),
329
- Target : []string {},
330
- Refresh : statusVolumeAttachment (ctx , conn , volumeID , instanceID , deviceName ),
331
- Timeout : timeout ,
332
- Delay : 10 * time .Second ,
333
- MinTimeout : 3 * time .Second ,
334
- }
335
-
336
- outputRaw , err := stateConf .WaitForStateContext (ctx )
337
-
338
- if output , ok := outputRaw .(* awstypes.VolumeAttachment ); ok {
339
- return output , err
340
- }
341
-
342
- return nil , err
343
- }
344
-
345
- func statusVolumeAttachmentInstanceState (ctx context.Context , conn * ec2.Client , id string ) retry.StateRefreshFunc {
346
- return func () (any , string , error ) {
347
- // Don't call FindInstanceByID as it maps useful status codes to NotFoundError.
348
- output , err := findInstance (ctx , conn , & ec2.DescribeInstancesInput {
349
- InstanceIds : []string {id },
350
- })
351
-
352
- if tfresource .NotFound (err ) {
353
- return nil , "" , nil
354
- }
355
-
356
- if err != nil {
357
- return nil , "" , err
358
- }
359
-
360
- return output , string (output .State .Name ), nil
361
- }
362
- }
0 commit comments