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

fix for dynamodb autoscaling policy import #8397

Merged
48 changes: 36 additions & 12 deletions aws/resource_aws_appautoscaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"strconv"
"time"

"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/applicationautoscaling"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"strings"
)

func resourceAwsAppautoscalingPolicy() *schema.Resource {
Expand Down Expand Up @@ -415,30 +416,53 @@ func resourceAwsAppautoscalingPolicyDelete(d *schema.ResourceData, meta interfac
}

func resourceAwsAppautoscalingPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")

if len(idParts) < 4 {
idParts, err := validateAppautoscalingPolicyImportInput(d.Id())
if err != nil {
return nil, fmt.Errorf("unexpected format (%q), expected <service-namespace>/<resource-id>/<scalable-dimension>/<policy-name>", d.Id())
}

serviceNamespace := idParts[0]
resourceId := strings.Join(idParts[1:len(idParts)-2], "/")
scalableDimension := idParts[len(idParts)-2]
policyName := idParts[len(idParts)-1]

if serviceNamespace == "" || resourceId == "" || scalableDimension == "" || policyName == "" {
return nil, fmt.Errorf("unexpected format (%q), expected <service-namespace>/<resource-id>/<scalable-dimension>/<policy-name>", d.Id())
}
resourceId := idParts[1]
scalableDimension := idParts[2]
policyName := idParts[3]

d.Set("service_namespace", serviceNamespace)
d.Set("resource_id", resourceId)
d.Set("scalable_dimension", scalableDimension)
d.Set("name", policyName)
d.SetId(policyName)

return []*schema.ResourceData{d}, nil
}

func validateAppautoscalingPolicyImportInput(id string) ([]string, error) {

idParts := strings.Split(id, "/")
if len(idParts) < 4 {
return nil, fmt.Errorf("unexpected format (%q), expected <service-namespace>/<resource-id>/<scalable-dimension>/<policy-name>", id)
}

var serviceNamespace, resourceId, scalableDimension, policyName string
switch idParts[0] {
nywilken marked this conversation as resolved.
Show resolved Hide resolved
case "dynamodb":
serviceNamespace = idParts[0]
resourceId = strings.Join(idParts[1:3], "/")
scalableDimension = idParts[3]
policyName = strings.Join(idParts[4:], "/")
default:
serviceNamespace = idParts[0]
resourceId = strings.Join(idParts[1:len(idParts)-2], "/")
scalableDimension = idParts[len(idParts)-2]
policyName = idParts[len(idParts)-1]

}

if serviceNamespace == "" || resourceId == "" || scalableDimension == "" || policyName == "" {
return nil, fmt.Errorf("unexpected format (%q), expected <service-namespace>/<resource-id>/<scalable-dimension>/<policy-name>", id)
}

return []string{serviceNamespace, resourceId, scalableDimension, policyName}, nil
}

// Takes the result of flatmap.Expand for an array of step adjustments and
// returns a []*applicationautoscaling.StepAdjustment.
func expandAppautoscalingStepAdjustments(configured []interface{}) ([]*applicationautoscaling.StepAdjustment, error) {
Expand Down
73 changes: 67 additions & 6 deletions aws/resource_aws_appautoscaling_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package aws

import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -11,6 +13,64 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestValidateAppautoscalingPolicyImportInput(t *testing.T) {
testCases := []struct {
input string
errorExpected bool
expected []string
}{
{
input: "appstream/fleet/sample-fleet/appstream:fleet:DesiredCapacity/test-policy-name",
expected: []string{"appstream", "fleet/sample-fleet", "appstream:fleet:DesiredCapacity", "test-policy-name"},
errorExpected: false,
},
{
input: "dynamodb/table/tableName/dynamodb:table:ReadCapacityUnits/DynamoDBReadCapacityUtilization:table/tableName",
expected: []string{"dynamodb", "table/tableName", "dynamodb:table:ReadCapacityUnits", "DynamoDBReadCapacityUtilization:table/tableName"},
errorExpected: false,
},
{
input: "ec2/spot-fleet-request/sfr-d77c6508-1c1d-4e79-8789-fc019ee44c96/ec2:spot-fleet-request:TargetCapacity/test-appautoscaling-policy-ruuhd",
expected: []string{"ec2", "spot-fleet-request/sfr-d77c6508-1c1d-4e79-8789-fc019ee44c96", "ec2:spot-fleet-request:TargetCapacity", "test-appautoscaling-policy-ruuhd"},
errorExpected: false,
},
{
input: "ecs/service/clusterName/serviceName/ecs:service:DesiredCount/scale-down",
expected: []string{"ecs", "service/clusterName/serviceName", "ecs:service:DesiredCount", "scale-down"},
errorExpected: false,
},
{
input: "elasticmapreduce/instancegroup/j-2EEZNYKUA1NTV/ig-1791Y4E1L8YI0/elasticmapreduce:instancegroup:InstanceCount/test-appautoscaling-policy-ruuhd",
expected: []string{"elasticmapreduce", "instancegroup/j-2EEZNYKUA1NTV/ig-1791Y4E1L8YI0", "elasticmapreduce:instancegroup:InstanceCount", "test-appautoscaling-policy-ruuhd"},
errorExpected: false,
},
{
input: "rds/cluster:id/rds:cluster:ReadReplicaCount/cpu-auto-scaling",
expected: []string{"rds", "cluster:id", "rds:cluster:ReadReplicaCount", "cpu-auto-scaling"},
errorExpected: false,
},
{
input: "dynamodb/missing/parts",
errorExpected: true,
},
}

for _, tc := range testCases {
idParts, err := validateAppautoscalingPolicyImportInput(tc.input)
if tc.errorExpected == false && err != nil {
t.Errorf("validateAppautoscalingPolicyImportInput(%q): resulted in an unexpected error: %s", tc.input, err)
}

if tc.errorExpected == true && err == nil {
t.Errorf("validateAppautoscalingPolicyImportInput(%q): expected an error, but returned successfully", tc.input)
}

if !reflect.DeepEqual(tc.expected, idParts) {
t.Errorf("validateAppautoscalingPolicyImportInput(%q): expected %q, but got %q", tc.input, strings.Join(tc.expected, "/"), strings.Join(idParts, "/"))
}
}
}

func TestAccAWSAppautoScalingPolicy_basic(t *testing.T) {
var policy applicationautoscaling.ScalingPolicy
appAutoscalingTargetResourceName := "aws_appautoscaling_target.test"
Expand Down Expand Up @@ -184,7 +244,8 @@ func TestAccAWSAppautoScalingPolicy_dynamoDb(t *testing.T) {
Config: testAccAWSAppautoscalingPolicyDynamoDB(randPolicyName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.dynamo_test", &policy),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.dynamo_test", "name", randPolicyName),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.dynamo_test", "name", fmt.Sprintf("DynamoDBWriteCapacityUtilization:table/%s", randPolicyName)),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.dynamo_test", "policy_type", "TargetTrackingScaling"),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.dynamo_test", "service_namespace", "dynamodb"),
resource.TestCheckResourceAttr("aws_appautoscaling_policy.dynamo_test", "scalable_dimension", "dynamodb:table:WriteCapacityUnits"),
),
Expand Down Expand Up @@ -529,10 +590,10 @@ resource "aws_appautoscaling_target" "dynamo_test" {
}

resource "aws_appautoscaling_policy" "dynamo_test" {
name = "%s"
policy_type = "TargetTrackingScaling"
service_namespace = "dynamodb"
resource_id = "table/${aws_dynamodb_table.dynamodb_table_test.name}"
name = "DynamoDBWriteCapacityUtilization:${aws_appautoscaling_target.dynamo_test.resource_id}"
policy_type = "TargetTrackingScaling"
service_namespace = "dynamodb"
resource_id = "table/${aws_dynamodb_table.dynamodb_table_test.name}"
scalable_dimension = "dynamodb:table:WriteCapacityUnits"

target_tracking_scaling_policy_configuration {
Expand All @@ -547,7 +608,7 @@ resource "aws_appautoscaling_policy" "dynamo_test" {

depends_on = ["aws_appautoscaling_target.dynamo_test"]
}
`, randPolicyName, randPolicyName)
`, randPolicyName)
}

func testAccAWSAppautoscalingPolicy_multiplePoliciesSameName(tableName1, tableName2, namePrefix string) string {
Expand Down