From c2151914fec9a03e1ef98854c3ffbbb9705f8c92 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Mon, 6 Feb 2023 23:22:42 -0700 Subject: [PATCH 1/5] r/aws_autoscaling_policy Change resource_label to optional Fixes #28597 --- internal/service/autoscaling/policy.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/service/autoscaling/policy.go b/internal/service/autoscaling/policy.go index ff4ad34b30c..74e0aa58678 100644 --- a/internal/service/autoscaling/policy.go +++ b/internal/service/autoscaling/policy.go @@ -159,7 +159,7 @@ func ResourcePolicy() *schema.Resource { }, "resource_label": { Type: schema.TypeString, - Required: true, + Required: false, }, }, }, @@ -177,7 +177,7 @@ func ResourcePolicy() *schema.Resource { }, "resource_label": { Type: schema.TypeString, - Required: true, + Required: false, }, }, }, @@ -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"]; ok { + predefinedLoadMetricSpecification.ResourceLabel = aws.String(label.(string)) } return predefinedLoadMetricSpecification } @@ -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"]; ok { + predefinedMetricPairSpecification.ResourceLabel = aws.String(label.(string)) } return predefinedMetricPairSpecification } @@ -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"]; ok { + predefinedScalingMetricSpecification.ResourceLabel = aws.String(label.(string)) } return predefinedScalingMetricSpecification } From 952d5c3c088aac0c62e9fa8a01a993faae7d3886 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Fri, 10 Feb 2023 09:36:37 -0600 Subject: [PATCH 2/5] r'aws_autoscaling_policy: set Optional: true for resource_label --- internal/service/autoscaling/policy.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/service/autoscaling/policy.go b/internal/service/autoscaling/policy.go index 74e0aa58678..f38c1485c53 100644 --- a/internal/service/autoscaling/policy.go +++ b/internal/service/autoscaling/policy.go @@ -159,7 +159,7 @@ func ResourcePolicy() *schema.Resource { }, "resource_label": { Type: schema.TypeString, - Required: false, + Optional: true, }, }, }, @@ -177,7 +177,7 @@ func ResourcePolicy() *schema.Resource { }, "resource_label": { Type: schema.TypeString, - Required: false, + Optional: true, }, }, }, @@ -196,7 +196,7 @@ func ResourcePolicy() *schema.Resource { }, "resource_label": { Type: schema.TypeString, - Required: true, + Optional: true, }, }, }, @@ -784,8 +784,8 @@ func expandPredefinedLoadMetricSpecification(predefinedLoadMetricSpecificationSl predefinedLoadMetricSpecification := &autoscaling.PredictiveScalingPredefinedLoadMetric{ PredefinedMetricType: aws.String(predefinedLoadMetricSpecificationFlat["predefined_metric_type"].(string)), } - if label, ok := predefinedLoadMetricSpecificationFlat["resource_label"]; ok { - predefinedLoadMetricSpecification.ResourceLabel = aws.String(label.(string)) + if label, ok := predefinedLoadMetricSpecificationFlat["resource_label"].(string); ok && label != "" { + predefinedLoadMetricSpecification.ResourceLabel = aws.String(label) } return predefinedLoadMetricSpecification } @@ -798,8 +798,8 @@ func expandPredefinedMetricPairSpecification(predefinedMetricPairSpecificationSl predefinedMetricPairSpecification := &autoscaling.PredictiveScalingPredefinedMetricPair{ PredefinedMetricType: aws.String(predefinedMetricPairSpecificationFlat["predefined_metric_type"].(string)), } - if label, ok := predefinedMetricPairSpecificationFlat["resource_label"]; ok { - predefinedMetricPairSpecification.ResourceLabel = aws.String(label.(string)) + if label, ok := predefinedMetricPairSpecificationFlat["resource_label"].(string); ok && label != "" { + predefinedMetricPairSpecification.ResourceLabel = aws.String(label) } return predefinedMetricPairSpecification } @@ -812,8 +812,8 @@ func expandPredefinedScalingMetricSpecification(predefinedScalingMetricSpecifica predefinedScalingMetricSpecification := &autoscaling.PredictiveScalingPredefinedScalingMetric{ PredefinedMetricType: aws.String(predefinedScalingMetricSpecificationFlat["predefined_metric_type"].(string)), } - if label, ok := predefinedScalingMetricSpecificationFlat["resource_label"]; ok { - predefinedScalingMetricSpecification.ResourceLabel = aws.String(label.(string)) + if label, ok := predefinedScalingMetricSpecificationFlat["resource_label"].(string); ok && label != "" { + predefinedScalingMetricSpecification.ResourceLabel = aws.String(label) } return predefinedScalingMetricSpecification } From af78121b1bd7c8afaf60bcf2822e270babbcc988 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Fri, 10 Feb 2023 10:05:11 -0600 Subject: [PATCH 3/5] r/aws_autoscaling_policy: add test --- internal/service/autoscaling/policy_test.go | 62 +++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/internal/service/autoscaling/policy_test.go b/internal/service/autoscaling/policy_test.go index 063c77df587..79503fe8e4b 100644 --- a/internal/service/autoscaling/policy_test.go +++ b/internal/service/autoscaling/policy_test.go @@ -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 @@ -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" { From 8cf6e70894119f829fde49db0d738b93d52179e8 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Fri, 10 Feb 2023 10:05:52 -0600 Subject: [PATCH 4/5] add CHANGLOG entry --- .changelog/29277.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/29277.txt diff --git a/.changelog/29277.txt b/.changelog/29277.txt new file mode 100644 index 00000000000..6351cfc5468 --- /dev/null +++ b/.changelog/29277.txt @@ -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"` +``` \ No newline at end of file From 187624cfdee6f3e75c75bec334a929b46efc5cc4 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Fri, 10 Feb 2023 10:32:29 -0600 Subject: [PATCH 5/5] update CHANGLOG entry --- .changelog/29277.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/29277.txt b/.changelog/29277.txt index 6351cfc5468..972bbbe3cd5 100644 --- a/.changelog/29277.txt +++ b/.changelog/29277.txt @@ -1,3 +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"` +resource/aws_autoscaling_policy: Make `resource_label` optional in `predefined_load_metric_specification`, `predefined_metric_pair_specification`, and `predefined_scaling_metric_specification` ``` \ No newline at end of file