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 the ability to manage tags on ASGs #1076

Closed
wants to merge 3 commits into from
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
78 changes: 77 additions & 1 deletion builtin/providers/aws/resource_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"bytes"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -118,10 +119,73 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
return hashcode.String(v.(string))
},
},
"tags": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},

"value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},

"propagate": &schema.Schema{
Type: schema.TypeBool,
Required: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ASG API reference lists propagate as optional, is there a reason to mark this as required here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure what default people would like (I assume not propagated), so I made it mandatory so people had to choose.

I'd be happy to make it optional / default to false if that's what people think is best.

},
},
},
Set: resourceAwsAutoScalingGroupTagsHash,
},
},
}
}

func resourceAwsAutoScalingGroupTagsHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["value"].(string)))
if m["propagate"].(bool) {
buf.WriteString("true")
} else {
buf.WriteString("false")
}

return hashcode.String(buf.String())
}

func resourceAwsAutoscalingGroupTags(d *schema.ResourceData, doResources bool) []autoscaling.Tag {
if v, ok := d.GetOk("tags"); ok {
var f []interface{}
f = v.(*schema.Set).List()
tags := make([]autoscaling.Tag, 0, len(f))
for _, t := range f {
data := t.(map[string]interface{})
tag := autoscaling.Tag{
Key: aws.String(data["name"].(string)),
Value: aws.String(data["value"].(string)),
PropagateAtLaunch: aws.False(),
}
if doResources {
tag.ResourceType = aws.String("auto-scaling-group")
tag.ResourceID = aws.String(d.Id())
}
if data["propagate"].(bool) {
tag.PropagateAtLaunch = aws.True()
}
tags = append(tags, tag)
}
return tags
}
return []autoscaling.Tag{}
}

func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) error {
autoscalingconn := meta.(*AWSClient).autoscalingconn

Expand Down Expand Up @@ -163,6 +227,7 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
autoScalingGroupOpts.TerminationPolicies = expandStringList(
v.(*schema.Set).List())
}
autoScalingGroupOpts.Tags = resourceAwsAutoscalingGroupTags(d, false)

log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", autoScalingGroupOpts)
err := autoscalingconn.CreateAutoScalingGroup(&autoScalingGroupOpts)
Expand Down Expand Up @@ -197,7 +262,7 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e
d.Set("name", *g.AutoScalingGroupName)
d.Set("vpc_zone_identifier", strings.Split(*g.VPCZoneIdentifier, ","))
d.Set("termination_policies", g.TerminationPolicies)

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

Expand Down Expand Up @@ -231,6 +296,17 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error updating Autoscaling group: %s", err)
}

if d.HasChange("tags") {
log.Printf("[DEBUG] AutoScaling Group update tags")
var update autoscaling.CreateOrUpdateTagsType
update.Tags = resourceAwsAutoscalingGroupTags(d, true)
err = autoscalingconn.CreateOrUpdateTags(&update)
if err != nil {
d.Partial(true)
return fmt.Errorf("Error updating Autoscaling group tags: %s", err)
}
}

return resourceAwsAutoscalingGroupRead(d, meta)
}

Expand Down
22 changes: 22 additions & 0 deletions builtin/providers/aws/resource_aws_autoscaling_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
"aws_autoscaling_group.bar", "force_delete", "true"),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "termination_policies.912102603", "OldestInstance"),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "tags.3469205768.name", "sometag"),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "tags.3469205768.value", "bar"),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "tags.3469205768.propagate", "1"),
),
},

Expand All @@ -52,6 +58,10 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.new", &lc),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "desired_capacity", "5"),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "tags.3469205768.value", "quux"),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "tags.3469205768.propagate", "0"),
testLaunchConfigurationName("aws_autoscaling_group.bar", &lc),
),
},
Expand Down Expand Up @@ -226,6 +236,12 @@ resource "aws_autoscaling_group" "bar" {
termination_policies = ["OldestInstance"]

launch_configuration = "${aws_launch_configuration.foobar.name}"

tags {
name = "sometag"
value = "bar"
propagate = true
}
}
`

Expand Down Expand Up @@ -253,6 +269,12 @@ resource "aws_autoscaling_group" "bar" {
force_delete = true

launch_configuration = "${aws_launch_configuration.new.name}"

tags {
name = "sometag"
value = "quux"
propagate = false
}
}
`

Expand Down