diff --git a/aws/data_source_aws_ram_resource_share.go b/aws/data_source_aws_ram_resource_share.go index dea0e4e57c59..67dd963b6b90 100644 --- a/aws/data_source_aws_ram_resource_share.go +++ b/aws/data_source_aws_ram_resource_share.go @@ -7,6 +7,7 @@ import ( "github.com/aws/aws-sdk-go/service/ram" "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 dataSourceAwsRamResourceShare() *schema.Resource { @@ -108,7 +109,7 @@ func dataSourceAwsRamResourceShareRead(d *schema.ResourceData, meta interface{}) d.Set("owning_account_id", aws.StringValue(r.OwningAccountId)) d.Set("status", aws.StringValue(r.Status)) - if err := d.Set("tags", tagsToMapRAM(r.Tags)); err != nil { + if err := d.Set("tags", keyvaluetags.RamKeyValueTags(r.Tags).IgnoreAws().Map()); err != nil { return fmt.Errorf("error setting tags: %s", err) } diff --git a/aws/resource_aws_ram_resource_share.go b/aws/resource_aws_ram_resource_share.go index 192020487588..451b18cc287a 100644 --- a/aws/resource_aws_ram_resource_share.go +++ b/aws/resource_aws_ram_resource_share.go @@ -7,9 +7,9 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ram" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) func resourceAwsRamResourceShare() *schema.Resource { @@ -59,8 +59,7 @@ func resourceAwsRamResourceShareCreate(d *schema.ResourceData, meta interface{}) } if v, ok := d.GetOk("tags"); ok { - tags := tagsFromMapRAM(v.(map[string]interface{})) - request.Tags = tags + request.Tags = keyvaluetags.New(v.(map[string]interface{})).IgnoreAws().RamTags() } log.Println("[DEBUG] Create RAM resource share request:", request) @@ -122,7 +121,7 @@ func resourceAwsRamResourceShareRead(d *schema.ResourceData, meta interface{}) e d.Set("name", resourceShare.Name) d.Set("allow_external_principals", resourceShare.AllowExternalPrincipals) - if err := d.Set("tags", tagsToMapRAM(resourceShare.Tags)); err != nil { + if err := d.Set("tags", keyvaluetags.RamKeyValueTags(resourceShare.Tags).IgnoreAws().Map()); err != nil { return fmt.Errorf("Error setting tags: %s", err) } @@ -157,31 +156,10 @@ func resourceAwsRamResourceShareUpdate(d *schema.ResourceData, meta interface{}) } if d.HasChange("tags") { - // Reset all tags to empty set - oraw, nraw := d.GetChange("tags") - o := oraw.(map[string]interface{}) - n := nraw.(map[string]interface{}) - c, r := diffTagsRAM(tagsFromMapRAM(o), tagsFromMapRAM(n)) - - if len(r) > 0 { - _, err := conn.UntagResource(&ram.UntagResourceInput{ - ResourceShareArn: aws.String(d.Id()), - TagKeys: tagKeysRam(r), - }) - if err != nil { - return fmt.Errorf("Error deleting RAM resource share tags: %s", err) - } - } - - if len(c) > 0 { - input := &ram.TagResourceInput{ - ResourceShareArn: aws.String(d.Id()), - Tags: c, - } - _, err := conn.TagResource(input) - if err != nil { - return fmt.Errorf("Error updating RAM resource share tags: %s", err) - } + o, n := d.GetChange("tags") + + if err := keyvaluetags.RamUpdateTags(conn, d.Id(), o, n); err != nil { + return fmt.Errorf("error updating RAM Resource Share (%s) tags: %s", d.Id(), err) } d.SetPartial("tags") diff --git a/aws/tagsRAM.go b/aws/tagsRAM.go deleted file mode 100644 index f9fb6bd90fb9..000000000000 --- a/aws/tagsRAM.go +++ /dev/null @@ -1,85 +0,0 @@ -package aws - -import ( - "log" - "regexp" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ram" -) - -// 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 diffTagsRAM(oldTags, newTags []*ram.Tag) ([]*ram.Tag, []*ram.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 []*ram.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 { - delete(create, aws.StringValue(t.Key)) - } - } - - return tagsFromMapRAM(create), remove -} - -// tagsFromMapRAM returns the tags for the given map of data for RAM. -func tagsFromMapRAM(m map[string]interface{}) []*ram.Tag { - result := make([]*ram.Tag, 0, len(m)) - for k, v := range m { - t := &ram.Tag{ - Key: aws.String(k), - Value: aws.String(v.(string)), - } - if !tagIgnoredRAM(t) { - result = append(result, t) - } - } - - return result -} - -// tagsToMapRAM turns the list of RAM tags into a map. -func tagsToMapRAM(ts []*ram.Tag) map[string]string { - result := make(map[string]string) - for _, t := range ts { - if !tagIgnoredRAM(t) { - result[aws.StringValue(t.Key)] = aws.StringValue(t.Value) - } - } - - return result -} - -// compare a tag against a list of strings and checks if it should -// be ignored or not -func tagIgnoredRAM(t *ram.Tag) bool { - filter := []string{"^aws:"} - for _, v := range filter { - log.Printf("[DEBUG] Matching %v with %v\n", v, *t.Key) - if r, _ := regexp.MatchString(v, *t.Key); r { - log.Printf("[DEBUG] Found AWS specific tag %s (val: %s), ignoring.\n", *t.Key, *t.Value) - return true - } - } - return false -} - -// tagKeysRam returns the keys for the list of RAM tags -func tagKeysRam(ts []*ram.Tag) []*string { - result := make([]*string, 0, len(ts)) - for _, t := range ts { - result = append(result, t.Key) - } - return result -} diff --git a/aws/tagsRAM_test.go b/aws/tagsRAM_test.go deleted file mode 100644 index a18f91ee3a69..000000000000 --- a/aws/tagsRAM_test.go +++ /dev/null @@ -1,111 +0,0 @@ -package aws - -import ( - "reflect" - "testing" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ram" -) - -// go test -v -run="TestDiffRAMTags" -func TestDiffRAMTags(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 := diffTagsRAM(tagsFromMapRAM(tc.Old), tagsFromMapRAM(tc.New)) - cm := tagsToMapRAM(c) - rm := tagsToMapRAM(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) - } - } -} - -// go test -v -run="TestIgnoringTagsRAM" -func TestIgnoringTagsRAM(t *testing.T) { - var ignoredTags []*ram.Tag - ignoredTags = append(ignoredTags, &ram.Tag{ - Key: aws.String("aws:cloudformation:logical-id"), - Value: aws.String("foo"), - }) - ignoredTags = append(ignoredTags, &ram.Tag{ - Key: aws.String("aws:foo:bar"), - Value: aws.String("baz"), - }) - for _, tag := range ignoredTags { - if !tagIgnoredRAM(tag) { - t.Fatalf("Tag %v with value %v not ignored, but should be!", *tag.Key, *tag.Value) - } - } -}