From 925fc116a814931546fec2ed23d791b4acf6231d Mon Sep 17 00:00:00 2001 From: Dennis Webb Date: Fri, 16 Sep 2016 21:00:57 -0500 Subject: [PATCH 1/3] always set scaling_adjustment when policy_type = SimpleScaling --- .../aws/resource_aws_autoscaling_policy.go | 7 ++- .../resource_aws_autoscaling_policy_test.go | 59 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/builtin/providers/aws/resource_aws_autoscaling_policy.go b/builtin/providers/aws/resource_aws_autoscaling_policy.go index ea7b2bb06..490caf603 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_policy.go +++ b/builtin/providers/aws/resource_aws_autoscaling_policy.go @@ -218,8 +218,11 @@ func getAwsAutoscalingPutScalingPolicyInput(d *schema.ResourceData) (autoscaling params.PolicyType = aws.String(v.(string)) } - if v, ok := d.GetOk("scaling_adjustment"); ok { - params.ScalingAdjustment = aws.Int64(int64(v.(int))) + //if policy_type=="SimpleScaling" then scaling_adjustment is required and 0 is allowed + if *params.PolicyType == "SimpleScaling" { + params.ScalingAdjustment = aws.Int64(int64(d.Get("scaling_adjustment").(int))) + } else if v, ok := d.GetOk("scaling_adjustment"); ok { + params.EstimatedInstanceWarmup = aws.Int64(int64(v.(int))) } if v, ok := d.GetOk("step_adjustment"); ok { diff --git a/builtin/providers/aws/resource_aws_autoscaling_policy_test.go b/builtin/providers/aws/resource_aws_autoscaling_policy_test.go index d2d2f11fd..c7f5fbcaf 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_policy_test.go +++ b/builtin/providers/aws/resource_aws_autoscaling_policy_test.go @@ -316,3 +316,62 @@ resource "aws_autoscaling_policy" "foobar_simple" { } `, name, name, name) } + +func TestAccAWSAutoscalingPolicy_SimpleScalingStepAdjustment(t *testing.T) { + var policy autoscaling.ScalingPolicy + + name := acctest.RandString(5) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAutoscalingPolicyDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAutoscalingPolicyConfig_SimpleScalingStepAdjustment(name), + Check: resource.ComposeTestCheckFunc( + testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy), + resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "adjustment_type", "ExactCapacity"), + resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "scaling_adjustment", "0"), + ), + }, + }, + }) +} + +func testAccAWSAutoscalingPolicyConfig_SimpleScalingStepAdjustment(name string) string { + return fmt.Sprintf(` +resource "aws_launch_configuration" "foobar" { + name = "tf-test-%s" + image_id = "ami-21f78e11" + instance_type = "t1.micro" +} + +resource "aws_autoscaling_group" "foobar" { + availability_zones = ["us-west-2a"] + name = "terraform-test-%s" + max_size = 5 + min_size = 0 + health_check_grace_period = 300 + health_check_type = "ELB" + force_delete = true + termination_policies = ["OldestInstance"] + launch_configuration = "${aws_launch_configuration.foobar.name}" + + tag { + key = "Foo" + value = "foo-bar" + propagate_at_launch = true + } +} + +resource "aws_autoscaling_policy" "foobar_simple" { + name = "foobar_simple_%s" + adjustment_type = "ExactCapacity" + cooldown = 300 + policy_type = "SimpleScaling" + scaling_adjustment = 0 + autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" +} +`, name, name, name) +} From 22b442bed6e0cc81cf2df7695915563e5db3808e Mon Sep 17 00:00:00 2001 From: Dennis Webb Date: Mon, 19 Sep 2016 10:48:35 -0400 Subject: [PATCH 2/3] better if testing --- builtin/providers/aws/resource_aws_autoscaling_policy.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/builtin/providers/aws/resource_aws_autoscaling_policy.go b/builtin/providers/aws/resource_aws_autoscaling_policy.go index 490caf603..63ca8341c 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_policy.go +++ b/builtin/providers/aws/resource_aws_autoscaling_policy.go @@ -219,9 +219,7 @@ func getAwsAutoscalingPutScalingPolicyInput(d *schema.ResourceData) (autoscaling } //if policy_type=="SimpleScaling" then scaling_adjustment is required and 0 is allowed - if *params.PolicyType == "SimpleScaling" { - params.ScalingAdjustment = aws.Int64(int64(d.Get("scaling_adjustment").(int))) - } else if v, ok := d.GetOk("scaling_adjustment"); ok { + if v, ok := d.GetOk("scaling_adjustment"); ok || *params.PolicyType == "SimpleScaling" { params.EstimatedInstanceWarmup = aws.Int64(int64(v.(int))) } From e6f2f2e39c068eab12cbf241e25ab71f760787e8 Mon Sep 17 00:00:00 2001 From: Dennis Webb Date: Mon, 19 Sep 2016 11:24:27 -0400 Subject: [PATCH 3/3] fixed incorrect param name --- builtin/providers/aws/resource_aws_autoscaling_policy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_autoscaling_policy.go b/builtin/providers/aws/resource_aws_autoscaling_policy.go index 63ca8341c..3c91bd32b 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_policy.go +++ b/builtin/providers/aws/resource_aws_autoscaling_policy.go @@ -220,7 +220,7 @@ func getAwsAutoscalingPutScalingPolicyInput(d *schema.ResourceData) (autoscaling //if policy_type=="SimpleScaling" then scaling_adjustment is required and 0 is allowed if v, ok := d.GetOk("scaling_adjustment"); ok || *params.PolicyType == "SimpleScaling" { - params.EstimatedInstanceWarmup = aws.Int64(int64(v.(int))) + params.ScalingAdjustment = aws.Int64(int64(v.(int))) } if v, ok := d.GetOk("step_adjustment"); ok {