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 ssmincidents (Phase 3c) #30639

Merged
merged 5 commits into from
Apr 11, 2023
Merged
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion internal/service/ssmincidents/generate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -TagInIDElem=ResourceArn -ListTags -ListTagsInIDElem=ResourceArn -ServiceTagsMap -UpdateTags -UntagInTagsElem=TagKeys -KVTValues -SkipTypesImp
//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -TagInIDElem=ResourceArn -ListTags -ListTagsInIDElem=ResourceArn -ServiceTagsMap -KVTValues -SkipTypesImp
// ONLY generate directives and package declaration! Do not add anything else to this file.

package ssmincidents
11 changes: 10 additions & 1 deletion internal/service/ssmincidents/replication_set.go
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ const (
)

// @SDKResource("aws_ssmincidents_replication_set", name="Replication Set")
// @Tags(identifierAttribute="arn")
// @Tags(identifierAttribute="id")
func ResourceReplicationSet() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceReplicationSetCreate,
@@ -186,6 +186,15 @@ func resourceReplicationSetUpdate(ctx context.Context, d *schema.ResourceData, m
}
}

// tags_all does not detect changes when tag value is "" while this change is detected by tags
if d.HasChanges("tags_all", "tags") {
log.Printf("[DEBUG] Updating SSMIncidents ReplicationSet tags")

if err := updateResourceTags(ctx, client, d); err != nil {
return create.DiagError(names.SSMIncidents, create.ErrActionUpdating, ResNameReplicationSet, d.Id(), err)
}
}

return resourceReplicationSetRead(ctx, d, meta)
}

2 changes: 1 addition & 1 deletion internal/service/ssmincidents/service_package_gen.go

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

80 changes: 80 additions & 0 deletions internal/service/ssmincidents/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ssmincidents

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ssmincidents"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

// makes api calls to update Resource Data Tags
func updateResourceTags(context context.Context, client *ssmincidents.Client, d *schema.ResourceData) error {
old, new := d.GetChange("tags_all")

oldTags := tftags.New(context, old)
newTags := tftags.New(context, new)

allNewTagsMap := flex.ExpandStringValueMap(new.(map[string]interface{}))

if err := updateResourceTag(context, client, d.Id(), oldTags.Removed(newTags), oldTags.Updated(newTags)); err != nil {
return err
}

// provider level tags cannot have "" as value
// resource level tags can have "" as value but this change is not recorded by d.GetChange("tags_all")
// so we have to look specifically for any tags updated with "" as the value

old, new = d.GetChange("tags")

oldTags = tftags.New(context, old)
newTags = tftags.New(context, new)

toUpdate := make(map[string]string)

for k, v := range oldTags.Updated(newTags).Map() {
if v == "" {
toUpdate[k] = v
allNewTagsMap[k] = v
}
}

// since we are adding an extra tag to tags_all not initially detected by terraform
// we must set tags_all to what is properly expected in create/update function so that
// terraform plan is consistent to what we receive with terraform refresh/the update function
d.Set("tags_all", allNewTagsMap)

empty := tftags.KeyValueTags{}
if err := updateResourceTag(context, client, d.Id(), empty, tftags.New(context, toUpdate)); err != nil {
return err
}

return nil
}

func updateResourceTag(context context.Context, client *ssmincidents.Client, arn string, removedTags, addedTags tftags.KeyValueTags) error {
if len(removedTags) > 0 {
input := &ssmincidents.UntagResourceInput{
ResourceArn: aws.String(arn),
TagKeys: removedTags.Keys(),
}
if _, err := client.UntagResource(context, input); err != nil {
return err
}
}

if len(addedTags) > 0 {
input := &ssmincidents.TagResourceInput{
ResourceArn: aws.String(arn),
Tags: addedTags.IgnoreAWS().Map(),
}

if _, err := client.TagResource(context, input); err != nil {
return err
}
}

return nil
}
44 changes: 0 additions & 44 deletions internal/service/ssmincidents/tags_gen.go

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