Skip to content

Commit

Permalink
Merge pull request #29277 from tmccombs/optional-resource-labels
Browse files Browse the repository at this point in the history
r/aws_autoscaling_policy Change resource_label to optional
  • Loading branch information
johnsonaj authored Feb 10, 2023
2 parents 2f578b5 + 187624c commit 889cb5b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/29277.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_autoscaling_policy: Make `resource_label` optional in `predefined_load_metric_specification`, `predefined_metric_pair_specification`, and `predefined_scaling_metric_specification`
```
18 changes: 12 additions & 6 deletions internal/service/autoscaling/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func ResourcePolicy() *schema.Resource {
},
"resource_label": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
},
},
Expand All @@ -177,7 +177,7 @@ func ResourcePolicy() *schema.Resource {
},
"resource_label": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
},
},
Expand All @@ -196,7 +196,7 @@ func ResourcePolicy() *schema.Resource {
},
"resource_label": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
},
},
Expand Down Expand Up @@ -783,7 +783,9 @@ func expandPredefinedLoadMetricSpecification(predefinedLoadMetricSpecificationSl
predefinedLoadMetricSpecificationFlat := predefinedLoadMetricSpecificationSlice[0].(map[string]interface{})
predefinedLoadMetricSpecification := &autoscaling.PredictiveScalingPredefinedLoadMetric{
PredefinedMetricType: aws.String(predefinedLoadMetricSpecificationFlat["predefined_metric_type"].(string)),
ResourceLabel: aws.String(predefinedLoadMetricSpecificationFlat["resource_label"].(string)),
}
if label, ok := predefinedLoadMetricSpecificationFlat["resource_label"].(string); ok && label != "" {
predefinedLoadMetricSpecification.ResourceLabel = aws.String(label)
}
return predefinedLoadMetricSpecification
}
Expand All @@ -795,7 +797,9 @@ func expandPredefinedMetricPairSpecification(predefinedMetricPairSpecificationSl
predefinedMetricPairSpecificationFlat := predefinedMetricPairSpecificationSlice[0].(map[string]interface{})
predefinedMetricPairSpecification := &autoscaling.PredictiveScalingPredefinedMetricPair{
PredefinedMetricType: aws.String(predefinedMetricPairSpecificationFlat["predefined_metric_type"].(string)),
ResourceLabel: aws.String(predefinedMetricPairSpecificationFlat["resource_label"].(string)),
}
if label, ok := predefinedMetricPairSpecificationFlat["resource_label"].(string); ok && label != "" {
predefinedMetricPairSpecification.ResourceLabel = aws.String(label)
}
return predefinedMetricPairSpecification
}
Expand All @@ -807,7 +811,9 @@ func expandPredefinedScalingMetricSpecification(predefinedScalingMetricSpecifica
predefinedScalingMetricSpecificationFlat := predefinedScalingMetricSpecificationSlice[0].(map[string]interface{})
predefinedScalingMetricSpecification := &autoscaling.PredictiveScalingPredefinedScalingMetric{
PredefinedMetricType: aws.String(predefinedScalingMetricSpecificationFlat["predefined_metric_type"].(string)),
ResourceLabel: aws.String(predefinedScalingMetricSpecificationFlat["resource_label"].(string)),
}
if label, ok := predefinedScalingMetricSpecificationFlat["resource_label"].(string); ok && label != "" {
predefinedScalingMetricSpecification.ResourceLabel = aws.String(label)
}
return predefinedScalingMetricSpecification
}
Expand Down
62 changes: 62 additions & 0 deletions internal/service/autoscaling/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,43 @@ func TestAccAutoScalingPolicy_predictiveScalingPredefined(t *testing.T) {
})
}

func TestAccAutoScalingPolicy_predictiveScalingResourceLabel(t *testing.T) {
ctx := acctest.Context(t)
var v autoscaling.ScalingPolicy
resourceSimpleName := "aws_autoscaling_policy.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, autoscaling.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckPolicyDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccPolicyConfig_predictiveScalingPredefined_resourceLabel(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckScalingPolicyExists(ctx, resourceSimpleName, &v),
resource.TestCheckResourceAttr(resourceSimpleName, "predictive_scaling_configuration.0.mode", "ForecastAndScale"),
resource.TestCheckResourceAttr(resourceSimpleName, "predictive_scaling_configuration.0.scheduling_buffer_time", "10"),
resource.TestCheckResourceAttr(resourceSimpleName, "predictive_scaling_configuration.0.max_capacity_breach_behavior", "IncreaseMaxCapacity"),
resource.TestCheckResourceAttr(resourceSimpleName, "predictive_scaling_configuration.0.max_capacity_buffer", "0"),
resource.TestCheckResourceAttr(resourceSimpleName, "predictive_scaling_configuration.0.metric_specification.0.target_value", "32"),
resource.TestCheckResourceAttr(resourceSimpleName, "predictive_scaling_configuration.0.metric_specification.0.predefined_scaling_metric_specification.0.predefined_metric_type", "ASGAverageCPUUtilization"),
resource.TestCheckResourceAttr(resourceSimpleName, "predictive_scaling_configuration.0.metric_specification.0.predefined_scaling_metric_specification.0.resource_label", ""),
resource.TestCheckResourceAttr(resourceSimpleName, "predictive_scaling_configuration.0.metric_specification.0.predefined_load_metric_specification.0.predefined_metric_type", "ASGTotalCPUUtilization"),
resource.TestCheckResourceAttr(resourceSimpleName, "predictive_scaling_configuration.0.metric_specification.0.predefined_load_metric_specification.0.resource_label", ""),
),
},
{
ResourceName: resourceSimpleName,
ImportState: true,
ImportStateIdFunc: testAccPolicyImportStateIdFunc(resourceSimpleName),
ImportStateVerify: true,
},
},
})
}

func TestAccAutoScalingPolicy_predictiveScalingCustom(t *testing.T) {
ctx := acctest.Context(t)
var v autoscaling.ScalingPolicy
Expand Down Expand Up @@ -596,6 +633,31 @@ resource "aws_autoscaling_policy" "test" {
`, rName))
}

func testAccPolicyConfig_predictiveScalingPredefined_resourceLabel(rName string) string {
return acctest.ConfigCompose(testAccPolicyConfigBase(rName), fmt.Sprintf(`
resource "aws_autoscaling_policy" "test" {
name = "%[1]s-predictive"
policy_type = "PredictiveScaling"
autoscaling_group_name = aws_autoscaling_group.test.name
predictive_scaling_configuration {
metric_specification {
target_value = 32
predefined_scaling_metric_specification {
predefined_metric_type = "ASGAverageCPUUtilization"
}
predefined_load_metric_specification {
predefined_metric_type = "ASGTotalCPUUtilization"
}
}
mode = "ForecastAndScale"
scheduling_buffer_time = 10
max_capacity_breach_behavior = "IncreaseMaxCapacity"
max_capacity_buffer = 0
}
}
`, rName))
}

func testAccPolicyConfig_predictiveScalingCustom(rName string) string {
return acctest.ConfigCompose(testAccPolicyConfigBase(rName), fmt.Sprintf(`
resource "aws_autoscaling_policy" "test" {
Expand Down

0 comments on commit 889cb5b

Please sign in to comment.