github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_appautoscaling_target_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/applicationautoscaling"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSAppautoScalingTarget_basic(t *testing.T) {
    16  	var target applicationautoscaling.ScalableTarget
    17  
    18  	randClusterName := fmt.Sprintf("cluster-%s", acctest.RandString(10))
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:      func() { testAccPreCheck(t) },
    22  		IDRefreshName: "aws_appautoscaling_target.bar",
    23  		Providers:     testAccProviders,
    24  		CheckDestroy:  testAccCheckAWSAppautoscalingTargetDestroy,
    25  		Steps: []resource.TestStep{
    26  			resource.TestStep{
    27  				Config: testAccAWSAppautoscalingTargetConfig(randClusterName),
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckAWSAppautoscalingTargetExists("aws_appautoscaling_target.bar", &target),
    30  					resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "service_namespace", "ecs"),
    31  					resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "scalable_dimension", "ecs:service:DesiredCount"),
    32  					resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "min_capacity", "1"),
    33  					resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "max_capacity", "3"),
    34  				),
    35  			},
    36  
    37  			resource.TestStep{
    38  				Config: testAccAWSAppautoscalingTargetConfigUpdate(randClusterName),
    39  				Check: resource.ComposeTestCheckFunc(
    40  					testAccCheckAWSAppautoscalingTargetExists("aws_appautoscaling_target.bar", &target),
    41  					resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "min_capacity", "2"),
    42  					resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "max_capacity", "8"),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func testAccCheckAWSAppautoscalingTargetDestroy(s *terraform.State) error {
    50  	conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn
    51  
    52  	for _, rs := range s.RootModule().Resources {
    53  		if rs.Type != "aws_appautoscaling_target" {
    54  			continue
    55  		}
    56  
    57  		// Try to find the target
    58  		describeTargets, err := conn.DescribeScalableTargets(
    59  			&applicationautoscaling.DescribeScalableTargetsInput{
    60  				ResourceIds:      []*string{aws.String(rs.Primary.ID)},
    61  				ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]),
    62  			},
    63  		)
    64  
    65  		if err == nil {
    66  			if len(describeTargets.ScalableTargets) != 0 &&
    67  				*describeTargets.ScalableTargets[0].ResourceId == rs.Primary.ID {
    68  				return fmt.Errorf("Application AutoScaling Target still exists")
    69  			}
    70  		}
    71  
    72  		// Verify error
    73  		e, ok := err.(awserr.Error)
    74  		if !ok {
    75  			return err
    76  		}
    77  		if e.Code() != "" {
    78  			return e
    79  		}
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  func testAccCheckAWSAppautoscalingTargetExists(n string, target *applicationautoscaling.ScalableTarget) resource.TestCheckFunc {
    86  	return func(s *terraform.State) error {
    87  		rs, ok := s.RootModule().Resources[n]
    88  		if !ok {
    89  			return fmt.Errorf("Not found: %s", n)
    90  		}
    91  
    92  		if rs.Primary.ID == "" {
    93  			return fmt.Errorf("No Application AutoScaling Target ID is set")
    94  		}
    95  
    96  		conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn
    97  
    98  		describeTargets, err := conn.DescribeScalableTargets(
    99  			&applicationautoscaling.DescribeScalableTargetsInput{
   100  				ResourceIds:      []*string{aws.String(rs.Primary.ID)},
   101  				ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]),
   102  			},
   103  		)
   104  
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		if len(describeTargets.ScalableTargets) != 1 || *describeTargets.ScalableTargets[0].ResourceId != rs.Primary.ID {
   110  			return fmt.Errorf("Application AutoScaling ResourceId not found")
   111  		}
   112  
   113  		target = describeTargets.ScalableTargets[0]
   114  
   115  		return nil
   116  	}
   117  }
   118  
   119  func testAccAWSAppautoscalingTargetConfig(
   120  	randClusterName string) string {
   121  	return fmt.Sprintf(`
   122  resource "aws_iam_role" "autoscale_role" {
   123  	name = "autoscalerole%s"
   124  	path = "/"
   125  
   126  	assume_role_policy = <<EOF
   127  {
   128    "Version": "2012-10-17",
   129    "Statement": [
   130      {
   131        "Effect": "Allow",
   132        "Principal": {
   133          "Service": "application-autoscaling.amazonaws.com"
   134        },
   135        "Action": "sts:AssumeRole"
   136      }
   137    ]
   138  }
   139  EOF
   140  }
   141  
   142  resource "aws_iam_role_policy" "autoscale_role_policy" {
   143  	name = "autoscalepolicy%s"
   144  	role = "${aws_iam_role.autoscale_role.id}"
   145  
   146  	policy = <<EOF
   147  {
   148      "Version": "2012-10-17",
   149      "Statement": [
   150          {
   151              "Effect": "Allow",
   152              "Action": [
   153                  "ecs:DescribeServices",
   154                  "ecs:UpdateService"
   155              ],
   156              "Resource": [
   157                  "*"
   158              ]
   159          },
   160          {
   161              "Effect": "Allow",
   162              "Action": [
   163                  "cloudwatch:DescribeAlarms"
   164              ],
   165              "Resource": [
   166                  "*"
   167              ]
   168          }
   169      ]
   170  }
   171  EOF
   172  }
   173  
   174  resource "aws_ecs_cluster" "foo" {
   175  	name = "%s"
   176  }
   177  resource "aws_ecs_task_definition" "task" {
   178  	family = "foobar"
   179  	container_definitions = <<EOF
   180  [
   181      {
   182          "name": "busybox",
   183          "image": "busybox:latest",
   184          "cpu": 10,
   185          "memory": 128,
   186          "essential": true
   187      }
   188  ]
   189  EOF
   190  }
   191  resource "aws_ecs_service" "service" {
   192  	name = "foobar"
   193  	cluster = "${aws_ecs_cluster.foo.id}"
   194  	task_definition = "${aws_ecs_task_definition.task.arn}"
   195  	desired_count = 1
   196  
   197  	deployment_maximum_percent = 200
   198  	deployment_minimum_healthy_percent = 50
   199  }
   200  resource "aws_appautoscaling_target" "bar" {
   201  	service_namespace = "ecs"
   202  	resource_id = "service/${aws_ecs_cluster.foo.name}/${aws_ecs_service.service.name}"
   203  	scalable_dimension = "ecs:service:DesiredCount"
   204  	role_arn = "${aws_iam_role.autoscale_role.arn}"	
   205  	min_capacity = 1
   206  	max_capacity = 3
   207  }
   208  `, randClusterName, randClusterName, randClusterName)
   209  }
   210  
   211  func testAccAWSAppautoscalingTargetConfigUpdate(
   212  	randClusterName string) string {
   213  	return fmt.Sprintf(`
   214  resource "aws_iam_role" "autoscale_role" {
   215  	name = "autoscalerole%s"
   216  	path = "/"
   217  
   218  	assume_role_policy = <<EOF
   219  {
   220    "Version": "2012-10-17",
   221    "Statement": [
   222      {
   223        "Effect": "Allow",
   224        "Principal": {
   225          "Service": "application-autoscaling.amazonaws.com"
   226        },
   227        "Action": "sts:AssumeRole"
   228      }
   229    ]
   230  }
   231  EOF
   232  }
   233  
   234  resource "aws_iam_role_policy" "autoscale_role_policy" {
   235  	name = "autoscalepolicy%s"
   236  	role = "${aws_iam_role.autoscale_role.id}"
   237  
   238  	policy = <<EOF
   239  {
   240      "Version": "2012-10-17",
   241      "Statement": [
   242          {
   243              "Effect": "Allow",
   244              "Action": [
   245                  "ecs:DescribeServices",
   246                  "ecs:UpdateService"
   247              ],
   248              "Resource": [
   249                  "*"
   250              ]
   251          },
   252          {
   253              "Effect": "Allow",
   254              "Action": [
   255                  "cloudwatch:DescribeAlarms"
   256              ],
   257              "Resource": [
   258                  "*"
   259              ]
   260          }
   261      ]
   262  }
   263  EOF
   264  }
   265  
   266  resource "aws_ecs_cluster" "foo" {
   267  	name = "%s"
   268  }
   269  resource "aws_ecs_task_definition" "task" {
   270  	family = "foobar"
   271  	container_definitions = <<EOF
   272  [
   273      {
   274          "name": "busybox",
   275          "image": "busybox:latest",
   276          "cpu": 10,
   277          "memory": 128,
   278          "essential": true
   279      }
   280  ]
   281  EOF
   282  }
   283  resource "aws_ecs_service" "service" {
   284  	name = "foobar"
   285  	cluster = "${aws_ecs_cluster.foo.id}"
   286  	task_definition = "${aws_ecs_task_definition.task.arn}"
   287  	desired_count = 1
   288  
   289  	deployment_maximum_percent = 200
   290  	deployment_minimum_healthy_percent = 50
   291  }
   292  resource "aws_appautoscaling_target" "bar" {
   293  	service_namespace = "ecs"
   294  	resource_id = "service/${aws_ecs_cluster.foo.name}/${aws_ecs_service.service.name}"
   295  	scalable_dimension = "ecs:service:DesiredCount"
   296  	role_arn = "${aws_iam_role.autoscale_role.arn}"	
   297  	min_capacity = 2
   298  	max_capacity = 8
   299  }
   300  `, randClusterName, randClusterName, randClusterName)
   301  }