Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predictive autoscaling #19447

Merged
merged 9 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/19447.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_autoscaling_policy: Add `PredictiveScaling` `policy_type` and `predictive_scaling_configuration` argument
```
28 changes: 28 additions & 0 deletions aws/internal/experimental/nullable/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,31 @@ func ValidateTypeStringNullableIntAtLeast(min int) schema.SchemaValidateFunc {
return
}
}

// ValidateTypeStringNullableIntBetween provides custom error messaging for TypeString ints
// Some arguments require an int value or unspecified, empty field.
func ValidateTypeStringNullableIntBetween(min int, max int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (ws []string, es []error) {
value, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}

if value == "" {
return
}

v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
es = append(es, fmt.Errorf("%s: cannot parse '%s' as int: %w", k, value, err))
return
}

if v < int64(min) || v > int64(max) {
es = append(es, fmt.Errorf("expected %s to be at between (%d) and (%d), got %d", k, min, max, v))
}

return
}
}
291 changes: 281 additions & 10 deletions aws/resource_aws_autoscaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"bytes"
"fmt"
"log"
"strconv"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/experimental/nullable"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/hashcode"
)

Expand Down Expand Up @@ -43,16 +45,6 @@ func resourceAwsAutoscalingPolicy() *schema.Resource {
Required: true,
ForceNew: true,
},
"policy_type": {
Type: schema.TypeString,
Optional: true,
Default: "SimpleScaling", // preserve AWS's default to make validation easier.
ValidateFunc: validation.StringInSlice([]string{
"SimpleScaling",
"StepScaling",
"TargetTrackingScaling",
}, false),
},
"cooldown": {
Type: schema.TypeInt,
Optional: true,
Expand All @@ -71,6 +63,136 @@ func resourceAwsAutoscalingPolicy() *schema.Resource {
Optional: true,
ValidateFunc: validation.IntAtLeast(1),
},
"policy_type": {
Type: schema.TypeString,
Optional: true,
Default: "SimpleScaling", // preserve AWS's default to make validation easier.
ValidateFunc: validation.StringInSlice([]string{
"SimpleScaling",
"StepScaling",
"TargetTrackingScaling",
"PredictiveScaling",
}, false),
},
"predictive_scaling_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"metric_specification": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"predefined_metric_pair_specification": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"predefined_metric_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"ASGCPUUtilization",
"ASGNetworkIn",
"ASGNetworkOut",
"ALBRequestCount",
}, false),
},
"resource_label": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"predefined_scaling_metric_specification": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"predefined_metric_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"ASGAverageCPUUtilization",
"ASGAverageNetworkIn",
"ASGAverageNetworkOut",
"ALBRequestCountPerTarget",
}, false),
},
"resource_label": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"predefined_load_metric_specification": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"predefined_metric_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"ASGTotalCPUUtilization",
"ASGTotalNetworkIn",
"ASGTotalNetworkOut",
"ALBTargetGroupRequestCount",
}, false),
},
"resource_label": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"target_value": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
"max_capacity_breach_behavior": {
Type: schema.TypeString,
Optional: true,
Default: "HonorMaxCapacity",
ValidateFunc: validation.StringInSlice([]string{
"HonorMaxCapacity",
"IncreaseMaxCapacity",
}, false),
},
"max_capacity_buffer": {
Type: nullable.TypeNullableInt,
Optional: true,
ValidateFunc: nullable.ValidateTypeStringNullableIntBetween(0, 100),
},
"mode": {
Type: schema.TypeString,
Optional: true,
Default: "ForecastOnly",
ValidateFunc: validation.StringInSlice([]string{
"ForecastOnly",
"ForecastAndScale",
}, false),
},
"scheduling_buffer_time": {
Type: nullable.TypeNullableInt,
Optional: true,
ValidateFunc: nullable.ValidateTypeStringNullableIntAtLeast(0),
},
},
},
},
"scaling_adjustment": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -226,6 +348,9 @@ func resourceAwsAutoscalingPolicyRead(d *schema.ResourceData, meta interface{})
d.Set("arn", p.PolicyARN)
d.Set("name", p.PolicyName)
d.Set("scaling_adjustment", p.ScalingAdjustment)
if err := d.Set("predictive_scaling_configuration", flattenPredictiveScalingConfig(p.PredictiveScalingConfiguration)); err != nil {
return fmt.Errorf("error setting predictive_scaling_configuration: %s", err)
}
if err := d.Set("step_adjustment", flattenStepAdjustments(p.StepAdjustments)); err != nil {
return fmt.Errorf("error setting step_adjustment: %s", err)
}
Expand Down Expand Up @@ -309,6 +434,10 @@ func getAwsAutoscalingPutScalingPolicyInput(d *schema.ResourceData) (autoscaling
params.AdjustmentType = aws.String(v.(string))
}

if predictiveScalingConfigFlat := d.Get("predictive_scaling_configuration").([]interface{}); len(predictiveScalingConfigFlat) > 0 {
params.PredictiveScalingConfiguration = expandPredictiveScalingConfig(predictiveScalingConfigFlat)
}

// This parameter is supported if the policy type is SimpleScaling.
if v, ok := d.GetOkExists("cooldown"); ok {
// 0 is allowed as placeholder even if policyType is not supported
Expand Down Expand Up @@ -482,6 +611,75 @@ func expandTargetTrackingConfiguration(configs []interface{}) *autoscaling.Targe
return result
}

func expandPredictiveScalingConfig(predictiveScalingConfigSlice []interface{}) *autoscaling.PredictiveScalingConfiguration {
if predictiveScalingConfigSlice == nil || len(predictiveScalingConfigSlice) < 1 {
return nil
}
predictiveScalingConfigFlat := predictiveScalingConfigSlice[0].(map[string]interface{})
predictiveScalingConfig := &autoscaling.PredictiveScalingConfiguration{
MetricSpecifications: expandPredictiveScalingMetricSpecifications(predictiveScalingConfigFlat["metric_specification"].([]interface{})),
MaxCapacityBreachBehavior: aws.String(predictiveScalingConfigFlat["max_capacity_breach_behavior"].(string)),
Mode: aws.String(predictiveScalingConfigFlat["mode"].(string)),
}
if v, null, _ := nullable.Int(predictiveScalingConfigFlat["max_capacity_buffer"].(string)).Value(); !null {
predictiveScalingConfig.MaxCapacityBuffer = aws.Int64(v)
}
if v, null, _ := nullable.Int(predictiveScalingConfigFlat["scheduling_buffer_time"].(string)).Value(); !null {
predictiveScalingConfig.SchedulingBufferTime = aws.Int64(v)
}
return predictiveScalingConfig
}

func expandPredictiveScalingMetricSpecifications(metricSpecificationsSlice []interface{}) []*autoscaling.PredictiveScalingMetricSpecification {
if metricSpecificationsSlice == nil || len(metricSpecificationsSlice) < 1 {
return nil
}
metricSpecificationsFlat := metricSpecificationsSlice[0].(map[string]interface{})
metricSpecification := &autoscaling.PredictiveScalingMetricSpecification{
PredefinedLoadMetricSpecification: expandPredefinedLoadMetricSpecification(metricSpecificationsFlat["predefined_load_metric_specification"].([]interface{})),
PredefinedMetricPairSpecification: expandPredefinedMetricPairSpecification(metricSpecificationsFlat["predefined_metric_pair_specification"].([]interface{})),
PredefinedScalingMetricSpecification: expandPredefinedScalingMetricSpecification(metricSpecificationsFlat["predefined_scaling_metric_specification"].([]interface{})),
TargetValue: aws.Float64(float64(metricSpecificationsFlat["target_value"].(int))),
}
return []*autoscaling.PredictiveScalingMetricSpecification{metricSpecification}
}

func expandPredefinedLoadMetricSpecification(predefinedLoadMetricSpecificationSlice []interface{}) *autoscaling.PredictiveScalingPredefinedLoadMetric {
if predefinedLoadMetricSpecificationSlice == nil || len(predefinedLoadMetricSpecificationSlice) < 1 {
return nil
}
predefinedLoadMetricSpecificationFlat := predefinedLoadMetricSpecificationSlice[0].(map[string]interface{})
predefinedLoadMetricSpecification := &autoscaling.PredictiveScalingPredefinedLoadMetric{
PredefinedMetricType: aws.String(predefinedLoadMetricSpecificationFlat["predefined_metric_type"].(string)),
ResourceLabel: aws.String(predefinedLoadMetricSpecificationFlat["resource_label"].(string)),
}
return predefinedLoadMetricSpecification
}

func expandPredefinedMetricPairSpecification(predefinedMetricPairSpecificationSlice []interface{}) *autoscaling.PredictiveScalingPredefinedMetricPair {
if predefinedMetricPairSpecificationSlice == nil || len(predefinedMetricPairSpecificationSlice) < 1 {
return nil
}
predefinedMetricPairSpecificationFlat := predefinedMetricPairSpecificationSlice[0].(map[string]interface{})
predefinedMetricPairSpecification := &autoscaling.PredictiveScalingPredefinedMetricPair{
PredefinedMetricType: aws.String(predefinedMetricPairSpecificationFlat["predefined_metric_type"].(string)),
ResourceLabel: aws.String(predefinedMetricPairSpecificationFlat["resource_label"].(string)),
}
return predefinedMetricPairSpecification
}

func expandPredefinedScalingMetricSpecification(predefinedScalingMetricSpecificationSlice []interface{}) *autoscaling.PredictiveScalingPredefinedScalingMetric {
if predefinedScalingMetricSpecificationSlice == nil || len(predefinedScalingMetricSpecificationSlice) < 1 {
return nil
}
predefinedScalingMetricSpecificationFlat := predefinedScalingMetricSpecificationSlice[0].(map[string]interface{})
predefinedScalingMetricSpecification := &autoscaling.PredictiveScalingPredefinedScalingMetric{
PredefinedMetricType: aws.String(predefinedScalingMetricSpecificationFlat["predefined_metric_type"].(string)),
ResourceLabel: aws.String(predefinedScalingMetricSpecificationFlat["resource_label"].(string)),
}
return predefinedScalingMetricSpecification
}

func flattenTargetTrackingConfiguration(config *autoscaling.TargetTrackingConfiguration) []interface{} {
if config == nil {
return []interface{}{}
Expand Down Expand Up @@ -521,3 +719,76 @@ func flattenTargetTrackingConfiguration(config *autoscaling.TargetTrackingConfig
}
return []interface{}{result}
}

func flattenPredictiveScalingConfig(predictiveScalingConfig *autoscaling.PredictiveScalingConfiguration) []map[string]interface{} {
predictiveScalingConfigFlat := map[string]interface{}{}
if predictiveScalingConfig == nil {
return nil
}
if predictiveScalingConfig.MetricSpecifications != nil && len(predictiveScalingConfig.MetricSpecifications) > 0 {
predictiveScalingConfigFlat["metric_specification"] = flattenPredictiveScalingMetricSpecifications(predictiveScalingConfig.MetricSpecifications)
}
if predictiveScalingConfig.Mode != nil {
predictiveScalingConfigFlat["mode"] = aws.StringValue(predictiveScalingConfig.Mode)
}
if predictiveScalingConfig.SchedulingBufferTime != nil {
predictiveScalingConfigFlat["scheduling_buffer_time"] = strconv.FormatInt(aws.Int64Value(predictiveScalingConfig.SchedulingBufferTime), 10)
}
if predictiveScalingConfig.MaxCapacityBreachBehavior != nil {
predictiveScalingConfigFlat["max_capacity_breach_behavior"] = aws.StringValue(predictiveScalingConfig.MaxCapacityBreachBehavior)
}
if predictiveScalingConfig.MaxCapacityBuffer != nil {
predictiveScalingConfigFlat["max_capacity_buffer"] = strconv.FormatInt(aws.Int64Value(predictiveScalingConfig.MaxCapacityBuffer), 10)
}
return []map[string]interface{}{predictiveScalingConfigFlat}
}

func flattenPredictiveScalingMetricSpecifications(metricSpecification []*autoscaling.PredictiveScalingMetricSpecification) []map[string]interface{} {
metricSpecificationFlat := map[string]interface{}{}
if metricSpecification == nil || len(metricSpecification) < 1 {
return []map[string]interface{}{metricSpecificationFlat}
}
if metricSpecification[0].TargetValue != nil {
metricSpecificationFlat["target_value"] = aws.Float64Value(metricSpecification[0].TargetValue)
}
if metricSpecification[0].PredefinedLoadMetricSpecification != nil {
metricSpecificationFlat["predefined_load_metric_specification"] = flattenPredefinedLoadMetricSpecification(metricSpecification[0].PredefinedLoadMetricSpecification)
}
if metricSpecification[0].PredefinedMetricPairSpecification != nil {
metricSpecificationFlat["predefined_metric_pair_specification"] = flattenPredefinedMetricPairSpecification(metricSpecification[0].PredefinedMetricPairSpecification)
}
if metricSpecification[0].PredefinedScalingMetricSpecification != nil {
metricSpecificationFlat["predefined_scaling_metric_specification"] = flattenPredefinedScalingMetricSpecification(metricSpecification[0].PredefinedScalingMetricSpecification)
}
return []map[string]interface{}{metricSpecificationFlat}
}

func flattenPredefinedScalingMetricSpecification(predefinedScalingMetricSpecification *autoscaling.PredictiveScalingPredefinedScalingMetric) []map[string]interface{} {
predefinedScalingMetricSpecificationFlat := map[string]interface{}{}
if predefinedScalingMetricSpecification == nil {
return []map[string]interface{}{predefinedScalingMetricSpecificationFlat}
}
predefinedScalingMetricSpecificationFlat["predefined_metric_type"] = aws.StringValue(predefinedScalingMetricSpecification.PredefinedMetricType)
predefinedScalingMetricSpecificationFlat["resource_label"] = aws.StringValue(predefinedScalingMetricSpecification.ResourceLabel)
return []map[string]interface{}{predefinedScalingMetricSpecificationFlat}
}

func flattenPredefinedLoadMetricSpecification(predefinedLoadMetricSpecification *autoscaling.PredictiveScalingPredefinedLoadMetric) []map[string]interface{} {
predefinedLoadMetricSpecificationFlat := map[string]interface{}{}
if predefinedLoadMetricSpecification == nil {
return []map[string]interface{}{predefinedLoadMetricSpecificationFlat}
}
predefinedLoadMetricSpecificationFlat["predefined_metric_type"] = aws.StringValue(predefinedLoadMetricSpecification.PredefinedMetricType)
predefinedLoadMetricSpecificationFlat["resource_label"] = aws.StringValue(predefinedLoadMetricSpecification.ResourceLabel)
return []map[string]interface{}{predefinedLoadMetricSpecificationFlat}
}

func flattenPredefinedMetricPairSpecification(predefinedMetricPairSpecification *autoscaling.PredictiveScalingPredefinedMetricPair) []map[string]interface{} {
predefinedMetricPairSpecificationFlat := map[string]interface{}{}
if predefinedMetricPairSpecification == nil {
return []map[string]interface{}{predefinedMetricPairSpecificationFlat}
}
predefinedMetricPairSpecificationFlat["predefined_metric_type"] = aws.StringValue(predefinedMetricPairSpecification.PredefinedMetricType)
predefinedMetricPairSpecificationFlat["resource_label"] = aws.StringValue(predefinedMetricPairSpecification.ResourceLabel)
return []map[string]interface{}{predefinedMetricPairSpecificationFlat}
}
Loading