Skip to content

Commit

Permalink
Add tag for resource groups
Browse files Browse the repository at this point in the history
  • Loading branch information
vnguyen7 committed Mar 6, 2019
1 parent ca888e3 commit 63ae3a7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
77 changes: 76 additions & 1 deletion aws/resource_aws_resourcegroups_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func resourceAwsResourceGroupsGroup() *schema.Resource {
},
},
},

"tags": tagsSchema(),
"arn": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -83,6 +83,10 @@ func resourceAwsResourceGroupsGroupCreate(d *schema.ResourceData, meta interface
ResourceQuery: extractResourceGroupResourceQuery(d.Get("resource_query").([]interface{})),
}

if v, ok := d.GetOk("tags"); ok {
input.Tags = tagsToMapGenericRG(v.(map[string]*string))
}

res, err := conn.CreateGroup(&input)
if err != nil {
return fmt.Errorf("error creating resource group: %s", err)
Expand Down Expand Up @@ -114,6 +118,8 @@ func resourceAwsResourceGroupsGroupRead(d *schema.ResourceData, meta interface{}
d.Set("description", aws.StringValue(g.Group.Description))
d.Set("arn", aws.StringValue(g.Group.GroupArn))

arn := aws.StringValue(g.Group.GroupArn)

q, err := conn.GetGroupQuery(&resourcegroups.GetGroupQueryInput{
GroupName: aws.String(d.Id()),
})
Expand All @@ -129,6 +135,17 @@ func resourceAwsResourceGroupsGroupRead(d *schema.ResourceData, meta interface{}
return fmt.Errorf("error setting resource_query: %s", err)
}

v, err1 := conn.GetTags(&resourcegroups.GetTagsInput{
Arn: aws.String(arn),
})

if err1 != nil {
return fmt.Errorf("error getting resource tags :%s", err1)
}

tags := tagsToMapGenericRG(v.Tags)

d.Set("tags", tags)
return nil
}

Expand Down Expand Up @@ -159,6 +176,37 @@ func resourceAwsResourceGroupsGroupUpdate(d *schema.ResourceData, meta interface
}
}

if d.HasChange("tags") {
arn := d.Get("arn")
oraw, nraw := d.GetChange("tags")
o := oraw.(map[string]interface{})
n := nraw.(map[string]interface{})
create, remove := diffTagsGenericRG(o, n)

// Set tags
if len(remove) > 0 {
log.Printf("[DEBUG] Removing tags: %#v", remove)
_, err := conn.Untag(&resourcegroups.UntagInput{
Arn: aws.String(arn.(string)),
Keys: remove,
})
if err != nil {
return fmt.Errorf("error setting resource groups id (%s): %s", d.Id(), err)
}
}
if len(create) > 0 {
log.Printf("[DEBUG] Creating tags: %#v", create)

_, err := conn.Tag(&resourcegroups.TagInput{
Arn: aws.String(arn.(string)),
Tags: create,
})
if err != nil {
return fmt.Errorf("error setting resource groups id (%s): %s", d.Id(), err)
}
}
}

return resourceAwsResourceGroupsGroupRead(d, meta)
}

Expand All @@ -176,3 +224,30 @@ func resourceAwsResourceGroupsGroupDelete(d *schema.ResourceData, meta interface

return nil
}

func tagsToMapGenericRG(ts map[string]*string) map[string]*string {
result := make(map[string]*string)
for k, v := range ts {
if !tagIgnoredGeneric(k) {
result[k] = v
}
}

return result
}

func diffTagsGenericRG(oldTags, newTags map[string]interface{}) (map[string]*string, []*string) {
// First, we're creating everything we have
create := make(map[string]*string)
for k, v := range newTags {
create[k] = aws.String(v.(string))
}
// Remove the tags from the set
var remove = []*string{}
for _, v := range oldTags {
r := v.(string)
remove = append(remove, &r)
}

return create, remove
}
6 changes: 5 additions & 1 deletion aws/resource_aws_resourcegroups_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestAccAWSResourceGroup_basic(t *testing.T) {
]
}
`

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -57,6 +56,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 @@ -130,6 +131,9 @@ resource "aws_resourcegroups_group" "test" {
name = "%s"
description = "%s"
tags = {
Foo = "bar"
}
resource_query {
query = <<JSON
%s
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/resourcegroups_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ An `resource_query` block supports the following arguments:

* `query` - (Required) The resource query as a JSON string.
* `type` - (Required) The type of the resource query. Defaults to `TAG_FILTERS_1_0`.

* `tags` - (Optional) Tag for the group itself, not to support tagging other resources via the group
## Attributes Reference

In addition to all arguments above, the following attributes are exported:
Expand Down

0 comments on commit 63ae3a7

Please sign in to comment.