Skip to content

Commit f3c5032

Browse files
committed
r/aws_iam_policy: Use 'inttypes.IsZero'.
1 parent e70bd48 commit f3c5032

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

internal/service/iam/policy.go

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"log"
1010
"net/url"
11-
"reflect"
1211

1312
"github.com/aws/aws-sdk-go-v2/aws"
1413
"github.com/aws/aws-sdk-go-v2/service/iam"
@@ -25,6 +24,7 @@ import (
2524
"github.com/hashicorp/terraform-provider-aws/internal/slices"
2625
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
2726
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
27+
inttypes "github.com/hashicorp/terraform-provider-aws/internal/types"
2828
"github.com/hashicorp/terraform-provider-aws/internal/verify"
2929
"github.com/hashicorp/terraform-provider-aws/names"
3030
)
@@ -113,22 +113,22 @@ func resourcePolicyCreate(ctx context.Context, d *schema.ResourceData, meta any)
113113
}
114114

115115
name := create.Name(d.Get(names.AttrName).(string), d.Get(names.AttrNamePrefix).(string))
116-
input := &iam.CreatePolicyInput{
116+
input := iam.CreatePolicyInput{
117117
Description: aws.String(d.Get(names.AttrDescription).(string)),
118118
Path: aws.String(d.Get(names.AttrPath).(string)),
119119
PolicyDocument: aws.String(policy),
120120
PolicyName: aws.String(name),
121121
Tags: getTagsIn(ctx),
122122
}
123123

124-
output, err := conn.CreatePolicy(ctx, input)
124+
output, err := conn.CreatePolicy(ctx, &input)
125125

126126
// Some partitions (e.g. ISO) may not support tag-on-create.
127127
partition := meta.(*conns.AWSClient).Partition(ctx)
128128
if input.Tags != nil && errs.IsUnsupportedOperationInPartitionError(partition, err) {
129129
input.Tags = nil
130130

131-
output, err = conn.CreatePolicy(ctx, input)
131+
output, err = conn.CreatePolicy(ctx, &input)
132132
}
133133

134134
if err != nil {
@@ -171,7 +171,7 @@ func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta any) d
171171
return nil, err
172172
}
173173

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 {
175175
iamPolicy.policyVersion = v
176176
} else {
177177
return nil, err
@@ -202,7 +202,6 @@ func resourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta any) d
202202
setTagsOut(ctx, policy.Tags)
203203

204204
policyDocument, err := url.QueryUnescape(aws.ToString(output.policyVersion.Document))
205-
206205
if err != nil {
207206
return sdkdiag.AppendErrorf(diags, "parsing IAM Policy (%s) document: %s", d.Id(), err)
208207
}
@@ -231,13 +230,13 @@ func resourcePolicyUpdate(ctx context.Context, d *schema.ResourceData, meta any)
231230
return sdkdiag.AppendErrorf(diags, "policy (%s) is invalid JSON: %s", policy, err)
232231
}
233232

234-
input := &iam.CreatePolicyVersionInput{
233+
input := iam.CreatePolicyVersionInput{
235234
PolicyArn: aws.String(d.Id()),
236235
PolicyDocument: aws.String(policy),
237236
SetAsDefault: true,
238237
}
239238

240-
_, err = conn.CreatePolicyVersion(ctx, input)
239+
_, err = conn.CreatePolicyVersion(ctx, &input)
241240

242241
if err != nil {
243242
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)
273272
}
274273

275274
log.Printf("[INFO] Deleting IAM Policy: %s", d.Id())
276-
_, err = conn.DeletePolicy(ctx, &iam.DeletePolicyInput{
275+
input := iam.DeletePolicyInput{
277276
PolicyArn: aws.String(d.Id()),
278-
})
277+
}
278+
_, err = conn.DeletePolicy(ctx, &input)
279279

280280
if errs.IsA[*awstypes.NoSuchEntityException](err) {
281281
return diags
@@ -325,12 +325,12 @@ func policyPruneVersions(ctx context.Context, conn *iam.Client, arn string) erro
325325
}
326326

