Skip to content

Commit 898c9b5

Browse files
authored
Merge pull request #36487 from hashicorp/td-generate-tagtests-iam
Generated tagging acceptance tests for IAM resources
2 parents db09042 + c6022f5 commit 898c9b5

30 files changed

+5478
-375
lines changed

internal/generate/tagstests/file.tmpl

Lines changed: 32 additions & 32 deletions
Large diffs are not rendered by default.

internal/generate/tagstests/main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"go/token"
1616
"os"
1717
"path/filepath"
18+
"strconv"
1819
"strings"
1920

2021
"github.com/YakDriver/regexache"
@@ -165,6 +166,7 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
165166
FileName: v.fileName,
166167
}
167168
tagged := false
169+
skip := false
168170

169171
for _, line := range funcDecl.Doc.List {
170172
line := line.Text
@@ -210,13 +212,25 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
210212
d.Generator = attr
211213
}
212214
if attr, ok := args.Keyword["importIgnore"]; ok {
213-
d.ImportIgnore = strings.Split(attr, ",")
215+
d.ImportIgnore = strings.Split(attr, ";")
216+
}
217+
if attr, ok := args.Keyword["name"]; ok {
218+
d.Name = strings.ReplaceAll(attr, " ", "")
219+
}
220+
if attr, ok := args.Keyword["tagsTest"]; ok {
221+
if b, err := strconv.ParseBool(attr); err != nil {
222+
v.errs = append(v.errs, fmt.Errorf("invalid tagsTest value: %q at %s. Should be boolean value.", attr, fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
223+
continue
224+
} else if !b {
225+
v.g.Infof("Skipping tags test for %s.%s", v.packageName, v.functionName)
226+
skip = true
227+
}
214228
}
215229
}
216230
}
217231
}
218232

219-
if tagged {
233+
if tagged && !skip {
220234
v.taggedResources = append(v.taggedResources, d)
221235
}
222236

internal/provider/fwprovider/intercept.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework/path"
1313
"github.com/hashicorp/terraform-plugin-framework/resource"
1414
fwtypes "github.com/hashicorp/terraform-plugin-framework/types"
15+
"github.com/hashicorp/terraform-plugin-log/tflog"
1516
"github.com/hashicorp/terraform-provider-aws/internal/conns"
1617
"github.com/hashicorp/terraform-provider-aws/internal/errs"
1718
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
@@ -427,6 +428,11 @@ func (r tagsResourceInterceptor) read(ctx context.Context, request resource.Read
427428
ListTags(context.Context, any, string, string) error
428429
}); ok && r.tags.ResourceType != "" {
429430
err = v.ListTags(ctx, meta, identifier, r.tags.ResourceType) // Sets tags in Context
431+
} else {
432+
tflog.Warn(ctx, "No ListTags method found", map[string]interface{}{
433+
"ServicePackage": sp.ServicePackageName(),
434+
"ResourceType": r.tags.ResourceType,
435+
})
430436
}
431437

432438
// ISO partitions may not support tagging, giving error.
@@ -554,6 +560,11 @@ func (r tagsResourceInterceptor) update(ctx context.Context, request resource.Up
554560
UpdateTags(context.Context, any, string, string, any, any) error
555561
}); ok && r.tags.ResourceType != "" {
556562
err = v.UpdateTags(ctx, meta, identifier, r.tags.ResourceType, oldTagsAll, newTagsAll)
563+
} else {
564+
tflog.Warn(ctx, "No UpdateTags method found", map[string]interface{}{
565+
"ServicePackage": sp.ServicePackageName(),
566+
"ResourceType": r.tags.ResourceType,
567+
})
557568
}
558569

559570
// ISO partitions may not support tagging, giving error.

internal/provider/intercept.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
1010
"github.com/hashicorp/go-cty/cty"
11+
"github.com/hashicorp/terraform-plugin-log/tflog"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1314
"github.com/hashicorp/terraform-provider-aws/internal/conns"
@@ -274,6 +275,11 @@ func (r tagsResourceInterceptor) run(ctx context.Context, d schemaResourceData,
274275
UpdateTags(context.Context, any, string, string, any, any) error
275276
}); ok && r.tags.ResourceType != "" {
276277
err = v.UpdateTags(ctx, meta, identifier, r.tags.ResourceType, o, n)
278+
} else {
279+
tflog.Warn(ctx, "No UpdateTags method found", map[string]interface{}{
280+
"ServicePackage": sp.ServicePackageName(),
281+
"ResourceType": r.tags.ResourceType,
282+
})
277283
}
278284

