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

resource/aws_autoscaling_policy: Support ec2 autoscaling TargetTrackingScaling policy #3160

Closed
Closed
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
257 changes: 256 additions & 1 deletion aws/resource_aws_autoscaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func resourceAwsAutoscalingPolicy() *schema.Resource {
},
"adjustment_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
},
"autoscaling_group_name": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -93,6 +93,86 @@ func resourceAwsAutoscalingPolicy() *schema.Resource {
},
Set: resourceAwsAutoscalingScalingAdjustmentHash,
},
"target_tracking_configuration": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"customized_metric_specification": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"target_tracking_configuration.0.predefined_metric_specification"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dimensions": &schema.Schema{
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": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"namespace": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"statistic": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAutoscalingCustomizedMetricSpecificationStatistic,
},
"unit": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
"predefined_metric_specification": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"target_tracking_configuration.0.customized_metric_specification"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"predefined_metric_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAutoscalingPredefinedMetricSpecificationPredefinedMetricType,
},
"resource_label": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
"disable_scale_in": &schema.Schema{
Type: schema.TypeBool,
Default: false,
Optional: true,
},
"target_value": &schema.Schema{
Type: schema.TypeFloat,
Required: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -147,6 +227,7 @@ func resourceAwsAutoscalingPolicyRead(d *schema.ResourceData, meta interface{})
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))

return nil
}
Expand Down Expand Up @@ -233,6 +314,10 @@ func getAwsAutoscalingPutScalingPolicyInput(d *schema.ResourceData) (autoscaling
params.StepAdjustments = steps
}

if v, ok := d.GetOk("target_tracking_configuration"); ok {
params.TargetTrackingConfiguration = expandTargetTrackingConfiguration(v.([]interface{}))
}

