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

provider/aws: Fix reading auto scaling group termination policies #5101

Merged
merged 3 commits into from
Feb 23, 2016
Merged
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
22 changes: 12 additions & 10 deletions builtin/providers/aws/resource_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,16 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e
d.Set("name", g.AutoScalingGroupName)
d.Set("tag", g.Tags)
d.Set("vpc_zone_identifier", strings.Split(*g.VPCZoneIdentifier, ","))
d.Set("termination_policies", g.TerminationPolicies)

// If no termination polices are explicitly configured and the upstream state
// is only using the "Default" policy, clear the state to make it consistent
// with the default AWS create API behavior.
_, ok := d.GetOk("termination_policies")
if !ok && len(g.TerminationPolicies) == 1 && *g.TerminationPolicies[0] == "Default" {
d.Set("termination_policies", []interface{}{})
} else {
d.Set("termination_policies", flattenStringList(g.TerminationPolicies))
}

return nil
}
Expand Down Expand Up @@ -312,18 +321,11 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
if d.HasChange("termination_policies") {
// If the termination policy is set to null, we need to explicitly set
// it back to "Default", or the API won't reset it for us.
// This means GetOk() will fail us on the zero check.
v := d.Get("termination_policies")
if len(v.([]interface{})) > 0 {
if v, ok := d.GetOk("termination_policies"); ok && len(v.([]interface{})) > 0 {
opts.TerminationPolicies = expandStringList(v.([]interface{}))
} else {
// Policies is a slice of string pointers, so build one.
// Maybe there's a better idiom for this?
log.Printf("[DEBUG] Explictly setting null termination policy to 'Default'")
pol := "Default"
s := make([]*string, 1, 1)
s[0] = &pol
opts.TerminationPolicies = s
opts.TerminationPolicies = aws.StringSlice([]string{"Default"})
}
}

Expand Down
95 changes: 95 additions & 0 deletions builtin/providers/aws/resource_aws_autoscaling_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,51 @@ func TestAccAWSAutoScalingGroup_autoGeneratedName(t *testing.T) {
})
}

func TestAccAWSAutoScalingGroup_terminationPolicies(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesEmpty,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "termination_policies.#", "0"),
),
},

resource.TestStep{
Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesUpdate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "termination_policies.#", "1"),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "termination_policies.0", "OldestInstance"),
),
},

resource.TestStep{
Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesExplicitDefault,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "termination_policies.#", "1"),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "termination_policies.0", "Default"),
),
},

resource.TestStep{
Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesEmpty,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "termination_policies.#", "0"),
),
},
},
})
}

func TestAccAWSAutoScalingGroup_tags(t *testing.T) {
var group autoscaling.Group

Expand Down Expand Up @@ -415,6 +460,56 @@ resource "aws_autoscaling_group" "bar" {
}
`

const testAccAWSAutoScalingGroupConfig_terminationPoliciesEmpty = `
resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11"
instance_type = "t1.micro"
}

resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-west-2a"]
max_size = 0
min_size = 0
desired_capacity = 0

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

const testAccAWSAutoScalingGroupConfig_terminationPoliciesExplicitDefault = `
resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11"
instance_type = "t1.micro"
}

resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-west-2a"]
max_size = 0
min_size = 0
desired_capacity = 0
termination_policies = ["Default"]

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

const testAccAWSAutoScalingGroupConfig_terminationPoliciesUpdate = `
resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11"
instance_type = "t1.micro"
}

resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-west-2a"]
max_size = 0
min_size = 0
desired_capacity = 0
termination_policies = ["OldestInstance"]

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

const testAccAWSAutoScalingGroupConfig = `
resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11"
Expand Down