Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tech debt: Reduce tags boilerplate code - Plugin SDK resources serverlessrepo (Phase 3c) #30642

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 17 additions & 29 deletions internal/service/serverlessrepo/cloudformation_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ( // nosemgrep:ci.aws-sdk-go-multiple-service-imports
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)

const (
Expand All @@ -31,7 +32,8 @@ const (
cloudFormationStackTagSemanticVersion = "serverlessrepo:semanticVersion"
)

// @SDKResource("aws_serverlessapplicationrepository_cloudformation_stack")
// @SDKResource("aws_serverlessapplicationrepository_cloudformation_stack", name="CloudFormation Stack")
// @Tags
func ResourceCloudFormationStack() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceCloudFormationStackCreate,
Expand All @@ -50,11 +52,6 @@ func ResourceCloudFormationStack() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"application_id": {
Type: schema.TypeString,
Required: true,
Expand All @@ -68,7 +65,16 @@ func ResourceCloudFormationStack() *schema.Resource {
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(serverlessrepo.Capability_Values(), false),
},
Set: schema.HashString,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"outputs": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"parameters": {
Type: schema.TypeMap,
Expand All @@ -81,13 +87,8 @@ func ResourceCloudFormationStack() *schema.Resource {
Optional: true,
Computed: true,
},
"outputs": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
Expand Down Expand Up @@ -132,8 +133,6 @@ func resourceCloudFormationStackRead(ctx context.Context, d *schema.ResourceData
var diags diag.Diagnostics
serverlessConn := meta.(*conns.AWSClient).ServerlessRepoConn()
cfConn := meta.(*conns.AWSClient).CloudFormationConn()
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

stack, err := tfcloudformation.FindStackByID(ctx, cfConn, d.Id())

Expand Down Expand Up @@ -166,16 +165,7 @@ func resourceCloudFormationStackRead(ctx context.Context, d *schema.ResourceData
return sdkdiag.AppendErrorf(diags, "describing Serverless Application Repository CloudFormation Stack (%s): missing required tag \"%s\"", d.Id(), cloudFormationStackTagSemanticVersion)
}

tags = tags.IgnoreServerlessApplicationRepository().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err = d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return sdkdiag.AppendErrorf(diags, "to set tags: %s", err)
}

if err := d.Set("tags_all", tags.Map()); err != nil {
return sdkdiag.AppendErrorf(diags, "to set tags_all: %s", err)
}
SetTagsOut(ctx, Tags(tags))

if err = d.Set("outputs", flattenCloudFormationOutputs(stack.Outputs)); err != nil {
return sdkdiag.AppendErrorf(diags, "to set outputs: %s", err)
Expand Down Expand Up @@ -305,15 +295,13 @@ func resourceCloudFormationStackImport(ctx context.Context, d *schema.ResourceDa
func createCloudFormationChangeSet(ctx context.Context, d *schema.ResourceData, client *conns.AWSClient) (*cloudformation.DescribeChangeSetOutput, error) {
serverlessConn := client.ServerlessRepoConn()
cfConn := client.CloudFormationConn()
defaultTagsConfig := client.DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(ctx, d.Get("tags").(map[string]interface{})))

stackName := d.Get("name").(string)
changeSetRequest := serverlessrepo.CreateCloudFormationChangeSetRequest{
StackName: aws.String(stackName),
ApplicationId: aws.String(d.Get("application_id").(string)),
Capabilities: flex.ExpandStringSet(d.Get("capabilities").(*schema.Set)),
Tags: Tags(tags.IgnoreServerlessApplicationRepository()),
Tags: GetTagsIn(ctx),
}
if v, ok := d.GetOk("semantic_version"); ok {
changeSetRequest.SemanticVersion = aws.String(v.(string))
Expand Down
2 changes: 2 additions & 0 deletions internal/service/serverlessrepo/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.