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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"regexp"
     7  	"sort"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/aws/aws-sdk-go/aws"
    12  	"github.com/aws/aws-sdk-go/aws/awserr"
    13  	"github.com/aws/aws-sdk-go/service/codedeploy"
    14  	"github.com/hashicorp/terraform/helper/acctest"
    15  	"github.com/hashicorp/terraform/helper/resource"
    16  	"github.com/hashicorp/terraform/helper/schema"
    17  	"github.com/hashicorp/terraform/terraform"
    18  )
    19  
    20  func TestAccAWSCodeDeployDeploymentGroup_basic(t *testing.T) {
    21  	var group codedeploy.DeploymentGroupInfo
    22  
    23  	rName := acctest.RandString(5)
    24  
    25  	resource.Test(t, resource.TestCase{
    26  		PreCheck:     func() { testAccPreCheck(t) },
    27  		Providers:    testAccProviders,
    28  		CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
    29  		Steps: []resource.TestStep{
    30  			resource.TestStep{
    31  				Config: testAccAWSCodeDeployDeploymentGroup(rName),
    32  				Check: resource.ComposeTestCheckFunc(
    33  					testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
    34  					resource.TestCheckResourceAttr(
    35  						"aws_codedeploy_deployment_group.foo", "app_name", "foo_app_"+rName),
    36  					resource.TestCheckResourceAttr(
    37  						"aws_codedeploy_deployment_group.foo", "deployment_group_name", "foo_"+rName),
    38  					resource.TestCheckResourceAttr(
    39  						"aws_codedeploy_deployment_group.foo", "deployment_config_name", "CodeDeployDefault.OneAtATime"),
    40  
    41  					resource.TestCheckResourceAttr(
    42  						"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.#", "1"),
    43  					resource.TestCheckResourceAttr(
    44  						"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2916377465.key", "filterkey"),
    45  					resource.TestCheckResourceAttr(
    46  						"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2916377465.type", "KEY_AND_VALUE"),
    47  					resource.TestCheckResourceAttr(
    48  						"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2916377465.value", "filtervalue"),
    49  				),
    50  			},
    51  			resource.TestStep{
    52  				Config: testAccAWSCodeDeployDeploymentGroupModified(rName),
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
    55  					resource.TestCheckResourceAttr(
    56  						"aws_codedeploy_deployment_group.foo", "app_name", "foo_app_"+rName),
    57  					resource.TestCheckResourceAttr(
    58  						"aws_codedeploy_deployment_group.foo", "deployment_group_name", "bar_"+rName),
    59  					resource.TestCheckResourceAttr(
    60  						"aws_codedeploy_deployment_group.foo", "deployment_config_name", "CodeDeployDefault.OneAtATime"),
    61  
    62  					resource.TestCheckResourceAttr(
    63  						"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.#", "1"),
    64  					resource.TestCheckResourceAttr(
    65  						"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2369538975.key", "filterkey"),
    66  					resource.TestCheckResourceAttr(
    67  						"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2369538975.type", "KEY_AND_VALUE"),
    68  					resource.TestCheckResourceAttr(
    69  						"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2369538975.value", "anotherfiltervalue"),
    70  				),
    71  			},
    72  		},
    73  	})
    74  }
    75  
    76  func TestAccAWSCodeDeployDeploymentGroup_onPremiseTag(t *testing.T) {
    77  	var group codedeploy.DeploymentGroupInfo
    78  
    79  	rName := acctest.RandString(5)
    80  
    81  	resource.Test(t, resource.TestCase{
    82  		PreCheck:     func() { testAccPreCheck(t) },
    83  		Providers:    testAccProviders,
    84  		CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
    85  		Steps: []resource.TestStep{
    86  			resource.TestStep{
    87  				Config: testAccAWSCodeDeployDeploymentGroupOnPremiseTags(rName),
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
    90  					resource.TestCheckResourceAttr(
    91  						"aws_codedeploy_deployment_group.foo", "app_name", "foo_app_"+rName),
    92  					resource.TestCheckResourceAttr(
    93  						"aws_codedeploy_deployment_group.foo", "deployment_group_name", "foo_"+rName),
    94  					resource.TestCheckResourceAttr(
    95  						"aws_codedeploy_deployment_group.foo", "deployment_config_name", "CodeDeployDefault.OneAtATime"),
    96  
    97  					resource.TestCheckResourceAttr(
    98  						"aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.#", "1"),
    99  					resource.TestCheckResourceAttr(
   100  						"aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.2916377465.key", "filterkey"),
   101  					resource.TestCheckResourceAttr(
   102  						"aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.2916377465.type", "KEY_AND_VALUE"),
   103  					resource.TestCheckResourceAttr(
   104  						"aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.2916377465.value", "filtervalue"),
   105  				),
   106  			},
   107  		},
   108  	})
   109  }
   110  
   111  func TestAccAWSCodeDeployDeploymentGroup_disappears(t *testing.T) {
   112  	var group codedeploy.DeploymentGroupInfo
   113  	rName := acctest.RandString(5)
   114  
   115  	resource.Test(t, resource.TestCase{
   116  		PreCheck:     func() { testAccPreCheck(t) },
   117  		Providers:    testAccProviders,
   118  		CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
   119  		Steps: []resource.TestStep{
   120  			resource.TestStep{
   121  				Config: testAccAWSCodeDeployDeploymentGroup(rName),
   122  				Check: resource.ComposeTestCheckFunc(
   123  					testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
   124  					testAccAWSCodeDeployDeploymentGroupDisappears(&group),
   125  				),
   126  				ExpectNonEmptyPlan: true,
   127  			},
   128  		},
   129  	})
   130  }
   131  
   132  func TestAccAWSCodeDeployDeploymentGroup_triggerConfiguration_basic(t *testing.T) {
   133  	var group codedeploy.DeploymentGroupInfo
   134  
   135  	rName := acctest.RandString(5)
   136  
   137  	resource.Test(t, resource.TestCase{
   138  		PreCheck:     func() { testAccPreCheck(t) },
   139  		Providers:    testAccProviders,
   140  		CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
   141  		Steps: []resource.TestStep{
   142  			resource.TestStep{
   143  				Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_create(rName),
   144  				Check: resource.ComposeTestCheckFunc(
   145  					testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group),
   146  					resource.TestCheckResourceAttr(
   147  						"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName),
   148  					resource.TestCheckResourceAttr(
   149  						"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName),
   150  					testAccCheckTriggerEvents(&group, "foo-trigger", []string{
   151  						"DeploymentFailure",
   152  					}),
   153  				),
   154  			},
   155  			resource.TestStep{
   156  				Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_update(rName),
   157  				Check: resource.ComposeTestCheckFunc(
   158  					testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group),
   159  					resource.TestCheckResourceAttr(
   160  						"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName),
   161  					resource.TestCheckResourceAttr(
   162  						"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName),
   163  					testAccCheckTriggerEvents(&group, "foo-trigger", []string{
   164  						"DeploymentFailure",
   165  						"DeploymentSuccess",
   166  					}),
   167  				),
   168  			},
   169  		},
   170  	})
   171  }
   172  
   173  func TestAccAWSCodeDeployDeploymentGroup_triggerConfiguration_multiple(t *testing.T) {
   174  	var group codedeploy.DeploymentGroupInfo
   175  
   176  	rName := acctest.RandString(5)
   177  
   178  	resource.Test(t, resource.TestCase{
   179  		PreCheck:     func() { testAccPreCheck(t) },
   180  		Providers:    testAccProviders,
   181  		CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
   182  		Steps: []resource.TestStep{
   183  			resource.TestStep{
   184  				Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_createMultiple(rName),
   185  				Check: resource.ComposeTestCheckFunc(
   186  					testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group),
   187  					resource.TestCheckResourceAttr(
   188  						"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName),
   189  					resource.TestCheckResourceAttr(
   190  						"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName),
   191  					testAccCheckTriggerEvents(&group, "foo-trigger", []string{
   192  						"DeploymentFailure",
   193  					}),
   194  					testAccCheckTriggerEvents(&group, "bar-trigger", []string{
   195  						"InstanceFailure",
   196  					}),
   197  					testAccCheckTriggerTargetArn(&group, "bar-trigger",
   198  						regexp.MustCompile("^arn:aws:sns:[^:]+:[0-9]{12}:bar-topic-"+rName+"$")),
   199  				),
   200  			},
   201  			resource.TestStep{
   202  				Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_updateMultiple(rName),
   203  				Check: resource.ComposeTestCheckFunc(
   204  					testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group),
   205  					resource.TestCheckResourceAttr(
   206  						"aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName),
   207  					resource.TestCheckResourceAttr(
   208  						"aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName),
   209  					testAccCheckTriggerEvents(&group, "foo-trigger", []string{
   210  						"DeploymentFailure",
   211  						"DeploymentStart",
   212  						"DeploymentStop",
   213  						"DeploymentSuccess",
   214  					}),
   215  					testAccCheckTriggerEvents(&group, "bar-trigger", []string{
   216  						"InstanceFailure",
   217  					}),
   218  					testAccCheckTriggerTargetArn(&group, "bar-trigger",
   219  						regexp.MustCompile("^arn:aws:sns:[^:]+:[0-9]{12}:baz-topic-"+rName+"$")),
   220  				),
   221  			},
   222  		},
   223  	})
   224  }
   225  
   226  func TestValidateAWSCodeDeployTriggerEvent(t *testing.T) {
   227  	cases := []struct {
   228  		Value    string
   229  		ErrCount int
   230  	}{
   231  		{
   232  			Value:    "DeploymentStart",
   233  			ErrCount: 0,
   234  		},
   235  		{
   236  			Value:    "DeploymentStop",
   237  			ErrCount: 0,
   238  		},
   239  		{
   240  			Value:    "DeploymentSuccess",
   241  			ErrCount: 0,
   242  		},
   243  		{
   244  			Value:    "DeploymentFailure",
   245  			ErrCount: 0,
   246  		},
   247  		{
   248  			Value:    "InstanceStart",
   249  			ErrCount: 0,
   250  		},
   251  		{
   252  			Value:    "InstanceSuccess",
   253  			ErrCount: 0,
   254  		},
   255  		{
   256  			Value:    "InstanceFailure",
   257  			ErrCount: 0,
   258  		},
   259  		{
   260  			Value:    "DeploymentStarts",
   261  			ErrCount: 1,
   262  		},
   263  		{
   264  			Value:    "InstanceFail",
   265  			ErrCount: 1,
   266  		},
   267  		{
   268  			Value:    "Foo",
   269  			ErrCount: 1,
   270  		},
   271  		{
   272  			Value:    "",
   273  			ErrCount: 1,
   274  		},
   275  	}
   276  
   277  	for _, tc := range cases {
   278  		_, errors := validateTriggerEvent(tc.Value, "trigger_event")
   279  		if len(errors) != tc.ErrCount {
   280  			t.Fatalf("Trigger event validation failed for event type %q: %q", tc.Value, errors)
   281  		}
   282  	}
   283  }
   284  
   285  func TestBuildTriggerConfigs(t *testing.T) {
   286  	input := []interface{}{
   287  		map[string]interface{}{
   288  			"trigger_events": schema.NewSet(schema.HashString, []interface{}{
   289  				"DeploymentFailure",
   290  			}),
   291  			"trigger_name":       "foo-trigger",
   292  			"trigger_target_arn": "arn:aws:sns:us-west-2:123456789012:foo-topic",
   293  		},
   294  	}
   295  
   296  	expected := []*codedeploy.TriggerConfig{
   297  		&codedeploy.TriggerConfig{
   298  			TriggerEvents: []*string{
   299  				aws.String("DeploymentFailure"),
   300  			},
   301  			TriggerName:      aws.String("foo-trigger"),
   302  			TriggerTargetArn: aws.String("arn:aws:sns:us-west-2:123456789012:foo-topic"),
   303  		},
   304  	}
   305  
   306  	actual := buildTriggerConfigs(input)
   307  
   308  	if !reflect.DeepEqual(actual, expected) {
   309  		t.Fatalf("buildTriggerConfigs output is not correct.\nGot:\n%#v\nExpected:\n%#v\n",
   310  			actual, expected)
   311  	}
   312  }
   313  
   314  func TestTriggerConfigsToMap(t *testing.T) {
   315  	input := []*codedeploy.TriggerConfig{
   316  		&codedeploy.TriggerConfig{
   317  			TriggerEvents: []*string{
   318  				aws.String("DeploymentFailure"),
   319  				aws.String("InstanceFailure"),
   320  			},
   321  			TriggerName:      aws.String("bar-trigger"),
   322  			TriggerTargetArn: aws.String("arn:aws:sns:us-west-2:123456789012:bar-topic"),
   323  		},
   324  	}
   325  
   326  	expected := map[string]interface{}{
   327  		"trigger_events": schema.NewSet(schema.HashString, []interface{}{
   328  			"DeploymentFailure",
   329  			"InstanceFailure",
   330  		}),
   331  		"trigger_name":       "bar-trigger",
   332  		"trigger_target_arn": "arn:aws:sns:us-west-2:123456789012:bar-topic",
   333  	}
   334  
   335  	actual := triggerConfigsToMap(input)[0]
   336  
   337  	fatal := false
   338  
   339  	if actual["trigger_name"] != expected["trigger_name"] {
   340  		fatal = true
   341  	}
   342  
   343  	if actual["trigger_target_arn"] != expected["trigger_target_arn"] {
   344  		fatal = true
   345  	}
   346  
   347  	actualEvents := actual["trigger_events"].(*schema.Set)
   348  	expectedEvents := expected["trigger_events"].(*schema.Set)
   349  	if !actualEvents.Equal(expectedEvents) {
   350  		fatal = true
   351  	}
   352  
   353  	if fatal {
   354  		t.Fatalf("triggerConfigsToMap output is not correct.\nGot:\n%#v\nExpected:\n%#v\n",
   355  			actual, expected)
   356  	}
   357  }
   358  
   359  func testAccCheckTriggerEvents(group *codedeploy.DeploymentGroupInfo, triggerName string, expectedEvents []string) resource.TestCheckFunc {
   360  	return func(s *terraform.State) error {
   361  
   362  		for _, actual := range group.TriggerConfigurations {
   363  			if *actual.TriggerName == triggerName {
   364  
   365  				numberOfEvents := len(actual.TriggerEvents)
   366  				if numberOfEvents != len(expectedEvents) {
   367  					return fmt.Errorf("Trigger events do not match. Expected: %d. Got: %d.",
   368  						len(expectedEvents), numberOfEvents)
   369  				}
   370  
   371  				actualEvents := make([]string, 0, numberOfEvents)
   372  				for _, event := range actual.TriggerEvents {
   373  					actualEvents = append(actualEvents, *event)
   374  				}
   375  				sort.Strings(actualEvents)
   376  
   377  				if !reflect.DeepEqual(actualEvents, expectedEvents) {
   378  					return fmt.Errorf("Trigger events do not match.\nExpected: %v\nGot: %v\n",
   379  						expectedEvents, actualEvents)
   380  				}
   381  				break
   382  			}
   383  		}
   384  		return nil
   385  	}
   386  }
   387  
   388  func testAccCheckTriggerTargetArn(group *codedeploy.DeploymentGroupInfo, triggerName string, r *regexp.Regexp) resource.TestCheckFunc {
   389  	return func(s *terraform.State) error {
   390  		for _, actual := range group.TriggerConfigurations {
   391  			if *actual.TriggerName == triggerName {
   392  				if !r.MatchString(*actual.TriggerTargetArn) {
   393  					return fmt.Errorf("Trigger target arn does not match regular expression.\nRegex: %v\nTriggerTargetArn: %v\n",
   394  						r, *actual.TriggerTargetArn)
   395  				}
   396  				break
   397  			}
   398  		}
   399  		return nil
   400  	}
   401  }
   402  
   403  func testAccCheckAWSCodeDeployDeploymentGroupDestroy(s *terraform.State) error {
   404  	conn := testAccProvider.Meta().(*AWSClient).codedeployconn
   405  
   406  	for _, rs := range s.RootModule().Resources {
   407  		if rs.Type != "aws_codedeploy_deployment_group" {
   408  			continue
   409  		}
   410  
   411  		resp, err := conn.GetDeploymentGroup(&codedeploy.GetDeploymentGroupInput{
   412  			ApplicationName:     aws.String(rs.Primary.Attributes["app_name"]),
   413  			DeploymentGroupName: aws.String(rs.Primary.Attributes["deployment_group_name"]),
   414  		})
   415  
   416  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "ApplicationDoesNotExistException" {
   417  			continue
   418  		}
   419  
   420  		if err == nil {
   421  			if resp.DeploymentGroupInfo.DeploymentGroupName != nil {
   422  				return fmt.Errorf("CodeDeploy deployment group still exists:\n%#v", *resp.DeploymentGroupInfo.DeploymentGroupName)
   423  			}
   424  		}
   425  
   426  		return err
   427  	}
   428  
   429  	return nil
   430  }
   431  
   432  func testAccAWSCodeDeployDeploymentGroupDisappears(group *codedeploy.DeploymentGroupInfo) resource.TestCheckFunc {
   433  	return func(s *terraform.State) error {
   434  		conn := testAccProvider.Meta().(*AWSClient).codedeployconn
   435  		opts := &codedeploy.DeleteDeploymentGroupInput{
   436  			ApplicationName:     group.ApplicationName,
   437  			DeploymentGroupName: group.DeploymentGroupName,
   438  		}
   439  		if _, err := conn.DeleteDeploymentGroup(opts); err != nil {
   440  			return err
   441  		}
   442  		return resource.Retry(40*time.Minute, func() *resource.RetryError {
   443  			opts := &codedeploy.GetDeploymentGroupInput{
   444  				ApplicationName:     group.ApplicationName,
   445  				DeploymentGroupName: group.DeploymentGroupName,
   446  			}
   447  			_, err := conn.GetDeploymentGroup(opts)
   448  			if err != nil {
   449  				codedeploy, ok := err.(awserr.Error)
   450  				if ok && codedeploy.Code() == "DeploymentGroupDoesNotExistException" {
   451  					return nil
   452  				}
   453  				return resource.NonRetryableError(
   454  					fmt.Errorf("Error retrieving CodeDeploy Deployment Group: %s", err))
   455  			}
   456  			return resource.RetryableError(fmt.Errorf(
   457  				"Waiting for CodeDeploy Deployment Group: %v", group.DeploymentGroupName))
   458  		})
   459  	}
   460  }
   461  
   462  func testAccCheckAWSCodeDeployDeploymentGroupExists(name string, group *codedeploy.DeploymentGroupInfo) resource.TestCheckFunc {
   463  	return func(s *terraform.State) error {
   464  		rs, ok := s.RootModule().Resources[name]
   465  		if !ok {
   466  			return fmt.Errorf("Not found: %s", name)
   467  		}
   468  
   469  		conn := testAccProvider.Meta().(*AWSClient).codedeployconn
   470  
   471  		resp, err := conn.GetDeploymentGroup(&codedeploy.GetDeploymentGroupInput{
   472  			ApplicationName:     aws.String(rs.Primary.Attributes["app_name"]),
   473  			DeploymentGroupName: aws.String(rs.Primary.Attributes["deployment_group_name"]),
   474  		})
   475  
   476  		if err != nil {
   477  			return err
   478  		}
   479  
   480  		*group = *resp.DeploymentGroupInfo
   481  
   482  		return nil
   483  	}
   484  }
   485  
   486  func testAccAWSCodeDeployDeploymentGroup(rName string) string {
   487  	return fmt.Sprintf(`
   488  resource "aws_codedeploy_app" "foo_app" {
   489  	name = "foo_app_%s"
   490  }
   491  
   492  resource "aws_iam_role_policy" "foo_policy" {
   493  	name = "foo_policy_%s"
   494  	role = "${aws_iam_role.foo_role.id}"
   495  	policy = <<EOF
   496  {
   497  	"Version": "2012-10-17",
   498  	"Statement": [
   499  		{
   500  			"Effect": "Allow",
   501  			"Action": [
   502  				"autoscaling:CompleteLifecycleAction",
   503  				"autoscaling:DeleteLifecycleHook",
   504  				"autoscaling:DescribeAutoScalingGroups",
   505  				"autoscaling:DescribeLifecycleHooks",
   506  				"autoscaling:PutLifecycleHook",
   507  				"autoscaling:RecordLifecycleActionHeartbeat",
   508  				"ec2:DescribeInstances",
   509  				"ec2:DescribeInstanceStatus",
   510  				"tag:GetTags",
   511  				"tag:GetResources"
   512  			],
   513  			"Resource": "*"
   514  		}
   515  	]
   516  }
   517  EOF
   518  }
   519  
   520  resource "aws_iam_role" "foo_role" {
   521  	name = "foo_role_%s"
   522  	assume_role_policy = <<EOF
   523  {
   524  	"Version": "2012-10-17",
   525  	"Statement": [
   526  		{
   527  			"Sid": "",
   528  			"Effect": "Allow",
   529  			"Principal": {
   530  				"Service": [
   531  					"codedeploy.amazonaws.com"
   532  				]
   533  			},
   534  			"Action": "sts:AssumeRole"
   535  		}
   536  	]
   537  }
   538  EOF
   539  }
   540  
   541  resource "aws_codedeploy_deployment_group" "foo" {
   542  	app_name = "${aws_codedeploy_app.foo_app.name}"
   543  	deployment_group_name = "foo_%s"
   544  	service_role_arn = "${aws_iam_role.foo_role.arn}"
   545  	ec2_tag_filter {
   546  		key = "filterkey"
   547  		type = "KEY_AND_VALUE"
   548  		value = "filtervalue"
   549  	}
   550  }`, rName, rName, rName, rName)
   551  }
   552  
   553  func testAccAWSCodeDeployDeploymentGroupModified(rName string) string {
   554  	return fmt.Sprintf(`
   555  resource "aws_codedeploy_app" "foo_app" {
   556  	name = "foo_app_%s"
   557  }
   558  
   559  resource "aws_iam_role_policy" "foo_policy" {
   560  	name = "foo_policy_%s"
   561  	role = "${aws_iam_role.foo_role.id}"
   562  	policy = <<EOF
   563  {
   564  	"Version": "2012-10-17",
   565  	"Statement": [
   566  		{
   567  			"Effect": "Allow",
   568  			"Action": [
   569  				"autoscaling:CompleteLifecycleAction",
   570  				"autoscaling:DeleteLifecycleHook",
   571  				"autoscaling:DescribeAutoScalingGroups",
   572  				"autoscaling:DescribeLifecycleHooks",
   573  				"autoscaling:PutLifecycleHook",
   574  				"autoscaling:RecordLifecycleActionHeartbeat",
   575  				"ec2:DescribeInstances",
   576  				"ec2:DescribeInstanceStatus",
   577  				"tag:GetTags",
   578  				"tag:GetResources"
   579  			],
   580  			"Resource": "*"
   581  		}
   582  	]
   583  }
   584  EOF
   585  }
   586  
   587  resource "aws_iam_role" "foo_role" {
   588  	name = "foo_role_%s"
   589  	assume_role_policy = <<EOF
   590  {
   591  	"Version": "2012-10-17",
   592  	"Statement": [
   593  		{
   594  			"Sid": "",
   595  			"Effect": "Allow",
   596  			"Principal": {
   597  				"Service": [
   598  					"codedeploy.amazonaws.com"
   599  				]
   600  			},
   601  			"Action": "sts:AssumeRole"
   602  		}
   603  	]
   604  }
   605  EOF
   606  }
   607  
   608  resource "aws_codedeploy_deployment_group" "foo" {
   609  	app_name = "${aws_codedeploy_app.foo_app.name}"
   610  	deployment_group_name = "bar_%s"
   611  	service_role_arn = "${aws_iam_role.foo_role.arn}"
   612  	ec2_tag_filter {
   613  		key = "filterkey"
   614  		type = "KEY_AND_VALUE"
   615  		value = "anotherfiltervalue"
   616  	}
   617  }`, rName, rName, rName, rName)
   618  }
   619  
   620  func testAccAWSCodeDeployDeploymentGroupOnPremiseTags(rName string) string {
   621  	return fmt.Sprintf(`
   622  resource "aws_codedeploy_app" "foo_app" {
   623  	name = "foo_app_%s"
   624  }
   625  
   626  resource "aws_iam_role_policy" "foo_policy" {
   627  	name = "foo_policy_%s"
   628  	role = "${aws_iam_role.foo_role.id}"
   629  	policy = <<EOF
   630  {
   631  	"Version": "2012-10-17",
   632  	"Statement": [
   633  		{
   634  			"Effect": "Allow",
   635  			"Action": [
   636  				"autoscaling:CompleteLifecycleAction",
   637  				"autoscaling:DeleteLifecycleHook",
   638  				"autoscaling:DescribeAutoScalingGroups",
   639  				"autoscaling:DescribeLifecycleHooks",
   640  				"autoscaling:PutLifecycleHook",
   641  				"autoscaling:RecordLifecycleActionHeartbeat",
   642  				"ec2:DescribeInstances",
   643  				"ec2:DescribeInstanceStatus",
   644  				"tag:GetTags",
   645  				"tag:GetResources"
   646  			],
   647  			"Resource": "*"
   648  		}
   649  	]
   650  }
   651  EOF
   652  }
   653  
   654  resource "aws_iam_role" "foo_role" {
   655  	name = "foo_role_%s"
   656  	assume_role_policy = <<EOF
   657  {
   658  	"Version": "2012-10-17",
   659  	"Statement": [
   660  		{
   661  			"Sid": "",
   662  			"Effect": "Allow",
   663  			"Principal": {
   664  				"Service": [
   665  					"codedeploy.amazonaws.com"
   666  				]
   667  			},
   668  			"Action": "sts:AssumeRole"
   669  		}
   670  	]
   671  }
   672  EOF
   673  }
   674  
   675  resource "aws_codedeploy_deployment_group" "foo" {
   676  	app_name = "${aws_codedeploy_app.foo_app.name}"
   677  	deployment_group_name = "foo_%s"
   678  	service_role_arn = "${aws_iam_role.foo_role.arn}"
   679  	on_premises_instance_tag_filter {
   680  		key = "filterkey"
   681  		type = "KEY_AND_VALUE"
   682  		value = "filtervalue"
   683  	}
   684  }`, rName, rName, rName, rName)
   685  }
   686  
   687  func baseCodeDeployConfig(rName string) string {
   688  	return fmt.Sprintf(`
   689  resource "aws_codedeploy_app" "foo_app" {
   690  	name = "foo-app-%s"
   691  }
   692  
   693  resource "aws_iam_role_policy" "foo_policy" {
   694  	name = "foo-policy-%s"
   695  	role = "${aws_iam_role.foo_role.id}"
   696  	policy = <<EOF
   697  {
   698  	"Version": "2012-10-17",
   699  	"Statement": [
   700  		{
   701  			"Effect": "Allow",
   702  			"Action": [
   703  				"autoscaling:CompleteLifecycleAction",
   704  				"autoscaling:DeleteLifecycleHook",
   705  				"autoscaling:DescribeAutoScalingGroups",
   706  				"autoscaling:DescribeLifecycleHooks",
   707  				"autoscaling:PutLifecycleHook",
   708  				"autoscaling:RecordLifecycleActionHeartbeat",
   709  				"codedeploy:*",
   710  				"ec2:DescribeInstances",
   711  				"ec2:DescribeInstanceStatus",
   712  				"tag:GetTags",
   713  				"tag:GetResources",
   714  				"sns:Publish"
   715  			],
   716  			"Resource": "*"
   717  		}
   718  	]
   719  }
   720  EOF
   721  }
   722  
   723  resource "aws_iam_role" "foo_role" {
   724  	name = "foo-role-%s"
   725  	assume_role_policy = <<EOF
   726  {
   727  	"Version": "2012-10-17",
   728  	"Statement": [
   729  		{
   730  			"Sid": "",
   731  			"Effect": "Allow",
   732  			"Principal": {
   733  				"Service": "codedeploy.amazonaws.com"
   734  			},
   735  			"Action": "sts:AssumeRole"
   736  		}
   737  	]
   738  }
   739  EOF
   740  }
   741  
   742  resource "aws_sns_topic" "foo_topic" {
   743  	name = "foo-topic-%s"
   744  }`, rName, rName, rName, rName)
   745  }
   746  
   747  func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_create(rName string) string {
   748  	return fmt.Sprintf(`
   749  
   750  	%s
   751  	
   752  resource "aws_codedeploy_deployment_group" "foo_group" {
   753  	app_name = "${aws_codedeploy_app.foo_app.name}"
   754  	deployment_group_name = "foo-group-%s"
   755  	service_role_arn = "${aws_iam_role.foo_role.arn}"
   756  
   757  	trigger_configuration {
   758  		trigger_events = ["DeploymentFailure"]
   759  		trigger_name = "foo-trigger"
   760  		trigger_target_arn = "${aws_sns_topic.foo_topic.arn}"
   761  	}
   762  }`, baseCodeDeployConfig(rName), rName)
   763  }
   764  
   765  func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_update(rName string) string {
   766  	return fmt.Sprintf(`
   767  
   768  	%s
   769  	
   770  resource "aws_codedeploy_deployment_group" "foo_group" {
   771  	app_name = "${aws_codedeploy_app.foo_app.name}"
   772  	deployment_group_name = "foo-group-%s"
   773  	service_role_arn = "${aws_iam_role.foo_role.arn}"
   774  
   775  	trigger_configuration {
   776  		trigger_events = ["DeploymentSuccess", "DeploymentFailure"]
   777  		trigger_name = "foo-trigger"
   778  		trigger_target_arn = "${aws_sns_topic.foo_topic.arn}"
   779  	}
   780  }`, baseCodeDeployConfig(rName), rName)
   781  }
   782  
   783  func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_createMultiple(rName string) string {
   784  	return fmt.Sprintf(`
   785  
   786  	%s
   787  	
   788  resource "aws_sns_topic" "bar_topic" {
   789  	name = "bar-topic-%s"
   790  }
   791  
   792  resource "aws_codedeploy_deployment_group" "foo_group" {
   793  	app_name = "${aws_codedeploy_app.foo_app.name}"
   794  	deployment_group_name = "foo-group-%s"
   795  	service_role_arn = "${aws_iam_role.foo_role.arn}"
   796  
   797  	trigger_configuration {
   798  		trigger_events = ["DeploymentFailure"]
   799  		trigger_name = "foo-trigger"
   800  		trigger_target_arn = "${aws_sns_topic.foo_topic.arn}"
   801  	}
   802  
   803  	trigger_configuration {
   804  		trigger_events = ["InstanceFailure"]
   805  		trigger_name = "bar-trigger"
   806  		trigger_target_arn = "${aws_sns_topic.bar_topic.arn}"
   807  	}
   808  }`, baseCodeDeployConfig(rName), rName, rName)
   809  }
   810  
   811  func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_updateMultiple(rName string) string {
   812  	return fmt.Sprintf(`
   813  
   814  	%s
   815  	
   816  resource "aws_sns_topic" "bar_topic" {
   817  	name = "bar-topic-%s"
   818  }
   819  
   820  resource "aws_sns_topic" "baz_topic" {
   821  	name = "baz-topic-%s"
   822  }
   823  
   824  resource "aws_codedeploy_deployment_group" "foo_group" {
   825  	app_name = "${aws_codedeploy_app.foo_app.name}"
   826  	deployment_group_name = "foo-group-%s"
   827  	service_role_arn = "${aws_iam_role.foo_role.arn}"
   828  
   829  	trigger_configuration {
   830  		trigger_events = ["DeploymentStart", "DeploymentSuccess", "DeploymentFailure", "DeploymentStop"]
   831  		trigger_name = "foo-trigger"
   832  		trigger_target_arn = "${aws_sns_topic.foo_topic.arn}"
   833  	}
   834  
   835  	trigger_configuration {
   836  		trigger_events = ["InstanceFailure"]
   837  		trigger_name = "bar-trigger"
   838  		trigger_target_arn = "${aws_sns_topic.baz_topic.arn}"
   839  	}
   840  }`, baseCodeDeployConfig(rName), rName, rName, rName)
   841  }