diff --git a/aws/resource_aws_appautoscaling_policy.go b/aws/resource_aws_appautoscaling_policy.go index 673026f5a29..d90d2fa259f 100644 --- a/aws/resource_aws_appautoscaling_policy.go +++ b/aws/resource_aws_appautoscaling_policy.go @@ -270,7 +270,7 @@ func resourceAwsAppautoscalingPolicyCreate(d *schema.ResourceData, meta interfac return nil }) if err != nil { - return err + return fmt.Errorf("Failed to create scaling policy: %s", err) } d.Set("arn", resp.PolicyARN) @@ -317,7 +317,7 @@ func resourceAwsAppautoscalingPolicyUpdate(d *schema.ResourceData, meta interfac log.Printf("[DEBUG] Application Autoscaling Update Scaling Policy: %#v", params) _, err := conn.PutScalingPolicy(¶ms) if err != nil { - return err + return fmt.Errorf("Failed to update scaling policy: %s", err) } return resourceAwsAppautoscalingPolicyRead(d, meta) @@ -341,7 +341,7 @@ func resourceAwsAppautoscalingPolicyDelete(d *schema.ResourceData, meta interfac } log.Printf("[DEBUG] Deleting Application AutoScaling Policy opts: %#v", params) if _, err := conn.DeleteScalingPolicy(¶ms); err != nil { - return fmt.Errorf("Application AutoScaling Policy: %s", err) + return fmt.Errorf("Failed to delete autoscaling policy: %s", err) } d.SetId("") @@ -571,14 +571,14 @@ func getAwsAppautoscalingPolicy(d *schema.ResourceData, meta interface{}) (*appl } // find scaling policy - name := d.Get("name") + name := d.Get("name").(string) + dimension := d.Get("scalable_dimension").(string) for idx, sp := range resp.ScalingPolicies { - if *sp.PolicyName == name { + if *sp.PolicyName == name && *sp.ScalableDimension == dimension { return resp.ScalingPolicies[idx], nil } } - // policy not found return nil, nil } diff --git a/aws/resource_aws_appautoscaling_policy_test.go b/aws/resource_aws_appautoscaling_policy_test.go index 440e398427b..b039a0a6f75 100644 --- a/aws/resource_aws_appautoscaling_policy_test.go +++ b/aws/resource_aws_appautoscaling_policy_test.go @@ -122,6 +122,36 @@ func TestAccAWSAppautoScalingPolicy_dynamoDb(t *testing.T) { }) } +func TestAccAWSAppautoScalingPolicy_multiplePolicies(t *testing.T) { + var readPolicy applicationautoscaling.ScalingPolicy + var writePolicy applicationautoscaling.ScalingPolicy + + tableName := 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_multiplePolicies(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", "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", "service_namespace", "dynamodb"), + resource.TestCheckResourceAttr("aws_appautoscaling_policy.write", "scalable_dimension", "dynamodb:table:WriteCapacityUnits"), + ), + }, + }, + }) +} + func testAccCheckAWSAppautoscalingPolicyExists(n string, policy *applicationautoscaling.ScalingPolicy) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -525,3 +555,113 @@ resource "aws_appautoscaling_policy" "dynamo_test" { } `, randPolicyName, randPolicyName) } + +func testAccAWSAppautoscalingPolicy_multiplePolicies(tableName, namePrefix string) string { + return fmt.Sprintf(` +resource "aws_dynamodb_table" "dynamodb_table_test" { + name = "%s" + read_capacity = 5 + write_capacity = 5 + hash_key = "FooKey" + attribute { + name = "FooKey" + type = "S" + } +} + +resource "aws_iam_role" "autoscale_role" { + assume_role_policy = <