327327
func policyDeleteVersion(ctx context.Context, conn *iam.Client, arn, versionID string) error {
328-
input := &iam.DeletePolicyVersionInput{
328+
input := iam.DeletePolicyVersionInput{
329329
PolicyArn: aws.String(arn),
330330
VersionId: aws.String(versionID),
331331
}
332332

333-
_, err := conn.DeletePolicyVersion(ctx, input)
333+
_, err := conn.DeletePolicyVersion(ctx, &input)
334334

335335
if err != nil {
336336
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
340340
}
341341

342342
func findPolicyByARN(ctx context.Context, conn *iam.Client, arn string) (*awstypes.Policy, error) {
343-
input := &iam.GetPolicyInput{
343+
input := iam.GetPolicyInput{
344344
PolicyArn: aws.String(arn),
345345
}
346346

347+
return findPolicy(ctx, conn, &input)
348+
}
349+
350+
func findPolicy(ctx context.Context, conn *iam.Client, input *iam.GetPolicyInput) (*awstypes.Policy, error) {
347351
output, err := conn.GetPolicy(ctx, input)
348352

349353
if errs.IsA[*awstypes.NoSuchEntityException](err) {
@@ -365,12 +369,12 @@ func findPolicyByARN(ctx context.Context, conn *iam.Client, arn string) (*awstyp
365369
}
366370

367371
func findPolicyByTwoPartKey(ctx context.Context, conn *iam.Client, name, pathPrefix string) (*awstypes.Policy, error) {
368-
input := &iam.ListPoliciesInput{}
372+
var input iam.ListPoliciesInput
369373
if pathPrefix != "" {
370374
input.PathPrefix = aws.String(pathPrefix)
371375
}
372376

373-
output, err := findPolicies(ctx, conn, input)
377+
output, err := findPolicies(ctx, conn, &input)
374378

375379
if err != nil {
376380
return nil, err
@@ -397,7 +401,7 @@ func findPolicies(ctx context.Context, conn *iam.Client, input *iam.ListPolicies
397401
}
398402

399403
for _, v := range page.Policies {
400-
if !reflect.ValueOf(v).IsZero() {
404+
if p := &v; !inttypes.IsZero(p) {
401405
output = append(output, v)
402406
}
403407
}
@@ -406,12 +410,16 @@ func findPolicies(ctx context.Context, conn *iam.Client, input *iam.ListPolicies
406410
return output, nil
407411
}
408412

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{
411415
PolicyArn: aws.String(arn),
412416
VersionId: aws.String(versionID),
413417
}
414418

419+
return findPolicyVersion(ctx, conn, &input)
420+
}
421+
422+
func findPolicyVersion(ctx context.Context, conn *iam.Client, input *iam.GetPolicyVersionInput) (*awstypes.PolicyVersion, error) {
415423
output, err := conn.GetPolicyVersion(ctx, input)
416424

417425
if errs.IsA[*awstypes.NoSuchEntityException](err) {
@@ -433,9 +441,14 @@ func findPolicyVersion(ctx context.Context, conn *iam.Client, arn, versionID str
433441
}
434442

435443
func findPolicyVersionsByARN(ctx context.Context, conn *iam.Client, arn string) ([]awstypes.PolicyVersion, error) {
436-
input := &iam.ListPolicyVersionsInput{
444+
input := iam.ListPolicyVersionsInput{
437445
PolicyArn: aws.String(arn),
438446
}
447+
448+
return findPolicyVersions(ctx, conn, &input)
449+
}
450+
451+
func findPolicyVersions(ctx context.Context, conn *iam.Client, input *iam.ListPolicyVersionsInput) ([]awstypes.PolicyVersion, error) {
439452
var output []awstypes.PolicyVersion
440453

441454
pages := iam.NewListPolicyVersionsPaginator(conn, input)
@@ -454,7 +467,7 @@ func findPolicyVersionsByARN(ctx context.Context, conn *iam.Client, arn string)
454467
}
455468

456469
for _, v := range page.Versions {
457-
if !reflect.ValueOf(v).IsZero() {
470+
if p := &v; !inttypes.IsZero(p) {
458471
output = append(output, v)
459472
}
460473
}

internal/service/iam/policy_data_source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func dataSourcePolicyRead(ctx context.Context, d *schema.ResourceData, meta any)
113113

114114
output, err := tfresource.RetryWhenNotFound(ctx, propagationTimeout,
115115
func(ctx context.Context) (*awstypes.PolicyVersion, error) {
116-
return findPolicyVersion(ctx, conn, arn, aws.ToString(policy.DefaultVersionId))
116+
return findPolicyVersionByTwoPartKey(ctx, conn, arn, aws.ToString(policy.DefaultVersionId))
117117
},
118118
)
119119

0 commit comments

Comments
 (0)