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
25 changes: 21 additions & 4 deletions aws/resource_aws_appautoscaling_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,15 +407,13 @@ func resourceAwsAppautoscalingPolicyDelete(d *schema.ResourceData, meta interfac

func resourceAwsAppautoscalingPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
var serviceNamespace, resourceId, scalableDimension, policyName string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about moving all this string splitting logic into validateAppautoscalingPolicyImportInput to help simplify the Import function?

  func resourceAwsAppautoscalingPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {                          
    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 := 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                                                                                                         
  } 


if len(idParts) < 4 {
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]
serviceNamespace, resourceId, scalableDimension, policyName = validateAppautoscalingPolicyImportInput(idParts)

if serviceNamespace == "" || resourceId == "" || scalableDimension == "" || policyName == "" {
return nil, fmt.Errorf("unexpected format (%q), expected <service-namespace>/<resource-id>/<scalable-dimension>/<policy-name>", d.Id())
Expand All @@ -430,6 +428,25 @@ func resourceAwsAppautoscalingPolicyImport(d *schema.ResourceData, meta interfac
return []*schema.ResourceData{d}, nil
}

func validateAppautoscalingPolicyImportInput(idParts []string) (string, string, string, string) {
var serviceNamespace, resourceId, scalableDimension, policyName string

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

return serviceNamespace, resourceId, scalableDimension, policyName
}

// 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
39 changes: 39 additions & 0 deletions aws/resource_aws_appautoscaling_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -310,6 +311,44 @@ func TestAccAWSAppautoScalingPolicy_ResourceId_ForceNew(t *testing.T) {
})
}

func TestValidateAppautoscalingPolicyImportInput(t *testing.T) {
jdecarli marked this conversation as resolved.
Show resolved Hide resolved
testCases := []struct {
input string
expectedServiceNamespace string
expectedResourceId string
expectedScalableDimension string
expectedPolicyName string
}{
{
"rds/cluster:id/rds:cluster:ReadReplicaCount/cpu-auto-scaling",
"rds", "cluster:id", "rds:cluster:ReadReplicaCount", "cpu-auto-scaling"},
{
"ecs/service/clusterName/serviceName/ecs:service:DesiredCount/scale-down",
"ecs", "service/clusterName/serviceName", "ecs:service:DesiredCount", "scale-down"},
{
"dynamodb/table/tableName/dynamodb:table:ReadCapacityUnits/DynamoDBReadCapacityUtilization:table/tableName",
"dynamodb", "table/tableName", "dynamodb:table:ReadCapacityUnits", "DynamoDBReadCapacityUtilization:table/tableName"},
}

for _, test := range testCases {
idParts := strings.Split(test.input, "/")
serviceNamespace, resourceId, scalableDimension, policyName := validateAppautoscalingPolicyImportInput(idParts)

if serviceNamespace != test.expectedServiceNamespace {
t.Errorf("serviceNamespace interpolation error -> %s != %s", test.expectedServiceNamespace, serviceNamespace)
}
if resourceId != test.expectedResourceId {
t.Errorf("resourceId interpolation error -> %s != %s", test.expectedResourceId, resourceId)
}
if scalableDimension != test.expectedScalableDimension {
t.Errorf("scalableDimension interpolation error -> %s != %s", test.expectedScalableDimension, scalableDimension)
}
if policyName != test.expectedPolicyName {
t.Errorf("policyName interpolation error -> %s != %s", test.expectedPolicyName, policyName)
}
}
}

jdecarli marked this conversation as resolved.
Show resolved Hide resolved
func testAccCheckAWSAppautoscalingPolicyExists(n string, policy *applicationautoscaling.ScalingPolicy) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down