package aws import ( "fmt" "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/applicationautoscaling" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccAWSAppautoScalingPolicy_basic(t *testing.T) { var policy applicationautoscaling.ScalingPolicy randClusterName := fmt.Sprintf("cluster%s", acctest.RandString(10)) randPolicyName := fmt.Sprintf("terraform-test-foobar-%s", acctest.RandString(5)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSAppautoscalingPolicyDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAWSAppautoscalingPolicyConfig(randClusterName, randPolicyName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.foobar_simple", &policy), resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "adjustment_type", "ChangeInCapacity"), resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "policy_type", "StepScaling"), resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "cooldown", "60"), resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "name", randPolicyName), resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "resource_id", fmt.Sprintf("service/%s/foobar", randClusterName)), resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "service_namespace", "ecs"), resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "scalable_dimension", "ecs:service:DesiredCount"), ), }, }, }) } func TestAccAWSAppautoScalingPolicy_spotFleetRequest(t *testing.T) { var policy applicationautoscaling.ScalingPolicy randPolicyName := fmt.Sprintf("test-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: testAccAWSAppautoscalingPolicySpotFleetRequestConfig(randPolicyName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.test", &policy), resource.TestCheckResourceAttr("aws_appautoscaling_policy.test", "name", randPolicyName), resource.TestCheckResourceAttr("aws_appautoscaling_policy.test", "service_namespace", "ec2"), resource.TestCheckResourceAttr("aws_appautoscaling_policy.test", "scalable_dimension", "ec2:spot-fleet-request:TargetCapacity"), ), }, }, }) } func testAccCheckAWSAppautoscalingPolicyExists(n string, policy *applicationautoscaling.ScalingPolicy) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn params := &applicationautoscaling.DescribeScalingPoliciesInput{ ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]), PolicyNames: []*string{aws.String(rs.Primary.ID)}, } resp, err := conn.DescribeScalingPolicies(params) if err != nil { return err } if len(resp.ScalingPolicies) == 0 { return fmt.Errorf("ScalingPolicy %s not found", rs.Primary.ID) } return nil } } func testAccCheckAWSAppautoscalingPolicyDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn for _, rs := range s.RootModule().Resources { params := applicationautoscaling.DescribeScalingPoliciesInput{ ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]), PolicyNames: []*string{aws.String(rs.Primary.ID)}, } resp, err := conn.DescribeScalingPolicies(¶ms) if err == nil { if len(resp.ScalingPolicies) != 0 && *resp.ScalingPolicies[0].PolicyName == rs.Primary.ID { return fmt.Errorf("Application autoscaling policy still exists: %s", rs.Primary.ID) } } } return nil } func testAccAWSAppautoscalingPolicyConfig( randClusterName string, randPolicyName string) string { return fmt.Sprintf(` resource "aws_iam_role" "autoscale_role" { name = "%s" path = "/" assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"sts:AssumeRole\"]}]}" } resource "aws_iam_role_policy" "autoscale_role_policy" { name = "%s" role = "${aws_iam_role.autoscale_role.id}" policy = <