279285
// ISO partitions may not support tagging, giving error.
@@ -326,6 +332,11 @@ func (r tagsResourceInterceptor) run(ctx context.Context, d schemaResourceData,
326332
ListTags(context.Context, any, string, string) error
327333
}); ok && r.tags.ResourceType != "" {
328334
err = v.ListTags(ctx, meta, identifier, r.tags.ResourceType) // Sets tags in Context
335+
} else {
336+
tflog.Warn(ctx, "No ListTags method found", map[string]interface{}{
337+
"ServicePackage": sp.ServicePackageName(),
338+
"ResourceType": r.tags.ResourceType,
339+
})
329340
}
330341

331342
// ISO partitions may not support tagging, giving error.

internal/provider/tags_interceptor.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88

99
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
10+
"github.com/hashicorp/terraform-plugin-log/tflog"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1112
"github.com/hashicorp/terraform-provider-aws/internal/conns"
1213
"github.com/hashicorp/terraform-provider-aws/internal/errs"
@@ -83,6 +84,11 @@ func tagsUpdateFunc(ctx context.Context, d schemaResourceData, sp conns.ServiceP
8384
UpdateTags(context.Context, any, string, string, any, any) error
8485
}); ok && spt.ResourceType != "" {
8586
err = v.UpdateTags(ctx, meta, identifier, spt.ResourceType, oldTags, newTags)
87+
} else {
88+
tflog.Warn(ctx, "No UpdateTags method found", map[string]interface{}{
89+
"ServicePackage": sp.ServicePackageName(),
90+
"ResourceType": spt.ResourceType,
91+
})
8692
}
8793

8894
// ISO partitions may not support tagging, giving error.
@@ -127,6 +133,11 @@ func tagsReadFunc(ctx context.Context, d schemaResourceData, sp conns.ServicePac
127133
ListTags(context.Context, any, string, string) error
128134
}); ok && spt.ResourceType != "" {
129135
err = v.ListTags(ctx, meta, identifier, spt.ResourceType) // Sets tags in Context
136+
} else {
137+
tflog.Warn(ctx, "No ListTags method found", map[string]interface{}{
138+
"ServicePackage": sp.ServicePackageName(),
139+
"ResourceType": spt.ResourceType,
140+
})
130141
}
131142

132143
// ISO partitions may not support tagging, giving error.

internal/service/iam/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
//go:generate go run ../../generate/tags/main.go -ServiceTagsSlice
55
//go:generate go run ../../generate/servicepackage/main.go
6+
//go:generate go run ../../generate/tagstests/main.go
67
// ONLY generate directives and package declaration! Do not add anything else to this file.
78

89
package iam

internal/service/iam/instance_profile.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333

3434
// @SDKResource("aws_iam_instance_profile", name="Instance Profile")
3535
// @Tags(identifierAttribute="id", resourceType="InstanceProfile")
36+
// @Testing(existsType="github.com/aws/aws-sdk-go/service/iam.InstanceProfile")
3637
func resourceInstanceProfile() *schema.Resource {
3738
return &schema.Resource{
3839
CreateWithoutTimeout: resourceInstanceProfileCreate,
@@ -325,3 +326,14 @@ func findInstanceProfileByName(ctx context.Context, conn *iam.IAM, name string)
325326

326327
return output.InstanceProfile, nil
327328
}
329+
330+
func instanceProfileTags(ctx context.Context, conn *iam.IAM, identifier string) ([]*iam.Tag, error) {
331+
output, err := conn.ListInstanceProfileTagsWithContext(ctx, &iam.ListInstanceProfileTagsInput{
332+
InstanceProfileName: aws.String(identifier),
333+
})
334+
if err != nil {
335+
return nil, err
336+
}
337+
338+
return output.Tags, nil
339+
}

0 commit comments

Comments
 (0)