diff --git a/aws/resource_aws_appautoscaling_policy.go b/aws/resource_aws_appautoscaling_policy.go index 102c5406287..7e6e0492a52 100644 --- a/aws/resource_aws_appautoscaling_policy.go +++ b/aws/resource_aws_appautoscaling_policy.go @@ -1,7 +1,6 @@ package aws import ( - "bytes" "fmt" "log" "strconv" @@ -9,7 +8,6 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/applicationautoscaling" - "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" @@ -55,6 +53,7 @@ func resourceAwsAppautoscalingPolicy() *schema.Resource { }, "step_scaling_policy_configuration": { Type: schema.TypeList, + MaxItems: 1, Optional: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -105,30 +104,29 @@ func resourceAwsAppautoscalingPolicy() *schema.Resource { }, "adjustment_type": { - Type: schema.TypeString, - Optional: true, - Deprecated: "Use step_scaling_policy_configuration -> adjustment_type instead", + Type: schema.TypeString, + Optional: true, + Removed: "Use `step_scaling_policy_configuration` configuration block `adjustment_type` argument instead", }, "cooldown": { - Type: schema.TypeInt, - Optional: true, - Deprecated: "Use step_scaling_policy_configuration -> cooldown instead", + Type: schema.TypeInt, + Optional: true, + Removed: "Use `step_scaling_policy_configuration` configuration block `cooldown` argument instead", }, "metric_aggregation_type": { - Type: schema.TypeString, - Optional: true, - Deprecated: "Use step_scaling_policy_configuration -> metric_aggregation_type instead", + Type: schema.TypeString, + Optional: true, + Removed: "Use `step_scaling_policy_configuration` configuration block `metric_aggregation_type` argument instead", }, "min_adjustment_magnitude": { - Type: schema.TypeInt, - Optional: true, - Deprecated: "Use step_scaling_policy_configuration -> min_adjustment_magnitude instead", + Type: schema.TypeInt, + Optional: true, + Removed: "Use `step_scaling_policy_configuration` configuration block `min_adjustment_magnitude` argument instead", }, "step_adjustment": { - Type: schema.TypeSet, - Optional: true, - Deprecated: "Use step_scaling_policy_configuration -> step_adjustment instead", - Set: resourceAwsAppautoscalingAdjustmentHash, + Type: schema.TypeSet, + Optional: true, + Removed: "Use `step_scaling_policy_configuration` configuration block `step_adjustment` configuration block instead", Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "metric_interval_lower_bound": { @@ -315,9 +313,12 @@ func resourceAwsAppautoscalingPolicyRead(d *schema.ResourceData, meta interface{ d.Set("scalable_dimension", p.ScalableDimension) d.Set("service_namespace", p.ServiceNamespace) d.Set("alarms", p.Alarms) - d.Set("step_scaling_policy_configuration", flattenStepScalingPolicyConfiguration(p.StepScalingPolicyConfiguration)) - d.Set("target_tracking_scaling_policy_configuration", - flattenTargetTrackingScalingPolicyConfiguration(p.TargetTrackingScalingPolicyConfiguration)) + if err := d.Set("step_scaling_policy_configuration", flattenStepScalingPolicyConfiguration(p.StepScalingPolicyConfiguration)); err != nil { + return fmt.Errorf("error setting step_scaling_policy_configuration: %s", err) + } + if err := d.Set("target_tracking_scaling_policy_configuration", flattenTargetTrackingScalingPolicyConfiguration(p.TargetTrackingScalingPolicyConfiguration)); err != nil { + return fmt.Errorf("error setting target_tracking_scaling_policy_configuration: %s", err) + } return nil } @@ -504,43 +505,6 @@ func getAwsAppautoscalingPutScalingPolicyInput(d *schema.ResourceData) (applicat params.ScalableDimension = aws.String(v.(string)) } - // Deprecated fields - // TODO: Remove in next major version - at, atOk := d.GetOk("adjustment_type") - cd, cdOk := d.GetOk("cooldown") - mat, matOk := d.GetOk("metric_aggregation_type") - mam, mamOk := d.GetOk("min_adjustment_magnitude") - sa, saOk := d.GetOk("step_adjustment") - if atOk || cdOk || matOk || mamOk || saOk { - cfg := &applicationautoscaling.StepScalingPolicyConfiguration{} - - if atOk { - cfg.AdjustmentType = aws.String(at.(string)) - } - - if cdOk { - cfg.Cooldown = aws.Int64(int64(cd.(int))) - } - - if matOk { - cfg.MetricAggregationType = aws.String(mat.(string)) - } - - if saOk { - steps, err := expandAppautoscalingStepAdjustments(sa.(*schema.Set).List()) - if err != nil { - return params, fmt.Errorf("metric_interval_lower_bound and metric_interval_upper_bound must be strings!") - } - cfg.StepAdjustments = steps - } - - if mamOk { - cfg.MinAdjustmentMagnitude = aws.Int64(int64(mam.(int))) - } - - params.StepScalingPolicyConfiguration = cfg - } - if v, ok := d.GetOk("step_scaling_policy_configuration"); ok { params.StepScalingPolicyConfiguration = expandStepScalingPolicyConfiguration(v.([]interface{})) } @@ -638,19 +602,35 @@ func flattenStepScalingPolicyConfiguration(cfg *applicationautoscaling.StepScali m := make(map[string]interface{}) if cfg.AdjustmentType != nil { - m["adjustment_type"] = *cfg.AdjustmentType + m["adjustment_type"] = aws.StringValue(cfg.AdjustmentType) } if cfg.Cooldown != nil { - m["cooldown"] = *cfg.Cooldown + m["cooldown"] = aws.Int64Value(cfg.Cooldown) } if cfg.MetricAggregationType != nil { - m["metric_aggregation_type"] = *cfg.MetricAggregationType + m["metric_aggregation_type"] = aws.StringValue(cfg.MetricAggregationType) } if cfg.MinAdjustmentMagnitude != nil { - m["min_adjustment_magnitude"] = *cfg.MinAdjustmentMagnitude + m["min_adjustment_magnitude"] = aws.Int64Value(cfg.MinAdjustmentMagnitude) } if cfg.StepAdjustments != nil { - m["step_adjustment"] = flattenAppautoscalingStepAdjustments(cfg.StepAdjustments) + stepAdjustmentsResource := &schema.Resource{ + Schema: map[string]*schema.Schema{ + "metric_interval_lower_bound": { + Type: schema.TypeString, + Optional: true, + }, + "metric_interval_upper_bound": { + Type: schema.TypeString, + Optional: true, + }, + "scaling_adjustment": { + Type: schema.TypeInt, + Required: true, + }, + }, + } + m["step_adjustment"] = schema.NewSet(schema.HashResource(stepAdjustmentsResource), flattenAppautoscalingStepAdjustments(cfg.StepAdjustments)) } return []interface{}{m} @@ -662,13 +642,13 @@ func flattenAppautoscalingStepAdjustments(adjs []*applicationautoscaling.StepAdj for i, adj := range adjs { m := make(map[string]interface{}) - m["scaling_adjustment"] = *adj.ScalingAdjustment + m["scaling_adjustment"] = int(aws.Int64Value(adj.ScalingAdjustment)) if adj.MetricIntervalLowerBound != nil { - m["metric_interval_lower_bound"] = *adj.MetricIntervalLowerBound + m["metric_interval_lower_bound"] = fmt.Sprintf("%g", aws.Float64Value(adj.MetricIntervalLowerBound)) } if adj.MetricIntervalUpperBound != nil { - m["metric_interval_upper_bound"] = *adj.MetricIntervalUpperBound + m["metric_interval_upper_bound"] = fmt.Sprintf("%g", aws.Float64Value(adj.MetricIntervalUpperBound)) } out[i] = m @@ -748,17 +728,3 @@ func flattenPredefinedMetricSpecification(cfg *applicationautoscaling.Predefined } return []interface{}{m} } - -func resourceAwsAppautoscalingAdjustmentHash(v interface{}) int { - var buf bytes.Buffer - m := v.(map[string]interface{}) - if v, ok := m["metric_interval_lower_bound"]; ok { - buf.WriteString(fmt.Sprintf("%f-", v)) - } - if v, ok := m["metric_interval_upper_bound"]; ok { - buf.WriteString(fmt.Sprintf("%f-", v)) - } - buf.WriteString(fmt.Sprintf("%d-", m["scaling_adjustment"].(int))) - - return hashcode.String(buf.String()) -} diff --git a/aws/resource_aws_appautoscaling_policy_test.go b/aws/resource_aws_appautoscaling_policy_test.go index 2b6f77f1c5a..b4499a96ee7 100644 --- a/aws/resource_aws_appautoscaling_policy_test.go +++ b/aws/resource_aws_appautoscaling_policy_test.go @@ -13,9 +13,9 @@ import ( func TestAccAWSAppautoScalingPolicy_basic(t *testing.T) { var policy applicationautoscaling.ScalingPolicy - - randClusterName := fmt.Sprintf("cluster%s", acctest.RandString(10)) - randPolicyName := fmt.Sprintf("terraform-test-foobar-%s", acctest.RandString(5)) + appAutoscalingTargetResourceName := "aws_appautoscaling_target.test" + resourceName := "aws_appautoscaling_policy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -23,51 +23,20 @@ func TestAccAWSAppautoScalingPolicy_basic(t *testing.T) { CheckDestroy: testAccCheckAWSAppautoscalingPolicyDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSAppautoscalingPolicyConfig(randClusterName, randPolicyName), + Config: testAccAWSAppautoscalingPolicyConfig(rName), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.foobar_simple", &policy), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "adjustment_type", "ChangeInCapacity"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "policy_type", "StepScaling"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "cooldown", "60"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "step_adjustment.#", "1"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "step_adjustment.2087484785.scaling_adjustment", "1"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "step_adjustment.2087484785.metric_interval_lower_bound", "0"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "name", randPolicyName), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "resource_id", fmt.Sprintf("service/%s/foobar", randClusterName)), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "service_namespace", "ecs"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "scalable_dimension", "ecs:service:DesiredCount"), - ), - }, - }, - }) -} - -func TestAccAWSAppautoScalingPolicy_nestedSchema(t *testing.T) { - var policy applicationautoscaling.ScalingPolicy - - randClusterName := fmt.Sprintf("cluster%s", acctest.RandString(10)) - randPolicyName := fmt.Sprintf("terraform-test-foobar-%s", acctest.RandString(5)) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSAppautoscalingPolicyDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSAppautoscalingPolicyNestedSchemaConfig(randClusterName, randPolicyName), - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.foobar_simple", &policy), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "step_scaling_policy_configuration.0.adjustment_type", "PercentChangeInCapacity"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "step_scaling_policy_configuration.0.cooldown", "60"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "step_scaling_policy_configuration.0.step_adjustment.#", "1"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "step_scaling_policy_configuration.0.step_adjustment.1704088838.scaling_adjustment", "1"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "step_scaling_policy_configuration.0.step_adjustment.1704088838.metric_interval_lower_bound", "1"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "step_scaling_policy_configuration.0.step_adjustment.1704088838.metric_interval_upper_bound", ""), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "name", randPolicyName), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "policy_type", "StepScaling"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "resource_id", fmt.Sprintf("service/%s/foobar", randClusterName)), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "service_namespace", "ecs"), - resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "scalable_dimension", "ecs:service:DesiredCount"), + testAccCheckAWSAppautoscalingPolicyExists(resourceName, &policy), + resource.TestCheckResourceAttr(resourceName, "name", rName), + resource.TestCheckResourceAttr(resourceName, "policy_type", "StepScaling"), + resource.TestCheckResourceAttrPair(resourceName, "resource_id", appAutoscalingTargetResourceName, "resource_id"), + resource.TestCheckResourceAttrPair(resourceName, "scalable_dimension", appAutoscalingTargetResourceName, "scalable_dimension"), + resource.TestCheckResourceAttrPair(resourceName, "service_namespace", appAutoscalingTargetResourceName, "service_namespace"), + resource.TestCheckResourceAttr(resourceName, "step_scaling_policy_configuration.0.adjustment_type", "ChangeInCapacity"), + resource.TestCheckResourceAttr(resourceName, "step_scaling_policy_configuration.0.cooldown", "60"), + resource.TestCheckResourceAttr(resourceName, "step_scaling_policy_configuration.0.step_adjustment.#", "1"), + resource.TestCheckResourceAttr(resourceName, "step_scaling_policy_configuration.0.step_adjustment.207530251.scaling_adjustment", "1"), + resource.TestCheckResourceAttr(resourceName, "step_scaling_policy_configuration.0.step_adjustment.207530251.metric_interval_lower_bound", "0"), + resource.TestCheckResourceAttr(resourceName, "step_scaling_policy_configuration.0.step_adjustment.207530251.metric_interval_upper_bound", ""), ), }, }, @@ -289,61 +258,62 @@ func testAccCheckAWSAppautoscalingPolicyDestroy(s *terraform.State) error { return nil } -func testAccAWSAppautoscalingPolicyConfig( - randClusterName string, - randPolicyName string) string { +func testAccAWSAppautoscalingPolicyConfig(rName string) string { return fmt.Sprintf(` -resource "aws_ecs_cluster" "foo" { - name = "%s" +resource "aws_ecs_cluster" "test" { + name = %[1]q } -resource "aws_ecs_task_definition" "task" { - family = "foobar" - container_definitions = <