diff --git a/aws/resource_aws_sfn_activity.go b/aws/resource_aws_sfn_activity.go index 7f01931d292..3eb5afb5a2d 100644 --- a/aws/resource_aws_sfn_activity.go +++ b/aws/resource_aws_sfn_activity.go @@ -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 { @@ -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) @@ -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) } } @@ -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) } diff --git a/aws/resource_aws_sfn_state_machine.go b/aws/resource_aws_sfn_state_machine.go index 1d735651982..bbeb5ed6e34 100644 --- a/aws/resource_aws_sfn_state_machine.go +++ b/aws/resource_aws_sfn_state_machine.go @@ -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 { @@ -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 @@ -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) } @@ -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) } diff --git a/aws/tagsSfn.go b/aws/tagsSfn.go deleted file mode 100644 index 093fbc78fa4..00000000000 --- a/aws/tagsSfn.go +++ /dev/null @@ -1,78 +0,0 @@ -package aws - -import ( - "log" - "regexp" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/sfn" -) - -// diffTags takes our tags locally and the ones remotely and returns -// the set of tags that must be created, and the set of tags that must -// be destroyed. -func diffTagsSfn(oldTags, newTags []*sfn.Tag) ([]*sfn.Tag, []*sfn.Tag) { - // First, we're creating everything we have - create := make(map[string]interface{}) - for _, t := range newTags { - create[aws.StringValue(t.Key)] = aws.StringValue(t.Value) - } - - // Build the list of what to remove - var remove []*sfn.Tag - for _, t := range oldTags { - old, ok := create[aws.StringValue(t.Key)] - if !ok || old != aws.StringValue(t.Value) { - // Delete it! - remove = append(remove, t) - } else if ok { - // already present so remove from new - delete(create, aws.StringValue(t.Key)) - } - } - - return tagsFromMapSfn(create), remove -} - -// tagsFromMap returns the tags for the given map of data. -func tagsFromMapSfn(tagMap map[string]interface{}) []*sfn.Tag { - tags := make([]*sfn.Tag, 0, len(tagMap)) - for tagKey, tagValueRaw := range tagMap { - tag := &sfn.Tag{ - Key: aws.String(tagKey), - Value: aws.String(tagValueRaw.(string)), - } - if !tagIgnoredSfn(tag) { - tags = append(tags, tag) - } - } - - return tags -} - -// tagsToMap turns the list of tags into a map. -func tagsToMapSfn(tags []*sfn.Tag) map[string]string { - tagMap := make(map[string]string) - for _, tag := range tags { - if !tagIgnoredSfn(tag) { - tagMap[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value) - } - } - - return tagMap -} - -// compare a tag against a list of strings and checks if it should -// be ignored or not -func tagIgnoredSfn(t *sfn.Tag) bool { - filter := []string{"^aws:"} - for _, v := range filter { - log.Printf("[DEBUG] Matching %v with %v\n", v, aws.StringValue(t.Key)) - r, _ := regexp.MatchString(v, aws.StringValue(t.Key)) - if r { - log.Printf("[DEBUG] Found AWS specific tag %s (val: %s), ignoring.\n", aws.StringValue(t.Key), aws.StringValue(t.Value)) - return true - } - } - return false -} diff --git a/aws/tagsSfn_test.go b/aws/tagsSfn_test.go deleted file mode 100644 index cdb727ef7ec..00000000000 --- a/aws/tagsSfn_test.go +++ /dev/null @@ -1,110 +0,0 @@ -package aws - -import ( - "reflect" - "testing" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/sfn" -) - -func TestDiffSfnTags(t *testing.T) { - cases := []struct { - Old, New map[string]interface{} - Create, Remove map[string]string - }{ - // Add - { - Old: map[string]interface{}{ - "foo": "bar", - }, - New: map[string]interface{}{ - "foo": "bar", - "bar": "baz", - }, - Create: map[string]string{ - "bar": "baz", - }, - Remove: map[string]string{}, - }, - - // Modify - { - Old: map[string]interface{}{ - "foo": "bar", - }, - New: map[string]interface{}{ - "foo": "baz", - }, - Create: map[string]string{ - "foo": "baz", - }, - Remove: map[string]string{ - "foo": "bar", - }, - }, - - // Overlap - { - Old: map[string]interface{}{ - "foo": "bar", - "hello": "world", - }, - New: map[string]interface{}{ - "foo": "baz", - "hello": "world", - }, - Create: map[string]string{ - "foo": "baz", - }, - Remove: map[string]string{ - "foo": "bar", - }, - }, - - // Remove - { - Old: map[string]interface{}{ - "foo": "bar", - "bar": "baz", - }, - New: map[string]interface{}{ - "foo": "bar", - }, - Create: map[string]string{}, - Remove: map[string]string{ - "bar": "baz", - }, - }, - } - - for i, tc := range cases { - c, r := diffTagsSfn(tagsFromMapSfn(tc.Old), tagsFromMapSfn(tc.New)) - cm := tagsToMapSfn(c) - rm := tagsToMapSfn(r) - if !reflect.DeepEqual(cm, tc.Create) { - t.Fatalf("%d: bad create: %#v", i, cm) - } - if !reflect.DeepEqual(rm, tc.Remove) { - t.Fatalf("%d: bad remove: %#v", i, rm) - } - } -} - -func TestIgnoringTagsSfn(t *testing.T) { - ignoredTags := []*sfn.Tag{ - { - Key: aws.String("aws:cloudformation:logical-id"), - Value: aws.String("foo"), - }, - { - Key: aws.String("aws:foo:bar"), - Value: aws.String("baz"), - }, - } - for _, tag := range ignoredTags { - if !tagIgnoredSfn(tag) { - t.Fatalf("Tag %v with value %v not ignored, but should be!", *tag.Key, *tag.Value) - } - } -}