github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_codedeploy_deployment_group_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/service/codedeploy"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSCodeDeployDeploymentGroup_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccAWSCodeDeployDeploymentGroup,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo"),
    23  				),
    24  			},
    25  			resource.TestStep{
    26  				Config: testAccAWSCodeDeployDeploymentGroupModified,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo"),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func testAccCheckAWSCodeDeployDeploymentGroupDestroy(s *terraform.State) error {
    36  	conn := testAccProvider.Meta().(*AWSClient).codedeployconn
    37  
    38  	for _, rs := range s.RootModule().Resources {
    39  		if rs.Type != "aws_codedeploy_deployment_group" {
    40  			continue
    41  		}
    42  
    43  		resp, err := conn.GetDeploymentGroup(&codedeploy.GetDeploymentGroupInput{
    44  			ApplicationName:     aws.String(rs.Primary.Attributes["app_name"]),
    45  			DeploymentGroupName: aws.String(rs.Primary.Attributes["deployment_group_name"]),
    46  		})
    47  
    48  		if err == nil {
    49  			if resp.DeploymentGroupInfo.DeploymentGroupName != nil {
    50  				return fmt.Errorf("CodeDeploy deployment group still exists:\n%#v", *resp.DeploymentGroupInfo.DeploymentGroupName)
    51  			}
    52  		}
    53  
    54  		return err
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  func testAccCheckAWSCodeDeployDeploymentGroupExists(name string) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		_, ok := s.RootModule().Resources[name]
    63  		if !ok {
    64  			return fmt.Errorf("Not found: %s", name)
    65  		}
    66  
    67  		return nil
    68  	}
    69  }
    70  
    71  var testAccAWSCodeDeployDeploymentGroup = `
    72  resource "aws_codedeploy_app" "foo_app" {
    73  	name = "foo_app"
    74  }
    75  
    76  resource "aws_iam_role_policy" "foo_policy" {
    77  	name = "foo_policy"
    78  	role = "${aws_iam_role.foo_role.id}"
    79  	policy = <<EOF
    80  {
    81      "Version": "2012-10-17",
    82      "Statement": [
    83          {
    84              "Effect": "Allow",
    85              "Action": [
    86                  "autoscaling:CompleteLifecycleAction",
    87                  "autoscaling:DeleteLifecycleHook",
    88  	        "autoscaling:DescribeAutoScalingGroups",
    89                  "autoscaling:DescribeLifecycleHooks",
    90                  "autoscaling:PutLifecycleHook",
    91                  "autoscaling:RecordLifecycleActionHeartbeat",
    92                  "ec2:DescribeInstances",
    93                  "ec2:DescribeInstanceStatus",
    94                  "tag:GetTags",
    95  	        "tag:GetResources"
    96              ],
    97              "Resource": "*"
    98          }
    99      ]
   100  }
   101  EOF
   102  }
   103  
   104  resource "aws_iam_role" "foo_role" {
   105  	name = "foo_role"
   106  	assume_role_policy = <<EOF
   107  {
   108    "Version": "2012-10-17",
   109    "Statement": [
   110      {
   111        "Sid": "",
   112        "Effect": "Allow",
   113        "Principal": {
   114          "Service": [
   115            "codedeploy.amazonaws.com"
   116          ]
   117        },
   118        "Action": "sts:AssumeRole"
   119      }
   120    ]
   121  }
   122  EOF
   123  }
   124  
   125  resource "aws_codedeploy_deployment_group" "foo" {
   126  	app_name = "${aws_codedeploy_app.foo_app.name}"
   127  	deployment_group_name = "foo"
   128  	service_role_arn = "${aws_iam_role.foo_role.arn}"
   129  	ec2_tag_filter {
   130  		key = "filterkey"
   131  		type = "KEY_AND_VALUE"
   132  		value = "filtervalue"
   133  	}
   134  }`
   135  
   136  var testAccAWSCodeDeployDeploymentGroupModified = `
   137  resource "aws_codedeploy_app" "foo_app" {
   138  	name = "foo_app"
   139  }
   140  
   141  resource "aws_iam_role_policy" "foo_policy" {
   142  	name = "foo_policy"
   143  	role = "${aws_iam_role.foo_role.id}"
   144  	policy = <<EOF
   145  {
   146      "Version": "2012-10-17",
   147      "Statement": [
   148          {
   149              "Effect": "Allow",
   150              "Action": [
   151                  "autoscaling:CompleteLifecycleAction",
   152                  "autoscaling:DeleteLifecycleHook",
   153  	        "autoscaling:DescribeAutoScalingGroups",
   154                  "autoscaling:DescribeLifecycleHooks",
   155                  "autoscaling:PutLifecycleHook",
   156                  "autoscaling:RecordLifecycleActionHeartbeat",
   157                  "ec2:DescribeInstances",
   158                  "ec2:DescribeInstanceStatus",
   159                  "tag:GetTags",
   160  	        "tag:GetResources"
   161              ],
   162              "Resource": "*"
   163          }
   164      ]
   165  }
   166  EOF
   167  }
   168  
   169  resource "aws_iam_role" "foo_role" {
   170  	name = "foo_role"
   171  	assume_role_policy = <<EOF
   172  {
   173    "Version": "2012-10-17",
   174    "Statement": [
   175      {
   176        "Sid": "",
   177        "Effect": "Allow",
   178        "Principal": {
   179          "Service": [
   180            "codedeploy.amazonaws.com"
   181          ]
   182        },
   183        "Action": "sts:AssumeRole"
   184      }
   185    ]
   186  }
   187  EOF
   188  }
   189  
   190  resource "aws_codedeploy_deployment_group" "foo" {
   191  	app_name = "${aws_codedeploy_app.foo_app.name}"
   192  	deployment_group_name = "bar"
   193  	service_role_arn = "${aws_iam_role.foo_role.arn}"
   194  	ec2_tag_filter {
   195  		key = "filterkey"
   196  		type = "KEY_AND_VALUE"
   197  		value = "filtervalue"
   198  	}
   199  }`