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

service/sfn: Refactor tagging logic to keyvaluetags package #10744

Merged
merged 1 commit into from
Nov 15, 2019
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
48 changes: 8 additions & 40 deletions aws/resource_aws_sfn_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsSfnActivity() *schema.Resource {
Expand Down Expand Up @@ -46,7 +47,7 @@ func resourceAwsSfnActivityCreate(d *schema.ResourceData, meta interface{}) erro

params := &sfn.CreateActivityInput{
Name: aws.String(d.Get("name").(string)),
Tags: tagsFromMapSfn(d.Get("tags").(map[string]interface{})),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().SfnTags(),
}

activity, err := conn.CreateActivity(params)
Expand All @@ -63,38 +64,9 @@ func resourceAwsSfnActivityUpdate(d *schema.ResourceData, meta interface{}) erro
conn := meta.(*AWSClient).sfnconn

if d.HasChange("tags") {
oldTagsRaw, newTagsRaw := d.GetChange("tags")
oldTagsMap := oldTagsRaw.(map[string]interface{})
newTagsMap := newTagsRaw.(map[string]interface{})
createTags, removeTags := diffTagsSfn(tagsFromMapSfn(oldTagsMap), tagsFromMapSfn(newTagsMap))

if len(removeTags) > 0 {
removeTagKeys := make([]*string, len(removeTags))
for i, removeTag := range removeTags {
removeTagKeys[i] = removeTag.Key
}

input := &sfn.UntagResourceInput{
ResourceArn: aws.String(d.Id()),
TagKeys: removeTagKeys,
}

log.Printf("[DEBUG] Untagging State Function Activity: %s", input)
if _, err := conn.UntagResource(input); err != nil {
return fmt.Errorf("error untagging State Function Activity (%s): %s", d.Id(), err)
}
}

if len(createTags) > 0 {
input := &sfn.TagResourceInput{
ResourceArn: aws.String(d.Id()),
Tags: createTags,
}

log.Printf("[DEBUG] Tagging State Function Activity: %s", input)
if _, err := conn.TagResource(input); err != nil {
return fmt.Errorf("error tagging State Function Activity (%s): %s", d.Id(), err)
}
o, n := d.GetChange("tags")
if err := keyvaluetags.SfnUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}

Expand Down Expand Up @@ -122,17 +94,13 @@ func resourceAwsSfnActivityRead(d *schema.ResourceData, meta interface{}) error
log.Printf("[DEBUG] Error setting creation_date: %s", err)
}

tagsResp, err := conn.ListTagsForResource(
&sfn.ListTagsForResourceInput{
ResourceArn: aws.String(d.Id()),
},
)
tags, err := keyvaluetags.SfnListTags(conn, d.Id())

if err != nil {
return fmt.Errorf("error listing SFN Activity (%s) tags: %s", d.Id(), err)
return fmt.Errorf("error listing tags for SFN Activity (%s): %s", d.Id(), err)
}

if err := d.Set("tags", tagsToMapSfn(tagsResp.Tags)); err != nil {
if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

Expand Down
55 changes: 9 additions & 46 deletions aws/resource_aws_sfn_state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsSfnStateMachine() *schema.Resource {
Expand Down Expand Up @@ -65,7 +66,7 @@ func resourceAwsSfnStateMachineCreate(d *schema.ResourceData, meta interface{})
Definition: aws.String(d.Get("definition").(string)),
Name: aws.String(d.Get("name").(string)),
RoleArn: aws.String(d.Get("role_arn").(string)),
Tags: tagsFromMapSfn(d.Get("tags").(map[string]interface{})),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().SfnTags(),
}

var activity *sfn.CreateStateMachineOutput
Expand Down Expand Up @@ -127,23 +128,13 @@ func resourceAwsSfnStateMachineRead(d *schema.ResourceData, meta interface{}) er
log.Printf("[DEBUG] Error setting creation_date: %s", err)
}

tags := map[string]string{}

tagsResp, err := conn.ListTagsForResource(
&sfn.ListTagsForResourceInput{
ResourceArn: aws.String(d.Id()),
},
)
tags, err := keyvaluetags.SfnListTags(conn, d.Id())

if err != nil && !isAWSErr(err, "UnknownOperationException", "") {
return fmt.Errorf("error listing SFN Activity (%s) tags: %s", d.Id(), err)
}

if tagsResp != nil {
tags = tagsToMapSfn(tagsResp.Tags)
return fmt.Errorf("error listing tags for SFN State Machine (%s): %s", d.Id(), err)
}

if err := d.Set("tags", tags); err != nil {
if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

Expand Down Expand Up @@ -171,40 +162,12 @@ func resourceAwsSfnStateMachineUpdate(d *schema.ResourceData, meta interface{})
}

if d.HasChange("tags") {
oldTagsRaw, newTagsRaw := d.GetChange("tags")
oldTagsMap := oldTagsRaw.(map[string]interface{})
newTagsMap := newTagsRaw.(map[string]interface{})
createTags, removeTags := diffTagsSfn(tagsFromMapSfn(oldTagsMap), tagsFromMapSfn(newTagsMap))

if len(removeTags) > 0 {
removeTagKeys := make([]*string, len(removeTags))
for i, removeTag := range removeTags {
removeTagKeys[i] = removeTag.Key
}

input := &sfn.UntagResourceInput{
ResourceArn: aws.String(d.Id()),
TagKeys: removeTagKeys,
}

log.Printf("[DEBUG] Untagging State Function: %s", input)
if _, err := conn.UntagResource(input); err != nil {
return fmt.Errorf("error untagging State Function (%s): %s", d.Id(), err)
}
}

if len(createTags) > 0 {
input := &sfn.TagResourceInput{
ResourceArn: aws.String(d.Id()),
Tags: createTags,
}

log.Printf("[DEBUG] Tagging State Function: %s", input)
if _, err := conn.TagResource(input); err != nil {
return fmt.Errorf("error tagging State Function (%s): %s", d.Id(), err)
}
o, n := d.GetChange("tags")
if err := keyvaluetags.SfnUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}

return resourceAwsSfnStateMachineRead(d, meta)
}

Expand Down
78 changes: 0 additions & 78 deletions aws/tagsSfn.go

This file was deleted.

110 changes: 0 additions & 110 deletions aws/tagsSfn_test.go

This file was deleted.