Merge pull request #5101 from tpounds/fix-read-aws-asg-termination-policies

provider/aws: Fix reading auto scaling group termination policies
This commit is contained in:
Paul Hinze 2016-02-23 08:03:18 -06:00
commit b5e6a4fac7
2 changed files with 107 additions and 10 deletions

View File

@ -255,7 +255,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
}
@ -316,18 +325,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"})
}
}

View File

@ -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
@ -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"