if v, ok := d.GetOk("min_adjustment_magnitude"); ok {
// params.MinAdjustmentMagnitude = aws.Int64(int64(d.Get("min_adjustment_magnitude").(int)))
params.MinAdjustmentMagnitude = aws.Int64(int64(v.(int)))
Expand All @@ -252,6 +337,9 @@ func getAwsAutoscalingPutScalingPolicyInput(d *schema.ResourceData) (autoscaling
if *params.PolicyType == "SimpleScaling" && params.EstimatedInstanceWarmup != nil {
return params, fmt.Errorf("SimpleScaling policy types cannot use estimated_instance_warmup!")
}
if *params.PolicyType == "SimpleScaling" && params.TargetTrackingConfiguration != nil {
return params, fmt.Errorf("SimpleScaling policy types cannot use target_tracking_configuration!")
}

// Second, StepScaling policy types...
if *params.PolicyType == "StepScaling" && params.ScalingAdjustment != nil {
Expand All @@ -260,6 +348,29 @@ func getAwsAutoscalingPutScalingPolicyInput(d *schema.ResourceData) (autoscaling
if *params.PolicyType == "StepScaling" && params.Cooldown != nil {
return params, fmt.Errorf("StepScaling policy types cannot use cooldown!")
}
if *params.PolicyType == "StepScaling" && params.TargetTrackingConfiguration != nil {
return params, fmt.Errorf("StepScaling policy types cannot use target_tracking_configuration!")
}

// Third, TargetTrackingScaling policy types...
if *params.PolicyType == "TargetTrackingScaling" && params.AdjustmentType != nil {
return params, fmt.Errorf("TargetTrackingScaling policy types cannot use adjustment_type!")
}
if *params.PolicyType == "TargetTrackingScaling" && params.ScalingAdjustment != nil {
return params, fmt.Errorf("TargetTrackingScaling policy types cannot use scaling_adjustment!")
}
if *params.PolicyType == "TargetTrackingScaling" && params.Cooldown != nil {
return params, fmt.Errorf("TargetTrackingScaling policy types cannot use cooldown!")
}
if *params.PolicyType == "TargetTrackingScaling" && params.MetricAggregationType != nil {
return params, fmt.Errorf("TargetTrackingScaling policy types cannot use metric_aggregation_type!")
}
if *params.PolicyType == "TargetTrackingScaling" && params.MinAdjustmentStep != nil {
return params, fmt.Errorf("TargetTrackingScaling policy types cannot use min_adjustment_step!")
}
if *params.PolicyType == "TargetTrackingScaling" && params.StepAdjustments != nil {
return params, fmt.Errorf("TargetTrackingScaling policy types cannot use step_adjustments!")
}

return params, nil
}
Expand Down Expand Up @@ -297,6 +408,150 @@ func getAwsAutoscalingPolicy(d *schema.ResourceData, meta interface{}) (*autosca
return nil, nil
}

func expandTargetTrackingConfiguration(cfg []interface{}) *autoscaling.TargetTrackingConfiguration {
if len(cfg) < 1 {
return nil
}

m := cfg[0].(map[string]interface{})
out := &autoscaling.TargetTrackingConfiguration{
TargetValue: aws.Float64(m["target_value"].(float64)),
}

if v, ok := m["disable_scale_in"]; ok {
out.DisableScaleIn = aws.Bool(v.(bool))
}

if v, ok := m["customized_metric_specification"].([]interface{}); ok && len(v) > 0 {
out.CustomizedMetricSpecification = expandAutoscalingCustomizedMetricSpecification(v)
}

if v, ok := m["predefined_metric_specification"].([]interface{}); ok && len(v) > 0 {
out.PredefinedMetricSpecification = expandAutoscalingPredefinedMetricSpecification(v)
}

return out
}

func expandAutoscalingCustomizedMetricSpecification(configured []interface{}) *autoscaling.CustomizedMetricSpecification {
spec := &autoscaling.CustomizedMetricSpecification{}

for _, raw := range configured {
data := raw.(map[string]interface{})
if v, ok := data["metric_name"]; ok {
spec.MetricName = aws.String(v.(string))
}

if v, ok := data["namespace"]; ok {
spec.Namespace = aws.String(v.(string))
}

if v, ok := data["unit"].(string); ok && v != "" {
spec.Unit = aws.String(v)
}

if v, ok := data["statistic"]; ok {
spec.Statistic = aws.String(v.(string))
}

if s, ok := data["dimensions"].(*schema.Set); ok && s.Len() > 0 {
dimensions := make([]*autoscaling.MetricDimension, s.Len(), s.Len())
for i, d := range s.List() {
dimension := d.(map[string]interface{})
dimensions[i] = &autoscaling.MetricDimension{
Name: aws.String(dimension["name"].(string)),
Value: aws.String(dimension["value"].(string)),
}
}
spec.Dimensions = dimensions
}
}
return spec
}

func expandAutoscalingPredefinedMetricSpecification(configured []interface{}) *autoscaling.PredefinedMetricSpecification {
spec := &autoscaling.PredefinedMetricSpecification{}

for _, raw := range configured {
data := raw.(map[string]interface{})

if v, ok := data["predefined_metric_type"]; ok {
spec.PredefinedMetricType = aws.String(v.(string))
}

if v, ok := data["resource_label"].(string); ok && v != "" {
spec.ResourceLabel = aws.String(v)
}
}
return spec
}

func flattenTargetTrackingConfiguration(cfg *autoscaling.TargetTrackingConfiguration) []interface{} {
if cfg == nil {
return []interface{}{}
}

m := make(map[string]interface{}, 0)
m["target_value"] = *cfg.TargetValue

if cfg.DisableScaleIn != nil {
m["disable_scale_in"] = *cfg.DisableScaleIn
}
if cfg.CustomizedMetricSpecification != nil {
m["customized_metric_specification"] = ec2FlattenCustomizedMetricSpecification(cfg.CustomizedMetricSpecification)
}
if cfg.PredefinedMetricSpecification != nil {
m["predefined_metric_specification"] = ec2FlattenPredefinedMetricSpecification(cfg.PredefinedMetricSpecification)
}

return []interface{}{m}
}

func ec2FlattenCustomizedMetricSpecification(cfg *autoscaling.CustomizedMetricSpecification) []interface{} {
if cfg == nil {
return []interface{}{}
}

m := map[string]interface{}{
"metric_name": *cfg.MetricName,
"namespace": *cfg.Namespace,
"statistic": *cfg.Statistic,
}

if len(cfg.Dimensions) > 0 {
m["dimensions"] = ec2FlattenMetricDimensions(cfg.Dimensions)
}

if cfg.Unit != nil {
m["unit"] = *cfg.Unit
}
return []interface{}{m}
}

func ec2FlattenMetricDimensions(ds []*autoscaling.MetricDimension) []interface{} {
l := make([]interface{}, len(ds), len(ds))
for i, d := range ds {
l[i] = map[string]interface{}{
"name": *d.Name,
"value": *d.Value,
}
}
return l
}

func ec2FlattenPredefinedMetricSpecification(cfg *autoscaling.PredefinedMetricSpecification) []interface{} {
if cfg == nil {
return []interface{}{}
}
m := map[string]interface{}{
"predefined_metric_type": *cfg.PredefinedMetricType,
}
if cfg.ResourceLabel != nil {
m["resource_label"] = *cfg.ResourceLabel
}
return []interface{}{m}
}

func resourceAwsAutoscalingScalingAdjustmentHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
Expand Down
52 changes: 50 additions & 2 deletions aws/resource_aws_autoscaling_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func TestAccAWSAutoscalingPolicy_basic(t *testing.T) {
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "metric_aggregation_type", "Minimum"),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "estimated_instance_warmup", "200"),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "autoscaling_group_name", name),
testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_targettracking", &policy),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_targettracking", "policy_type", "TargetTrackingScaling"),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_targettracking", "name", "foobar_targettracking"),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_targettracking", "autoscaling_group_name", name),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_targettracking", "estimated_instance_warmup", "200"),
),
},
},
Expand Down Expand Up @@ -124,6 +129,8 @@ func TestAccAWSAutoscalingPolicy_upgrade(t *testing.T) {
testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_step", "0"),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_magnitude", "1"),
testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_targettracking", &policy),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_targettracking", "estimated_instance_warmup", "100"),
),
ExpectNonEmptyPlan: true,
},
Expand All @@ -134,6 +141,8 @@ func TestAccAWSAutoscalingPolicy_upgrade(t *testing.T) {
testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_step", "0"),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_magnitude", "1"),
testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_targettracking", &policy),
resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_targettracking", "estimated_instance_warmup", "100"),
),
},
},
Expand Down Expand Up @@ -238,6 +247,19 @@ resource "aws_autoscaling_policy" "foobar_step" {
}
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
}

