8
8
"fmt"
9
9
"log"
10
10
"net/url"
11
- "reflect"
12
11
13
12
"github.com/aws/aws-sdk-go-v2/aws"
14
13
"github.com/aws/aws-sdk-go-v2/service/iam"
@@ -25,6 +24,7 @@ import (
25
24
"github.com/hashicorp/terraform-provider-aws/internal/slices"
26
25
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
27
26
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
27
+ inttypes "github.com/hashicorp/terraform-provider-aws/internal/types"
28
28
"github.com/hashicorp/terraform-provider-aws/internal/verify"
29
29
"github.com/hashicorp/terraform-provider-aws/names"
30
30
)
@@ -113,22 +113,22 @@ func resourcePolicyCreate(ctx context.Context, d *schema.ResourceData, meta any)
113
113
}
114
114
115
115
name := create .Name (d .Get (names .AttrName ).(string ), d .Get (names .AttrNamePrefix ).(string ))
116
- input := & iam.CreatePolicyInput {
116
+ input := iam.CreatePolicyInput {
117
117
Description : aws .String (d .Get (names .AttrDescription ).(string )),
118
118
Path : aws .String (d .Get (names .AttrPath ).(string )),
119
119
PolicyDocument : aws .String (policy ),
120
120
PolicyName : aws .String (name ),
121
121
Tags : getTagsIn (ctx ),
122
122
}
123
123
124
- output , err := conn .CreatePolicy (ctx , input )
124
+ output , err := conn .CreatePolicy (ctx , & input )
125
125
126
126
// Some partitions (e.g. ISO) may not support tag-on-create.
127
127
partition := meta .(* conns.AWSClient ).Partition (ctx )
128
128
if input .Tags != nil && errs .IsUnsupportedOperationInPartitionError (partition , err ) {
129
129
input .Tags = nil
130
130
131
- output , err = conn .CreatePolicy (ctx , input )
131
+ output , err = conn .CreatePolicy (ctx , & input )
132
132
}
133
133
134
134
if err != nil {
@@ -171,7 +171,7 @@ func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta any) d
171
171
return nil , err
172
172
}
173
173
174
- if v , err := findPolicyVersion (ctx , conn , d .Id (), aws .ToString (iamPolicy .policy .DefaultVersionId )); err == nil {
174
+ if v , err := findPolicyVersionByTwoPartKey (ctx , conn , d .Id (), aws .ToString (iamPolicy .policy .DefaultVersionId )); err == nil {
175
175
iamPolicy .policyVersion = v
176
176
} else {
177
177
return nil , err
@@ -202,7 +202,6 @@ func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta any) d
202
202
setTagsOut (ctx , policy .Tags )
203
203
204
204
policyDocument , err := url .QueryUnescape (aws .ToString (output .policyVersion .Document ))
205
-
206
205
if err != nil {
207
206
return sdkdiag .AppendErrorf (diags , "parsing IAM Policy (%s) document: %s" , d .Id (), err )
208
207
}
@@ -231,13 +230,13 @@ func resourcePolicyUpdate(ctx context.Context, d *schema.ResourceData, meta any)
231
230
return sdkdiag .AppendErrorf (diags , "policy (%s) is invalid JSON: %s" , policy , err )
232
231
}
233
232
234
- input := & iam.CreatePolicyVersionInput {
233
+ input := iam.CreatePolicyVersionInput {
235
234
PolicyArn : aws .String (d .Id ()),
236
235
PolicyDocument : aws .String (policy ),
237
236
SetAsDefault : true ,
238
237
}
239
238
240
- _ , err = conn .CreatePolicyVersion (ctx , input )
239
+ _ , err = conn .CreatePolicyVersion (ctx , & input )
241
240
242
241
if err != nil {
243
242
return sdkdiag .AppendErrorf (diags , "updating IAM Policy (%s): %s" , d .Id (), err )
@@ -273,9 +272,10 @@ func resourcePolicyDelete(ctx context.Context, d *schema.ResourceData, meta any)
273
272
}
274
273
275
274
log .Printf ("[INFO] Deleting IAM Policy: %s" , d .Id ())
276
- _ , err = conn . DeletePolicy ( ctx , & iam.DeletePolicyInput {
275
+ input := iam.DeletePolicyInput {
277
276
PolicyArn : aws .String (d .Id ()),
278
- })
277
+ }
278
+ _ , err = conn .DeletePolicy (ctx , & input )
279
279
280
280
if errs.IsA [* awstypes.NoSuchEntityException ](err ) {
281
281
return diags
@@ -325,12 +325,12 @@ func policyPruneVersions(ctx context.Context, conn *iam.Client, arn string) erro
325
325
}
326
326
327
327
func policyDeleteVersion (ctx context.Context , conn * iam.Client , arn , versionID string ) error {
328
- input := & iam.DeletePolicyVersionInput {
328
+ input := iam.DeletePolicyVersionInput {
329
329
PolicyArn : aws .String (arn ),
330
330
VersionId : aws .String (versionID ),
331
331
}
332
332
333
- _ , err := conn .DeletePolicyVersion (ctx , input )
333
+ _ , err := conn .DeletePolicyVersion (ctx , & input )
334
334
335
335
if err != nil {
336
336
return fmt .Errorf ("deleting IAM Policy (%s) version (%s): %w" , arn , versionID , err )
@@ -340,10 +340,14 @@ func policyDeleteVersion(ctx context.Context, conn *iam.Client, arn, versionID s
340
340
}
341
341
342
342
func findPolicyByARN (ctx context.Context , conn * iam.Client , arn string ) (* awstypes.Policy , error ) {
343
- input := & iam.GetPolicyInput {
343
+ input := iam.GetPolicyInput {
344
344
PolicyArn : aws .String (arn ),
345
345
}
346
346
347
+ return findPolicy (ctx , conn , & input )
348
+ }
349
+
350
+ func findPolicy (ctx context.Context , conn * iam.Client , input * iam.GetPolicyInput ) (* awstypes.Policy , error ) {
347
351
output , err := conn .GetPolicy (ctx , input )
348
352
349
353
if errs.IsA [* awstypes.NoSuchEntityException ](err ) {
@@ -365,12 +369,12 @@ func findPolicyByARN(ctx context.Context, conn *iam.Client, arn string) (*awstyp
365
369
}
366
370
367
371
func findPolicyByTwoPartKey (ctx context.Context , conn * iam.Client , name , pathPrefix string ) (* awstypes.Policy , error ) {
368
- input := & iam.ListPoliciesInput {}
372
+ var input iam.ListPoliciesInput
369
373
if pathPrefix != "" {
370
374
input .PathPrefix = aws .String (pathPrefix )
371
375
}
372
376
373
- output , err := findPolicies (ctx , conn , input )
377
+ output , err := findPolicies (ctx , conn , & input )
374
378
375
379
if err != nil {
376
380
return nil , err
@@ -397,7 +401,7 @@ func findPolicies(ctx context.Context, conn *iam.Client, input *iam.ListPolicies
397
401
}
398
402
399
403
for _ , v := range page .Policies {
400
- if ! reflect . ValueOf ( v ). IsZero () {
404
+ if p := & v ; ! inttypes . IsZero (p ) {
401
405
output = append (output , v )
402
406
}
403
407
}
@@ -406,12 +410,16 @@ func findPolicies(ctx context.Context, conn *iam.Client, input *iam.ListPolicies
406
410
return output , nil
407
411
}
408
412
409
- func findPolicyVersion (ctx context.Context , conn * iam.Client , arn , versionID string ) (* awstypes.PolicyVersion , error ) {
410
- input := & iam.GetPolicyVersionInput {
413
+ func findPolicyVersionByTwoPartKey (ctx context.Context , conn * iam.Client , arn , versionID string ) (* awstypes.PolicyVersion , error ) {
414
+ input := iam.GetPolicyVersionInput {
411
415
PolicyArn : aws .String (arn ),
412
416
VersionId : aws .String (versionID ),
413
417
}
414
418
419
+ return findPolicyVersion (ctx , conn , & input )
420
+ }
421
+
422
+ func findPolicyVersion (ctx context.Context , conn * iam.Client , input * iam.GetPolicyVersionInput ) (* awstypes.PolicyVersion , error ) {
415
423
output , err := conn .GetPolicyVersion (ctx , input )
416
424
417
425
if errs.IsA [* awstypes.NoSuchEntityException ](err ) {
@@ -433,9 +441,14 @@ func findPolicyVersion(ctx context.Context, conn *iam.Client, arn, versionID str
433
441
}
434
442
435
443
func findPolicyVersionsByARN (ctx context.Context , conn * iam.Client , arn string ) ([]awstypes.PolicyVersion , error ) {
436
- input := & iam.ListPolicyVersionsInput {
444
+ input := iam.ListPolicyVersionsInput {
437
445
PolicyArn : aws .String (arn ),
438
446
}
447
+
448
+ return findPolicyVersions (ctx , conn , & input )
449
+ }
450
+
451
+ func findPolicyVersions (ctx context.Context , conn * iam.Client , input * iam.ListPolicyVersionsInput ) ([]awstypes.PolicyVersion , error ) {
439
452
var output []awstypes.PolicyVersion
440
453
441
454
pages := iam .NewListPolicyVersionsPaginator (conn , input )
@@ -454,7 +467,7 @@ func findPolicyVersionsByARN(ctx context.Context, conn *iam.Client, arn string)
454
467
}
455
468
456
469
for _ , v := range page .Versions {
457
- if ! reflect . ValueOf ( v ). IsZero () {
470
+ if p := & v ; ! inttypes . IsZero (p ) {
458
471
output = append (output , v )
459
472
}
460
473
}
0 commit comments