diff --git a/aws/resource_aws_resourcegroups_group.go b/aws/resource_aws_resourcegroups_group.go index ac3138ed4de..113d29f84d1 100644 --- a/aws/resource_aws_resourcegroups_group.go +++ b/aws/resource_aws_resourcegroups_group.go @@ -61,10 +61,30 @@ func resourceAwsResourceGroupsGroup() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "tags": tagsSchema(), }, } } +func diffTagsResourceGroups(oldTags map[string]interface{}, newTags map[string]interface{}) (map[string]*string, []*string) { + create := make(map[string]*string) + for k, v := range newTags { + x := v.(string) + create[k] = &x + } + + var remove []*string + for k, v := range oldTags { + if _, ok := create[k]; !ok { + r := v.(string) + remove = append(remove, &r) + } + } + + return create, remove +} + func extractResourceGroupResourceQuery(resourceQueryList []interface{}) *resourcegroups.ResourceQuery { resourceQuery := resourceQueryList[0].(map[string]interface{}) @@ -77,18 +97,31 @@ func extractResourceGroupResourceQuery(resourceQueryList []interface{}) *resourc func resourceAwsResourceGroupsGroupCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).resourcegroupsconn + name := d.Get("name").(string) input := resourcegroups.CreateGroupInput{ Description: aws.String(d.Get("description").(string)), - Name: aws.String(d.Get("name").(string)), + Name: aws.String(name), ResourceQuery: extractResourceGroupResourceQuery(d.Get("resource_query").([]interface{})), } - res, err := conn.CreateGroup(&input) + if t, ok := d.GetOk("tags"); ok { + tags := make(map[string]*string) + + for k, v := range t.(map[string]interface{}) { + tags[k] = aws.String(v.(string)) + } + + if len(tags) > 0 { + input.Tags = tags + } + } + + _, err := conn.CreateGroup(&input) if err != nil { return fmt.Errorf("error creating resource group: %s", err) } - d.SetId(aws.StringValue(res.Group.Name)) + d.SetId(name) return resourceAwsResourceGroupsGroupRead(d, meta) } @@ -110,9 +143,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()), @@ -129,6 +163,22 @@ func resourceAwsResourceGroupsGroupRead(d *schema.ResourceData, meta interface{} return fmt.Errorf("error setting resource_query: %s", err) } + t, err := conn.GetTags(&resourcegroups.GetTagsInput{ + Arn: aws.String(arn), + }) + + if err != nil { + return fmt.Errorf("error reading tags for resource group (%s): %s", d.Id(), err) + } + + tags := make(map[string]*string) + for k, v := range t.Tags { + x := *v + tags[k] = aws.String(x) + } + + d.Set("tags", tags) + return nil } @@ -159,6 +209,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: aws.String(arn.(string)), + 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: aws.String(arn.(string)), + Tags: create, + }) + + if err != nil { + return fmt.Errorf("error updating tags for resource group (%s): %s", d.Id(), err) + } + } + return resourceAwsResourceGroupsGroupRead(d, meta) } diff --git a/aws/resource_aws_resourcegroups_group_test.go b/aws/resource_aws_resourcegroups_group_test.go index a15f1f0a1f7..87da09e9793 100644 --- a/aws/resource_aws_resourcegroups_group_test.go +++ b/aws/resource_aws_resourcegroups_group_test.go @@ -57,6 +57,8 @@ func TestAccAWSResourceGroup_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "description", desc1), resource.TestCheckResourceAttr(resourceName, "resource_query.0.query", query1+"\n"), resource.TestCheckResourceAttrSet(resourceName, "arn"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), + resource.TestCheckResourceAttr(resourceName, "tags.Foo", "bar"), ), }, { @@ -135,6 +137,10 @@ resource "aws_resourcegroups_group" "test" { %s JSON } + + tags { + Foo = "bar" + } } `, rName, desc, query) } 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: