Skip to content

Commit

Permalink
resource/aws_autoscaling_policy: Properly read step_adjustment into T…
Browse files Browse the repository at this point in the history
…erraform state

Previously, the resource was silently failing to read this attribute and therefore unable to perform drift detection or properly import the resource (currently being added).

```
--- FAIL: TestAccAWSAutoscalingPolicy_basic (85.72s)
    testing.go:538: Step 2 error: ImportStateVerify attributes not equivalent. Difference is shown below. Top is actual, bottom is expected.

        (map[string]string) {
        }

        (map[string]string) (len=4) {
         (string) (len=17) "step_adjustment.#": (string) (len=1) "1",
         (string) (len=54) "step_adjustment.2042107634.metric_interval_lower_bound": (string) (len=1) "2",
         (string) (len=54) "step_adjustment.2042107634.metric_interval_upper_bound": (string) "",
         (string) (len=45) "step_adjustment.2042107634.scaling_adjustment": (string) (len=1) "1"
        }
```

Returning the error from `d.Set()`:

```
--- FAIL: TestAccAWSAutoscalingPolicy_basic (14.03s)
    testing.go:538: Step 0 error: Error applying: 1 error occurred:
        	* aws_autoscaling_policy.foobar_step: 1 error occurred:
        	* aws_autoscaling_policy.foobar_step: error setting step_adjustment: step_adjustment.0.metric_interval_lower_bound: '' expected type 'string', got unconvertible type 'float64'
```

Output from acceptance testing after adjustments:

```
--- PASS: TestAccAWSAutoscalingPolicy_zerovalue (44.61s)
--- PASS: TestAccAWSAutoscalingPolicy_SimpleScalingStepAdjustment (46.60s)
--- PASS: TestAccAWSAutoscalingPolicy_TargetTrack_Predefined (48.75s)
--- PASS: TestAccAWSAutoscalingPolicy_disappears (72.73s)
--- PASS: TestAccAWSAutoscalingPolicy_TargetTrack_Custom (74.47s)
--- PASS: TestAccAWSAutoscalingPolicy_upgrade (76.43s)
--- PASS: TestAccAWSAutoscalingPolicy_basic (82.25s)
```
  • Loading branch information
bflad committed Jan 25, 2019
1 parent ac093a5 commit 75bb031
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
8 changes: 6 additions & 2 deletions aws/resource_aws_autoscaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,12 @@ func resourceAwsAutoscalingPolicyRead(d *schema.ResourceData, meta interface{})
d.Set("arn", p.PolicyARN)
d.Set("name", p.PolicyName)
d.Set("scaling_adjustment", p.ScalingAdjustment)
d.Set("step_adjustment", flattenStepAdjustments(p.StepAdjustments))
d.Set("target_tracking_configuration", flattenTargetTrackingConfiguration(p.TargetTrackingConfiguration))
if err := d.Set("step_adjustment", flattenStepAdjustments(p.StepAdjustments)); err != nil {
return fmt.Errorf("error setting step_adjustment: %s", err)
}
if err := d.Set("target_tracking_configuration", flattenTargetTrackingConfiguration(p.TargetTrackingConfiguration)); err != nil {
return fmt.Errorf("error setting target_tracking_configuration: %s", err)
}

