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

Add tagging to aws_resourcegroup_group. #7774

Closed
Closed
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
82 changes: 78 additions & 4 deletions aws/resource_aws_resourcegroups_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand All @@ -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)
}
Expand All @@ -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()),
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 6 additions & 0 deletions aws/resource_aws_resourcegroups_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
{
Expand Down Expand Up @@ -135,6 +137,10 @@ resource "aws_resourcegroups_group" "test" {
%s
JSON
}

tags {
Foo = "bar"
}
}
`, rName, desc, query)
}
3 changes: 2 additions & 1 deletion website/docs/r/resourcegroups_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down