Skip to content

Commit

Permalink
Merge pull request #36487 from hashicorp/td-generate-tagtests-iam
Browse files Browse the repository at this point in the history
Generated tagging acceptance tests for IAM resources
  • Loading branch information
gdavison committed Mar 21, 2024
2 parents db09042 + c6022f5 commit 898c9b5
Show file tree
Hide file tree
Showing 30 changed files with 5,478 additions and 375 deletions.
64 changes: 32 additions & 32 deletions internal/generate/tagstests/file.tmpl

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions internal/generate/tagstests/main.go
Expand Up @@ -15,6 +15,7 @@ import (
"go/token"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/YakDriver/regexache"
Expand Down Expand Up @@ -165,6 +166,7 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
FileName: v.fileName,
}
tagged := false
skip := false

for _, line := range funcDecl.Doc.List {
line := line.Text
Expand Down Expand Up @@ -210,13 +212,25 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
d.Generator = attr
}
if attr, ok := args.Keyword["importIgnore"]; ok {
d.ImportIgnore = strings.Split(attr, ",")
d.ImportIgnore = strings.Split(attr, ";")
}
if attr, ok := args.Keyword["name"]; ok {
d.Name = strings.ReplaceAll(attr, " ", "")
}
if attr, ok := args.Keyword["tagsTest"]; ok {
if b, err := strconv.ParseBool(attr); err != nil {
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)))
continue
} else if !b {
v.g.Infof("Skipping tags test for %s.%s", v.packageName, v.functionName)
skip = true
}
}
}
}
}

if tagged {
if tagged && !skip {
v.taggedResources = append(v.taggedResources, d)
}

Expand Down
11 changes: 11 additions & 0 deletions internal/provider/fwprovider/intercept.go
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
fwtypes "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
Expand Down Expand Up @@ -427,6 +428,11 @@ func (r tagsResourceInterceptor) read(ctx context.Context, request resource.Read
ListTags(context.Context, any, string, string) error
}); ok && r.tags.ResourceType != "" {
err = v.ListTags(ctx, meta, identifier, r.tags.ResourceType) // Sets tags in Context
} else {
tflog.Warn(ctx, "No ListTags method found", map[string]interface{}{
"ServicePackage": sp.ServicePackageName(),
"ResourceType": r.tags.ResourceType,
})
}

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

// ISO partitions may not support tagging, giving error.
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/intercept.go
Expand Up @@ -8,6 +8,7 @@ import (

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

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

// ISO partitions may not support tagging, giving error.
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/tags_interceptor.go
Expand Up @@ -7,6 +7,7 @@ import (
"context"

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

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

// ISO partitions may not support tagging, giving error.
Expand Down
1 change: 1 addition & 0 deletions internal/service/iam/generate.go
Expand Up @@ -3,6 +3,7 @@

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

package iam
12 changes: 12 additions & 0 deletions internal/service/iam/instance_profile.go
Expand Up @@ -33,6 +33,7 @@ const (

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

return output.InstanceProfile, nil
}

func instanceProfileTags(ctx context.Context, conn *iam.IAM, identifier string) ([]*iam.Tag, error) {
output, err := conn.ListInstanceProfileTagsWithContext(ctx, &iam.ListInstanceProfileTagsInput{
InstanceProfileName: aws.String(identifier),
})
if err != nil {
return nil, err
}

return output.Tags, nil
}

0 comments on commit 898c9b5

Please sign in to comment.