From 44b00782c3df13732636969daefcde5574ddad7f Mon Sep 17 00:00:00 2001 From: Trevor Pounds Date: Fri, 22 Jan 2016 23:33:20 -0800 Subject: [PATCH 1/3] Fix termination policies read logic. --- .../providers/aws/resource_aws_autoscaling_group.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_autoscaling_group.go b/builtin/providers/aws/resource_aws_autoscaling_group.go index 83b39e6ae37c..9f278897861c 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group.go @@ -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 } From b39cc55f3742fe605ef2ad2ae03ac56944494c60 Mon Sep 17 00:00:00 2001 From: Trevor Pounds Date: Thu, 11 Feb 2016 08:45:11 -0800 Subject: [PATCH 2/3] Cleanup termination policy update logic. --- .../providers/aws/resource_aws_autoscaling_group.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/builtin/providers/aws/resource_aws_autoscaling_group.go b/builtin/providers/aws/resource_aws_autoscaling_group.go index 9f278897861c..697cf875dcc3 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group.go @@ -321,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"}) } } From 066a60debef91364317a566063169e88176a6c01 Mon Sep 17 00:00:00 2001 From: Trevor Pounds Date: Thu, 11 Feb 2016 08:53:34 -0800 Subject: [PATCH 3/3] Add termination policies acceptance test. --- .../resource_aws_autoscaling_group_test.go | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/builtin/providers/aws/resource_aws_autoscaling_group_test.go b/builtin/providers/aws/resource_aws_autoscaling_group_test.go index 8d26320c0f85..4292baca88bc 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group_test.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group_test.go @@ -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"