From 7660da125e371865024dae2dabd4d7b3a93b915a Mon Sep 17 00:00:00 2001 From: Joe Stump Date: Wed, 27 Feb 2019 13:40:16 -0800 Subject: [PATCH 1/2] Add tag delta changes. --- aws/resource_aws_resourcegroups_group.go | 64 +++++++++++++++++++++--- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/aws/resource_aws_resourcegroups_group.go b/aws/resource_aws_resourcegroups_group.go index 5422e19e465..4caca11020c 100644 --- a/aws/resource_aws_resourcegroups_group.go +++ b/aws/resource_aws_resourcegroups_group.go @@ -68,15 +68,27 @@ func resourceAwsResourceGroupsGroup() *schema.Resource { Computed: true, }, - "tags": { - Type: schema.TypeMap, - Optional: true, - ForceNew: true, - }, + "tags": tagsSchema(), }, } } +func diffTagsResourceGroups(oldTags map[string]interface{}, newTags map[string]interface{}) (map[string]*string, []*string) { + create := make(map[string]interface{}) + for k, v := range newTags { + create[k] = &v.(string) + } + + var remove []*string + for k, v := range oldTags { + if _, ok := create[k]; !ok { + remove = append(remove, &v) + } + } + + return create, remove +} + func extractResourceGroupResourceQuery(resourceQueryList []interface{}) *resourcegroups.ResourceQuery { resourceQuery := resourceQueryList[0].(map[string]interface{}) @@ -132,9 +144,10 @@ func resourceAwsResourceGroupsGroupRead(d *schema.ResourceData, meta interface{} return fmt.Errorf("error reading resource group (%s): %s", d.Id(), err) } + arn := aws.StringValue(g.Group.GroupArn) d.Set("name", aws.StringValue(g.Group.Name)) d.Set("description", aws.StringValue(g.Group.Description)) - d.Set("arn", aws.StringValue(g.Group.GroupArn)) + d.Set("arn", arn) q, err := conn.GetGroupQuery(&resourcegroups.GetGroupQueryInput{ GroupName: aws.String(d.Id()), @@ -149,6 +162,21 @@ func resourceAwsResourceGroupsGroupRead(d *schema.ResourceData, meta interface{} resultQuery["type"] = aws.StringValue(q.GroupQuery.ResourceQuery.Type) d.Set("resource_query", []map[string]interface{}{resultQuery}) + t, err := conn.GetTags(&resourcegroups.GetTagsInput{ + Arn: arn, + }) + + if err != nil { + return fmt.Errorf("error reading tags for resource group (%s): %s", d.Id(), err) + } + + var tags map[string]string + for k, v := range t { + tags[k] = aws.String(v) + } + + d.Set("tags", tags) + return nil } @@ -179,6 +207,30 @@ func resourceAwsResourceGroupsGroupUpdate(d *schema.ResourceData, meta interface } } + if d.HasChange("tags") { + arn := d.Get("arn") + old, new := d.GetChange("tags") + create, remove := diffTagsResourceGroups(old.(map[string]interface{}), new.(map[string]interface{})) + + _, err := conn.Untag(&resourcegroups.UntagInput{ + Arn: &arn, + Keys: remove, + }) + + if err != nil { + return fmt.Errorf("error removing tags for resource group (%s): %s", d.Id(), err) + } + + _, err := conn.Tag(&resourcegroups.TagInput{ + Arn: &arn, + Tags: create, + }) + + if err != nil { + return fmt.Errorf("error updating tags for resource group (%s): %s", d.Id(), err) + } + } + return resourceAwsResourceGroupsGroupRead(d, meta) } From 42bcee6c5ec4428893255570aefde1c7f90324ad Mon Sep 17 00:00:00 2001 From: Joe Stump Date: Thu, 28 Feb 2019 13:47:08 -0800 Subject: [PATCH 2/2] Update docs --- website/docs/r/resourcegroups_group.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/website/docs/r/resourcegroups_group.html.markdown b/website/docs/r/resourcegroups_group.html.markdown index 9f26c4f6fc9..2b546e99be3 100644 --- a/website/docs/r/resourcegroups_group.html.markdown +++ b/website/docs/r/resourcegroups_group.html.markdown @@ -39,8 +39,9 @@ JSON The following arguments are supported: * `name` - (Required) The resource group's name. A resource group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. -* `description` - (Optional) A description of the resource group. * `resource_query` - (Required) A `resource_query` block. Resource queries are documented below. +* `description` - (Optional) A description of the resource group. +* `tags` - (Optional) A mapping of tags to assign to the bucket. An `resource_query` block supports the following arguments: