From 991aa3430017c039efac8aee96773cc52b90baf9 Mon Sep 17 00:00:00 2001 From: Raymond Fallon Date: Thu, 2 Feb 2017 04:39:22 -0500 Subject: [PATCH] Update Application Auto Scaling to support scaling an Amazon EC2 Spot fleet. (#8697) * provider/aws: Update Application Auto Scaling service model - Add support for automatically scaling an Amazon EC2 Spot fleet. * Remove duplicate policy_type check. * Test creating a scalable target for a splot fleet request. * Test creating a scaling policy for a splot fleet request. * Update resource docs to support scaling an Amazon EC2 Spot fleet. - aws_appautoscaling_policy - aws_appautoscaling_target * Remove arn attribute from aws_appautoscaling_target - No arn is generated or returned for this resource. * Remove optional name attribute from aws_appautoscaling_target - ScalableTargets do not have a name - I think this was copied from aws_appautoscaling_policy * AWS Application Autoscaling resource documentation tweaks - include a target resource in the policy example - sort attributes by alpha - fixup markdown - add spaces to test config --- .../aws/resource_aws_appautoscaling_policy.go | 20 ++- ...resource_aws_appautoscaling_policy_test.go | 119 ++++++++++++++++++ .../aws/resource_aws_appautoscaling_target.go | 35 ++---- ...resource_aws_appautoscaling_target_test.go | 107 +++++++++++++++- builtin/providers/aws/validators.go | 26 ++++ builtin/providers/aws/validators_test.go | 73 +++++++++++ .../aws/r/appautoscaling_policy.html.markdown | 51 ++++---- .../aws/r/appautoscaling_target.html.markdown | 22 ++-- 8 files changed, 374 insertions(+), 79 deletions(-) diff --git a/builtin/providers/aws/resource_aws_appautoscaling_policy.go b/builtin/providers/aws/resource_aws_appautoscaling_policy.go index 1ce898781044..e75e76152d77 100644 --- a/builtin/providers/aws/resource_aws_appautoscaling_policy.go +++ b/builtin/providers/aws/resource_aws_appautoscaling_policy.go @@ -47,16 +47,16 @@ func resourceAwsAppautoscalingPolicy() *schema.Resource { Required: true, }, "scalable_dimension": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - Default: "ecs:service:DesiredCount", - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateAppautoscalingScalableDimension, }, "service_namespace": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - Default: "ecs", - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateAppautoscalingServiceNamespace, }, "adjustment_type": &schema.Schema{ Type: schema.TypeString, @@ -258,10 +258,6 @@ func getAwsAppautoscalingPutScalingPolicyInput(d *schema.ResourceData) (applicat params.ServiceNamespace = aws.String(v.(string)) } - if v, ok := d.GetOk("policy_type"); ok { - params.PolicyType = aws.String(v.(string)) - } - if v, ok := d.GetOk("scalable_dimension"); ok { params.ScalableDimension = aws.String(v.(string)) } diff --git a/builtin/providers/aws/resource_aws_appautoscaling_policy_test.go b/builtin/providers/aws/resource_aws_appautoscaling_policy_test.go index 0b5b0888d623..36abf932cf5f 100644 --- a/builtin/providers/aws/resource_aws_appautoscaling_policy_test.go +++ b/builtin/providers/aws/resource_aws_appautoscaling_policy_test.go @@ -39,6 +39,29 @@ func TestAccAWSAppautoScalingPolicy_basic(t *testing.T) { }) } +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] @@ -171,3 +194,99 @@ resource "aws_appautoscaling_policy" "foobar_simple" { } `, randClusterName, randClusterName, randClusterName, randPolicyName) } + +func testAccAWSAppautoscalingPolicySpotFleetRequestConfig( + randPolicyName string) string { + return fmt.Sprintf(` +resource "aws_iam_role" "fleet_role" { + assume_role_policy = < 255 { - errors = append(errors, fmt.Errorf( - "%q cannot be longer than 255 characters", k)) - } - return - }, - }, - "arn": &schema.Schema{ - Type: schema.TypeString, - Computed: true, - }, "max_capacity": &schema.Schema{ Type: schema.TypeInt, Required: true, @@ -60,16 +41,16 @@ func resourceAwsAppautoscalingTarget() *schema.Resource { ForceNew: true, }, "scalable_dimension": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - Default: "ecs:service:DesiredCount", - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateAppautoscalingScalableDimension, }, "service_namespace": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - Default: "ecs", - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateAppautoscalingServiceNamespace, }, }, } diff --git a/builtin/providers/aws/resource_aws_appautoscaling_target_test.go b/builtin/providers/aws/resource_aws_appautoscaling_target_test.go index 88f979391b27..313534fce103 100644 --- a/builtin/providers/aws/resource_aws_appautoscaling_target_test.go +++ b/builtin/providers/aws/resource_aws_appautoscaling_target_test.go @@ -46,6 +46,27 @@ func TestAccAWSAppautoScalingTarget_basic(t *testing.T) { }) } +func TestAccAWSAppautoScalingTarget_spotFleetRequest(t *testing.T) { + var target applicationautoscaling.ScalableTarget + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_appautoscaling_target.test", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAppautoscalingTargetDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAppautoscalingTargetSpotFleetRequestConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAppautoscalingTargetExists("aws_appautoscaling_target.test", &target), + resource.TestCheckResourceAttr("aws_appautoscaling_target.test", "service_namespace", "ec2"), + resource.TestCheckResourceAttr("aws_appautoscaling_target.test", "scalable_dimension", "ec2:spot-fleet-request:TargetCapacity"), + ), + }, + }, + }) +} + func testAccCheckAWSAppautoscalingTargetDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn @@ -174,6 +195,7 @@ EOF resource "aws_ecs_cluster" "foo" { name = "%s" } + resource "aws_ecs_task_definition" "task" { family = "foobar" container_definitions = <