return nil
}
Expand Down
42 changes: 42 additions & 0 deletions aws/resource_aws_autoscaling_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ func TestAccAWSAutoscalingPolicy_basic(t *testing.T) {
ImportStateIdFunc: testAccAWSAutoscalingPolicyImportStateIdFunc("aws_autoscaling_policy.foobar_simple"),
ImportStateVerify: true,
},
{
ResourceName: "aws_autoscaling_policy.foobar_step",
ImportState: true,
ImportStateIdFunc: testAccAWSAutoscalingPolicyImportStateIdFunc("aws_autoscaling_policy.foobar_step"),
ImportStateVerify: true,
},
{
ResourceName: "aws_autoscaling_policy.foobar_target_tracking",
ImportState: true,
ImportStateIdFunc: testAccAWSAutoscalingPolicyImportStateIdFunc("aws_autoscaling_policy.foobar_target_tracking"),
ImportStateVerify: true,
},
{
Config: testAccAWSAutoscalingPolicyConfig_basicUpdate(name),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -192,6 +204,12 @@ func TestAccAWSAutoscalingPolicy_SimpleScalingStepAdjustment(t *testing.T) {
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "scaling_adjustment", "0"),
),
},
{
ResourceName: "aws_autoscaling_policy.foobar_simple",
ImportState: true,
ImportStateIdFunc: testAccAWSAutoscalingPolicyImportStateIdFunc("aws_autoscaling_policy.foobar_simple"),
ImportStateVerify: true,
},
},
})
}
Expand All @@ -212,6 +230,12 @@ func TestAccAWSAutoscalingPolicy_TargetTrack_Predefined(t *testing.T) {
testAccCheckScalingPolicyExists("aws_autoscaling_policy.test", &policy),
),
},
{
ResourceName: "aws_autoscaling_policy.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAutoscalingPolicyImportStateIdFunc("aws_autoscaling_policy.test"),
ImportStateVerify: true,
},
},
})
}
Expand All @@ -232,6 +256,12 @@ func TestAccAWSAutoscalingPolicy_TargetTrack_Custom(t *testing.T) {
testAccCheckScalingPolicyExists("aws_autoscaling_policy.test", &policy),
),
},
{
ResourceName: "aws_autoscaling_policy.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAutoscalingPolicyImportStateIdFunc("aws_autoscaling_policy.test"),
ImportStateVerify: true,
},
},
})
}
Expand All @@ -256,6 +286,18 @@ func TestAccAWSAutoscalingPolicy_zerovalue(t *testing.T) {
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "estimated_instance_warmup", "0"),
),
},
{
ResourceName: "aws_autoscaling_policy.foobar_simple",
ImportState: true,
ImportStateIdFunc: testAccAWSAutoscalingPolicyImportStateIdFunc("aws_autoscaling_policy.foobar_simple"),
ImportStateVerify: true,
},
{
ResourceName: "aws_autoscaling_policy.foobar_step",
ImportState: true,
ImportStateIdFunc: testAccAWSAutoscalingPolicyImportStateIdFunc("aws_autoscaling_policy.foobar_step"),
ImportStateVerify: true,
},
},
})
}
Expand Down
6 changes: 3 additions & 3 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -1078,13 +1078,13 @@ func flattenStepAdjustments(adjustments []*autoscaling.StepAdjustment) []map[str
result := make([]map[string]interface{}, 0, len(adjustments))
for _, raw := range adjustments {
a := map[string]interface{}{
"scaling_adjustment": *raw.ScalingAdjustment,
"scaling_adjustment": aws.Int64Value(raw.ScalingAdjustment),
}
if raw.MetricIntervalUpperBound != nil {
a["metric_interval_upper_bound"] = *raw.MetricIntervalUpperBound
a["metric_interval_upper_bound"] = fmt.Sprintf("%g", aws.Float64Value(raw.MetricIntervalUpperBound))
}
if raw.MetricIntervalLowerBound != nil {
a["metric_interval_lower_bound"] = *raw.MetricIntervalLowerBound
a["metric_interval_lower_bound"] = fmt.Sprintf("%g", aws.Float64Value(raw.MetricIntervalLowerBound))
}
result = append(result, a)
}
Expand Down
10 changes: 5 additions & 5 deletions aws/structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ func TestFlattenStepAdjustments(t *testing.T) {
expanded := []*autoscaling.StepAdjustment{
{
MetricIntervalLowerBound: aws.Float64(1.0),
MetricIntervalUpperBound: aws.Float64(2.0),
MetricIntervalUpperBound: aws.Float64(2.5),
ScalingAdjustment: aws.Int64(int64(1)),
},
}
Expand All @@ -816,11 +816,11 @@ func TestFlattenStepAdjustments(t *testing.T) {
if result == nil {
t.Fatal("expected result to have value, but got nil")
}
if result["metric_interval_lower_bound"] != float64(1.0) {
t.Fatalf("expected metric_interval_lower_bound to be 1.0, but got %d", result["metric_interval_lower_bound"])
if result["metric_interval_lower_bound"] != "1" {
t.Fatalf("expected metric_interval_lower_bound to be 1, but got %s", result["metric_interval_lower_bound"])
}
if result["metric_interval_upper_bound"] != float64(2.0) {
t.Fatalf("expected metric_interval_upper_bound to be 1.0, but got %d", result["metric_interval_upper_bound"])
if result["metric_interval_upper_bound"] != "2.5" {
t.Fatalf("expected metric_interval_upper_bound to be 2.5, but got %s", result["metric_interval_upper_bound"])
}
if result["scaling_adjustment"] != int64(1) {
t.Fatalf("expected scaling_adjustment to be 1, but got %d", result["scaling_adjustment"])
Expand Down

0 comments on commit 75bb031

Please sign in to comment.