4
4
package ec2
5
5
6
6
import (
7
- "bytes"
8
7
"context"
9
8
"fmt"
10
- "log"
11
9
"sort"
12
10
"time"
13
11
@@ -20,7 +18,6 @@ import (
20
18
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
21
19
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
22
20
"github.com/hashicorp/terraform-provider-aws/internal/conns"
23
- "github.com/hashicorp/terraform-provider-aws/internal/create"
24
21
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
25
22
"github.com/hashicorp/terraform-provider-aws/internal/flex"
26
23
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
@@ -50,7 +47,6 @@ func dataSourceAMI() *schema.Resource {
50
47
"block_device_mappings" : {
51
48
Type : schema .TypeSet ,
52
49
Computed : true ,
53
- Set : amiBlockDeviceMappingHash ,
54
50
Elem : & schema.Resource {
55
51
Schema : map [string ]* schema.Schema {
56
52
names .AttrDeviceName : {
@@ -170,7 +166,6 @@ func dataSourceAMI() *schema.Resource {
170
166
"product_codes" : {
171
167
Type : schema .TypeSet ,
172
168
Computed : true ,
173
- Set : amiProductCodesHash ,
174
169
Elem : & schema.Resource {
175
170
Schema : map [string ]* schema.Schema {
176
171
"product_code_id" : {
@@ -349,48 +344,54 @@ func dataSourceAMIRead(ctx context.Context, d *schema.ResourceData, meta interfa
349
344
return diags
350
345
}
351
346
352
- func flattenAMIBlockDeviceMappings (m []awstypes.BlockDeviceMapping ) * schema. Set {
353
- s := & schema. Set {
354
- F : amiBlockDeviceMappingHash ,
347
+ func flattenAMIBlockDeviceMappings (apiObjects []awstypes.BlockDeviceMapping ) [] interface {} {
348
+ if len ( apiObjects ) == 0 {
349
+ return nil
355
350
}
356
- for _ , v := range m {
357
- mapping := map [string ]interface {}{
358
- names .AttrDeviceName : aws .ToString (v .DeviceName ),
359
- names .AttrVirtualName : aws .ToString (v .VirtualName ),
351
+
352
+ var tfList []interface {}
353
+
354
+ for _ , apiObject := range apiObjects {
355
+ tfMap := map [string ]interface {}{
356
+ names .AttrDeviceName : aws .ToString (apiObject .DeviceName ),
357
+ names .AttrVirtualName : aws .ToString (apiObject .VirtualName ),
360
358
}
361
359
362
- if v .Ebs != nil {
360
+ if apiObject := apiObject .Ebs ; apiObject != nil {
363
361
ebs := map [string ]interface {}{
364
- names .AttrDeleteOnTermination : fmt . Sprintf ( "%t" , aws . ToBool ( v . Ebs . DeleteOnTermination ) ),
365
- names .AttrEncrypted : fmt . Sprintf ( "%t" , aws . ToBool ( v . Ebs . Encrypted ) ),
366
- names .AttrIOPS : fmt . Sprintf ( "%d" , aws . ToInt32 ( v . Ebs . Iops ) ),
367
- names .AttrThroughput : fmt . Sprintf ( "%d" , aws .ToInt32 ( v . Ebs . Throughput ) ),
368
- names .AttrVolumeSize : fmt . Sprintf ( "%d" , aws . ToInt32 ( v . Ebs . VolumeSize ) ),
369
- names .AttrSnapshotID : aws . ToString ( v . Ebs . SnapshotId ),
370
- names .AttrVolumeType : v . Ebs .VolumeType ,
362
+ names .AttrDeleteOnTermination : flex . BoolToStringValue ( apiObject . DeleteOnTermination ),
363
+ names .AttrEncrypted : flex . BoolToStringValue ( apiObject . Encrypted ),
364
+ names .AttrIOPS : flex . Int32ToStringValue ( apiObject . Iops ),
365
+ names .AttrSnapshotID : aws .ToString ( apiObject . SnapshotId ),
366
+ names .AttrThroughput : flex . Int32ToStringValue ( apiObject . Throughput ),
367
+ names .AttrVolumeSize : flex . Int32ToStringValue ( apiObject . VolumeSize ),
368
+ names .AttrVolumeType : apiObject .VolumeType ,
371
369
}
372
370
373
- mapping ["ebs" ] = ebs
371
+ tfMap ["ebs" ] = ebs
374
372
}
375
373
376
- log .Printf ("[DEBUG] aws_ami - adding block device mapping: %v" , mapping )
377
- s .Add (mapping )
374
+ tfList = append (tfList , tfMap )
378
375
}
379
- return s
376
+
377
+ return tfList
380
378
}
381
379
382
- func flattenAMIProductCodes (m []awstypes.ProductCode ) * schema. Set {
383
- s := & schema. Set {
384
- F : amiProductCodesHash ,
380
+ func flattenAMIProductCodes (apiObjects []awstypes.ProductCode ) [] interface {} {
381
+ if len ( apiObjects ) == 0 {
382
+ return nil
385
383
}
386
- for _ , v := range m {
387
- code := map [string ]interface {}{
388
- "product_code_id" : aws .ToString (v .ProductCodeId ),
389
- "product_code_type" : v .ProductCodeType ,
390
- }
391
- s .Add (code )
384
+
385
+ var tfList []interface {}
386
+
387
+ for _ , apiObject := range apiObjects {
388
+ tfList = append (tfList , map [string ]interface {}{
389
+ "product_code_id" : aws .ToString (apiObject .ProductCodeId ),
390
+ "product_code_type" : apiObject .ProductCodeType ,
391
+ })
392
392
}
393
- return s
393
+
394
+ return tfList
394
395
}
395
396
396
397
func amiRootSnapshotId (image awstypes.Image ) string {
@@ -420,39 +421,3 @@ func flattenAMIStateReason(m *awstypes.StateReason) map[string]interface{} {
420
421
}
421
422
return s
422
423
}
423
-
424
- func amiBlockDeviceMappingHash (v interface {}) int {
425
- var buf bytes.Buffer
426
- // All keys added in alphabetical order.
427
- m := v .(map [string ]interface {})
428
- buf .WriteString (fmt .Sprintf ("%s-" , m [names .AttrDeviceName ].(string )))
429
- if d , ok := m ["ebs" ]; ok {
430
- if len (d .(map [string ]interface {})) > 0 {
431
- e := d .(map [string ]interface {})
432
- buf .WriteString (fmt .Sprintf ("%s-" , e [names .AttrDeleteOnTermination ].(string )))
433
- buf .WriteString (fmt .Sprintf ("%s-" , e [names .AttrEncrypted ].(string )))
434
- buf .WriteString (fmt .Sprintf ("%s-" , e [names .AttrIOPS ].(string )))
435
- buf .WriteString (fmt .Sprintf ("%s-" , e [names .AttrVolumeSize ].(string )))
436
- buf .WriteString (fmt .Sprintf ("%s-" , e [names .AttrVolumeType ]))
437
- }
438
- }
439
- if d , ok := m ["no_device" ]; ok {
440
- buf .WriteString (fmt .Sprintf ("%s-" , d .(string )))
441
- }
442
- if d , ok := m [names .AttrVirtualName ]; ok {
443
- buf .WriteString (fmt .Sprintf ("%s-" , d .(string )))
444
- }
445
- if d , ok := m [names .AttrSnapshotID ]; ok {
446
- buf .WriteString (fmt .Sprintf ("%s-" , d .(string )))
447
- }
448
- return create .StringHashcode (buf .String ())
449
- }
450
-
451
- func amiProductCodesHash (v interface {}) int {
452
- var buf bytes.Buffer
453
- m := v .(map [string ]interface {})
454
- // All keys added in alphabetical order.
455
- buf .WriteString (fmt .Sprintf ("%s-" , m ["product_code_id" ].(string )))
456
- buf .WriteString (fmt .Sprintf ("%s-" , m ["product_code_type" ].(string )))
457
- return create .StringHashcode (buf .String ())
458
- }
0 commit comments