resource "aws_autoscaling_policy" "foobar_targettracking" {
name = "foobar_targettracking"
policy_type = "TargetTrackingScaling"
estimated_instance_warmup = 200
target_tracking_configuration {
predefined_metric_specification {
predefined_metric_type = "ASGAverageCPUUtilization"
}
target_value = 20
}
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
}
`, name, name)
}

Expand Down Expand Up @@ -276,7 +298,20 @@ resource "aws_autoscaling_policy" "foobar_simple" {
min_adjustment_step = 1
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
}
`, name, name, name)

resource "aws_autoscaling_policy" "foobar_targettracking" {
name = "foobar_targettracking_%s"
policy_type = "TargetTrackingScaling"
estimated_instance_warmup = 100
target_tracking_configuration {
predefined_metric_specification {
predefined_metric_type = "ASGAverageCPUUtilization"
}
target_value = 30
}
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
}
`, name, name, name, name)
}

func testAccAWSAutoscalingPolicyConfig_upgrade_615(name string) string {
Expand Down Expand Up @@ -314,7 +349,20 @@ resource "aws_autoscaling_policy" "foobar_simple" {
min_adjustment_magnitude = 1
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
}
`, name, name, name)

resource "aws_autoscaling_policy" "foobar_targettracking" {
name = "foobar_targettracking_%s"
policy_type = "TargetTrackingScaling"
estimated_instance_warmup = 100
target_tracking_configuration {
predefined_metric_specification {
predefined_metric_type = "ASGAverageCPUUtilization"
}
target_value = 30
}
autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
}
`, name, name, name, name)
}

func TestAccAWSAutoscalingPolicy_SimpleScalingStepAdjustment(t *testing.T) {
Expand Down
Loading