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

r/aws_autoscaling_policy: Support metric math for target tracking policy #28560

Merged
Show file tree
Hide file tree
Changes from 1 commit
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/28560.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_autoscaling_policy: Support metric math for target tracking
```
277 changes: 234 additions & 43 deletions internal/service/autoscaling/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,90 @@ func ResourcePolicy() *schema.Resource {
ConflictsWith: []string{"target_tracking_configuration.0.predefined_metric_specification"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"metrics": {
Type: schema.TypeSet,
Optional: true,
ConflictsWith: []string{"target_tracking_configuration.0.customized_metric_specification.0.metric_dimension", "target_tracking_configuration.0.customized_metric_specification.0.metric_name", "target_tracking_configuration.0.customized_metric_specification.0.namespace", "target_tracking_configuration.0.customized_metric_specification.0.statistic", "target_tracking_configuration.0.customized_metric_specification.0.unit"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"expression": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 2047),
},
"id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
"label": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 2047),
},
"metric_stat": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"metric": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dimensions": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"metric_name": {
Type: schema.TypeString,
Required: true,
},
"namespace": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"stat": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
},
"unit": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"return_data": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
},
},
"metric_dimension": {
Type: schema.TypeList,
Optional: true,
Type: schema.TypeList,
Optional: true,
ConflictsWith: []string{"target_tracking_configuration.0.customized_metric_specification.0.metrics"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -276,20 +357,24 @@ func ResourcePolicy() *schema.Resource {
},
},
"metric_name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"target_tracking_configuration.0.customized_metric_specification.0.metrics"},
},
"namespace": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"target_tracking_configuration.0.customized_metric_specification.0.metrics"},
},
"statistic": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"target_tracking_configuration.0.customized_metric_specification.0.metrics"},
},
"unit": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"target_tracking_configuration.0.customized_metric_specification.0.metrics"},
},
},
},
Expand Down Expand Up @@ -709,32 +794,89 @@ func expandTargetTrackingConfiguration(configs []interface{}) *autoscaling.Targe
}
if v, ok := config["customized_metric_specification"]; ok && len(v.([]interface{})) > 0 {
spec := v.([]interface{})[0].(map[string]interface{})
customSpec := &autoscaling.CustomizedMetricSpecification{
Namespace: aws.String(spec["namespace"].(string)),
MetricName: aws.String(spec["metric_name"].(string)),
Statistic: aws.String(spec["statistic"].(string)),
}
if val, ok := spec["unit"]; ok && len(val.(string)) > 0 {
customSpec.Unit = aws.String(val.(string))
}
if val, ok := spec["metric_dimension"]; ok {
dims := val.([]interface{})
metDimList := make([]*autoscaling.MetricDimension, len(dims))
for i := range metDimList {
dim := dims[i].(map[string]interface{})
md := &autoscaling.MetricDimension{
Name: aws.String(dim["name"].(string)),
Value: aws.String(dim["value"].(string)),
customSpec := &autoscaling.CustomizedMetricSpecification{}
if val, ok := spec["metrics"].(*schema.Set); ok && val.Len() > 0 {
customSpec.Metrics = expandTargetTrackingMetricDataQueries(val.List())
} else {
customSpec.Namespace = aws.String(spec["namespace"].(string))
customSpec.MetricName = aws.String(spec["metric_name"].(string))
customSpec.Statistic = aws.String(spec["statistic"].(string))
if val, ok := spec["unit"]; ok && len(val.(string)) > 0 {
customSpec.Unit = aws.String(val.(string))
}
if val, ok := spec["metric_dimension"]; ok {
dims := val.([]interface{})
metDimList := make([]*autoscaling.MetricDimension, len(dims))
for i := range metDimList {
dim := dims[i].(map[string]interface{})
md := &autoscaling.MetricDimension{
Name: aws.String(dim["name"].(string)),
Value: aws.String(dim["value"].(string)),
}
metDimList[i] = md
}
metDimList[i] = md
customSpec.Dimensions = metDimList
}
customSpec.Dimensions = metDimList
}
result.CustomizedMetricSpecification = customSpec
}
return result
}

func expandTargetTrackingMetricDataQueries(metricDataQuerySlices []interface{}) []*autoscaling.TargetTrackingMetricDataQuery {
if metricDataQuerySlices == nil || len(metricDataQuerySlices) < 1 {
return nil
}
metricDataQueries := make([]*autoscaling.TargetTrackingMetricDataQuery, len(metricDataQuerySlices))

for i := range metricDataQueries {
metricDataQueryFlat := metricDataQuerySlices[i].(map[string]interface{})
metricDataQuery := &autoscaling.TargetTrackingMetricDataQuery{
Id: aws.String(metricDataQueryFlat["id"].(string)),
}
if val, ok := metricDataQueryFlat["metric_stat"]; ok && len(val.([]interface{})) > 0 {
metricStatSpec := val.([]interface{})[0].(map[string]interface{})
metricSpec := metricStatSpec["metric"].([]interface{})[0].(map[string]interface{})
metric := &autoscaling.Metric{
MetricName: aws.String(metricSpec["metric_name"].(string)),
Namespace: aws.String(metricSpec["namespace"].(string)),
}
if v, ok := metricSpec["dimensions"]; ok {
dims := v.(*schema.Set).List()
dimList := make([]*autoscaling.MetricDimension, len(dims))
for i := range dimList {
dim := dims[i].(map[string]interface{})
md := &autoscaling.MetricDimension{
Name: aws.String(dim["name"].(string)),
Value: aws.String(dim["value"].(string)),
}
dimList[i] = md
}
metric.Dimensions = dimList
}
metricStat := &autoscaling.TargetTrackingMetricStat{
Metric: metric,
Stat: aws.String(metricStatSpec["stat"].(string)),
}
if v, ok := metricStatSpec["unit"]; ok && len(v.(string)) > 0 {
metricStat.Unit = aws.String(v.(string))
}
metricDataQuery.MetricStat = metricStat
}
if val, ok := metricDataQueryFlat["expression"]; ok && val.(string) != "" {
metricDataQuery.Expression = aws.String(val.(string))
}
if val, ok := metricDataQueryFlat["label"]; ok && val.(string) != "" {
metricDataQuery.Label = aws.String(val.(string))
}
if val, ok := metricDataQueryFlat["return_data"]; ok {
metricDataQuery.ReturnData = aws.Bool(val.(bool))
}
metricDataQueries[i] = metricDataQuery
}
return metricDataQueries
}

func expandPredictiveScalingConfig(predictiveScalingConfigSlice []interface{}) *autoscaling.PredictiveScalingConfiguration {
if predictiveScalingConfigSlice == nil || len(predictiveScalingConfigSlice) < 1 {
return nil
Expand Down Expand Up @@ -912,28 +1054,77 @@ func flattenTargetTrackingConfiguration(config *autoscaling.TargetTrackingConfig
}
if config.CustomizedMetricSpecification != nil {
spec := map[string]interface{}{}
spec["metric_name"] = aws.StringValue(config.CustomizedMetricSpecification.MetricName)
spec["namespace"] = aws.StringValue(config.CustomizedMetricSpecification.Namespace)
spec["statistic"] = aws.StringValue(config.CustomizedMetricSpecification.Statistic)
if config.CustomizedMetricSpecification.Unit != nil {
spec["unit"] = aws.StringValue(config.CustomizedMetricSpecification.Unit)
}
if config.CustomizedMetricSpecification.Dimensions != nil {
dimSpec := make([]interface{}, len(config.CustomizedMetricSpecification.Dimensions))
for i := range dimSpec {
dim := map[string]interface{}{}
rawDim := config.CustomizedMetricSpecification.Dimensions[i]
dim["name"] = aws.StringValue(rawDim.Name)
dim["value"] = aws.StringValue(rawDim.Value)
dimSpec[i] = dim
if config.CustomizedMetricSpecification.Metrics != nil {
spec["metrics"] = flattenTargetTrackingMetricDataQueries(config.CustomizedMetricSpecification.Metrics)
} else {
spec["metric_name"] = aws.StringValue(config.CustomizedMetricSpecification.MetricName)
spec["namespace"] = aws.StringValue(config.CustomizedMetricSpecification.Namespace)
spec["statistic"] = aws.StringValue(config.CustomizedMetricSpecification.Statistic)
if config.CustomizedMetricSpecification.Unit != nil {
spec["unit"] = aws.StringValue(config.CustomizedMetricSpecification.Unit)
}
if config.CustomizedMetricSpecification.Dimensions != nil {
dimSpec := make([]interface{}, len(config.CustomizedMetricSpecification.Dimensions))
for i := range dimSpec {
dim := map[string]interface{}{}
rawDim := config.CustomizedMetricSpecification.Dimensions[i]
dim["name"] = aws.StringValue(rawDim.Name)
dim["value"] = aws.StringValue(rawDim.Value)
dimSpec[i] = dim
}
spec["metric_dimension"] = dimSpec
}
spec["metric_dimension"] = dimSpec
}
result["customized_metric_specification"] = []map[string]interface{}{spec}
}
return []interface{}{result}
}

func flattenTargetTrackingMetricDataQueries(metricDataQueries []*autoscaling.TargetTrackingMetricDataQuery) []interface{} {
metricDataQueriesSpec := make([]interface{}, len(metricDataQueries))
for i := range metricDataQueriesSpec {
metricDataQuery := map[string]interface{}{}
rawMetricDataQuery := metricDataQueries[i]
metricDataQuery["id"] = aws.StringValue(rawMetricDataQuery.Id)
if rawMetricDataQuery.Expression != nil {
metricDataQuery["expression"] = aws.StringValue(rawMetricDataQuery.Expression)
}
if rawMetricDataQuery.Label != nil {
metricDataQuery["label"] = aws.StringValue(rawMetricDataQuery.Label)
}
if rawMetricDataQuery.MetricStat != nil {
metricStatSpec := map[string]interface{}{}
rawMetricStat := rawMetricDataQuery.MetricStat
rawMetric := rawMetricStat.Metric
metricSpec := map[string]interface{}{}
if rawMetric.Dimensions != nil {
dimSpec := make([]interface{}, len(rawMetric.Dimensions))
for i := range dimSpec {
dim := map[string]interface{}{}
rawDim := rawMetric.Dimensions[i]
dim["name"] = aws.StringValue(rawDim.Name)
dim["value"] = aws.StringValue(rawDim.Value)
dimSpec[i] = dim
}
metricSpec["dimensions"] = dimSpec
}
metricSpec["metric_name"] = aws.StringValue(rawMetric.MetricName)
metricSpec["namespace"] = aws.StringValue(rawMetric.Namespace)
metricStatSpec["metric"] = []map[string]interface{}{metricSpec}
metricStatSpec["stat"] = aws.StringValue(rawMetricStat.Stat)
if rawMetricStat.Unit != nil {
metricStatSpec["unit"] = aws.StringValue(rawMetricStat.Unit)
}
metricDataQuery["metric_stat"] = []map[string]interface{}{metricStatSpec}
}
if rawMetricDataQuery.ReturnData != nil {
metricDataQuery["return_data"] = aws.BoolValue(rawMetricDataQuery.ReturnData)
}
metricDataQueriesSpec[i] = metricDataQuery
}
return metricDataQueriesSpec
}

func flattenPredictiveScalingConfig(predictiveScalingConfig *autoscaling.PredictiveScalingConfiguration) []map[string]interface{} {
predictiveScalingConfigFlat := map[string]interface{}{}
if predictiveScalingConfig == nil {
Expand Down
Loading