Skip to content

Commit

Permalink
Merge pull request #3012 from terraform-providers/b-aws_appautoscalin…
Browse files Browse the repository at this point in the history
…g_policy-precise-get

resource/aws_appautoscaling_policy: Match correct policy when multiple policies with same name
  • Loading branch information
bflad authored Jan 17, 2018
2 parents d0b1d7f + 7e036c1 commit ef8f776
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 14 deletions.
18 changes: 7 additions & 11 deletions aws/resource_aws_appautoscaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,26 +567,22 @@ func getAwsAppautoscalingPolicy(d *schema.ResourceData, meta interface{}) (*appl
conn := meta.(*AWSClient).appautoscalingconn

params := applicationautoscaling.DescribeScalingPoliciesInput{
PolicyNames: []*string{aws.String(d.Get("name").(string))},
ServiceNamespace: aws.String(d.Get("service_namespace").(string)),
PolicyNames: []*string{aws.String(d.Get("name").(string))},
ResourceId: aws.String(d.Get("resource_id").(string)),
ScalableDimension: aws.String(d.Get("scalable_dimension").(string)),
ServiceNamespace: aws.String(d.Get("service_namespace").(string)),
}

log.Printf("[DEBUG] Application AutoScaling Policy Describe Params: %#v", params)
resp, err := conn.DescribeScalingPolicies(&params)
if err != nil {
return nil, fmt.Errorf("Error retrieving scaling policies: %s", err)
}

// find scaling policy
name := d.Get("name").(string)
dimension := d.Get("scalable_dimension").(string)
for idx, sp := range resp.ScalingPolicies {
if *sp.PolicyName == name && *sp.ScalableDimension == dimension {
return resp.ScalingPolicies[idx], nil
}
if len(resp.ScalingPolicies) == 0 {
return nil, nil
}

return nil, nil
return resp.ScalingPolicies[0], nil
}

func expandStepScalingPolicyConfiguration(cfg []interface{}) *applicationautoscaling.StepScalingPolicyConfiguration {
Expand Down
117 changes: 114 additions & 3 deletions aws/resource_aws_appautoscaling_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,40 @@ func TestAccAWSAppautoScalingPolicy_dynamoDb(t *testing.T) {
})
}

func TestAccAWSAppautoScalingPolicy_multiplePolicies(t *testing.T) {
func TestAccAWSAppautoScalingPolicy_multiplePoliciesSameName(t *testing.T) {
var readPolicy1 applicationautoscaling.ScalingPolicy
var readPolicy2 applicationautoscaling.ScalingPolicy

tableName1 := fmt.Sprintf("tf-autoscaled-table-%s", acctest.RandString(5))
tableName2 := fmt.Sprintf("tf-autoscaled-table-%s", acctest.RandString(5))
namePrefix := fmt.Sprintf("tf-appautoscaling-policy-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAppautoscalingPolicyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAppautoscalingPolicy_multiplePoliciesSameName(tableName1, tableName2, namePrefix),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.read1", &readPolicy1),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read1", "name", namePrefix+"-read"),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read1", "resource_id", "table/"+tableName1),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read1", "service_namespace", "dynamodb"),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read1", "scalable_dimension", "dynamodb:table:ReadCapacityUnits"),

testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.read2", &readPolicy2),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read2", "name", namePrefix+"-read"),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read2", "resource_id", "table/"+tableName2),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read2", "service_namespace", "dynamodb"),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read2", "scalable_dimension", "dynamodb:table:ReadCapacityUnits"),
),
},
},
})
}

func TestAccAWSAppautoScalingPolicy_multiplePoliciesSameResource(t *testing.T) {
var readPolicy applicationautoscaling.ScalingPolicy
var writePolicy applicationautoscaling.ScalingPolicy

Expand All @@ -135,15 +168,17 @@ func TestAccAWSAppautoScalingPolicy_multiplePolicies(t *testing.T) {
CheckDestroy: testAccCheckAWSAppautoscalingPolicyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAppautoscalingPolicy_multiplePolicies(tableName, namePrefix),
Config: testAccAWSAppautoscalingPolicy_multiplePoliciesSameResource(tableName, namePrefix),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.read", &readPolicy),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read", "name", namePrefix+"-read"),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read", "resource_id", "table/"+tableName),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read", "service_namespace", "dynamodb"),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.read", "scalable_dimension", "dynamodb:table:ReadCapacityUnits"),

testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.write", &writePolicy),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.write", "name", namePrefix+"-write"),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.write", "resource_id", "table/"+tableName),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.write", "service_namespace", "dynamodb"),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.write", "scalable_dimension", "dynamodb:table:WriteCapacityUnits"),
),
Expand Down Expand Up @@ -427,7 +462,83 @@ resource "aws_appautoscaling_policy" "dynamo_test" {
`, randPolicyName, randPolicyName)
}

func testAccAWSAppautoscalingPolicy_multiplePolicies(tableName, namePrefix string) string {
func testAccAWSAppautoscalingPolicy_multiplePoliciesSameName(tableName1, tableName2, namePrefix string) string {
return fmt.Sprintf(`
resource "aws_dynamodb_table" "dynamodb_table_test1" {
name = "%[1]s"
read_capacity = 1
write_capacity = 1
hash_key = "FooKey"
attribute {
name = "FooKey"
type = "S"
}
}
resource "aws_dynamodb_table" "dynamodb_table_test2" {
name = "%[2]s"
read_capacity = 1
write_capacity = 1
hash_key = "FooKey"
attribute {
name = "FooKey"
type = "S"
}
}
resource "aws_appautoscaling_target" "read1" {
service_namespace = "dynamodb"
resource_id = "table/${aws_dynamodb_table.dynamodb_table_test1.name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
min_capacity = 1
max_capacity = 10
}
resource "aws_appautoscaling_policy" "read1" {
name = "%[3]s-read"
policy_type = "TargetTrackingScaling"
service_namespace = "dynamodb"
resource_id = "${aws_appautoscaling_target.read1.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.read1.scalable_dimension}"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
scale_in_cooldown = 10
scale_out_cooldown = 10
target_value = 70
}
}
resource "aws_appautoscaling_target" "read2" {
service_namespace = "dynamodb"
resource_id = "table/${aws_dynamodb_table.dynamodb_table_test2.name}"
scalable_dimension = "dynamodb:table:ReadCapacityUnits"
min_capacity = 1
max_capacity = 10
}
resource "aws_appautoscaling_policy" "read2" {
name = "%[3]s-read"
policy_type = "TargetTrackingScaling"
service_namespace = "dynamodb"
resource_id = "table/${aws_dynamodb_table.dynamodb_table_test2.name}"
scalable_dimension = "${aws_appautoscaling_target.read2.scalable_dimension}"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "DynamoDBReadCapacityUtilization"
}
scale_in_cooldown = 10
scale_out_cooldown = 10
target_value = 70
}
}
`, tableName1, tableName2, namePrefix)
}

func testAccAWSAppautoscalingPolicy_multiplePoliciesSameResource(tableName, namePrefix string) string {
return fmt.Sprintf(`
resource "aws_dynamodb_table" "dynamodb_table_test" {
name = "%s"
Expand Down

0 comments on commit ef8f776

Please sign in to comment.