github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 resource.TestMatchResourceAttr( 41 "aws_codedeploy_deployment_group.foo", "service_role_arn", 42 regexp.MustCompile("arn:aws:iam::[0-9]{12}:role/foo_role_.*")), 43 44 resource.TestCheckResourceAttr( 45 "aws_codedeploy_deployment_group.foo", "ec2_tag_filter.#", "1"), 46 resource.TestCheckResourceAttr( 47 "aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2916377465.key", "filterkey"), 48 resource.TestCheckResourceAttr( 49 "aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2916377465.type", "KEY_AND_VALUE"), 50 resource.TestCheckResourceAttr( 51 "aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2916377465.value", "filtervalue"), 52 53 resource.TestCheckResourceAttr( 54 "aws_codedeploy_deployment_group.foo", "alarm_configuration.#", "0"), 55 resource.TestCheckResourceAttr( 56 "aws_codedeploy_deployment_group.foo", "auto_rollback_configuration.#", "0"), 57 resource.TestCheckResourceAttr( 58 "aws_codedeploy_deployment_group.foo", "trigger_configuration.#", "0"), 59 ), 60 }, 61 resource.TestStep{ 62 Config: testAccAWSCodeDeployDeploymentGroupModified(rName), 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group), 65 resource.TestCheckResourceAttr( 66 "aws_codedeploy_deployment_group.foo", "app_name", "foo_app_"+rName), 67 resource.TestCheckResourceAttr( 68 "aws_codedeploy_deployment_group.foo", "deployment_group_name", "bar_"+rName), 69 resource.TestCheckResourceAttr( 70 "aws_codedeploy_deployment_group.foo", "deployment_config_name", "CodeDeployDefault.OneAtATime"), 71 resource.TestMatchResourceAttr( 72 "aws_codedeploy_deployment_group.foo", "service_role_arn", 73 regexp.MustCompile("arn:aws:iam::[0-9]{12}:role/bar_role_.*")), 74 75 resource.TestCheckResourceAttr( 76 "aws_codedeploy_deployment_group.foo", "ec2_tag_filter.#", "1"), 77 resource.TestCheckResourceAttr( 78 "aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2369538975.key", "filterkey"), 79 resource.TestCheckResourceAttr( 80 "aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2369538975.type", "KEY_AND_VALUE"), 81 resource.TestCheckResourceAttr( 82 "aws_codedeploy_deployment_group.foo", "ec2_tag_filter.2369538975.value", "anotherfiltervalue"), 83 84 resource.TestCheckResourceAttr( 85 "aws_codedeploy_deployment_group.foo", "alarm_configuration.#", "0"), 86 resource.TestCheckResourceAttr( 87 "aws_codedeploy_deployment_group.foo", "auto_rollback_configuration.#", "0"), 88 resource.TestCheckResourceAttr( 89 "aws_codedeploy_deployment_group.foo", "trigger_configuration.#", "0"), 90 ), 91 }, 92 }, 93 }) 94 } 95 96 func TestAccAWSCodeDeployDeploymentGroup_onPremiseTag(t *testing.T) { 97 var group codedeploy.DeploymentGroupInfo 98 99 rName := acctest.RandString(5) 100 101 resource.Test(t, resource.TestCase{ 102 PreCheck: func() { testAccPreCheck(t) }, 103 Providers: testAccProviders, 104 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 105 Steps: []resource.TestStep{ 106 resource.TestStep{ 107 Config: testAccAWSCodeDeployDeploymentGroupOnPremiseTags(rName), 108 Check: resource.ComposeTestCheckFunc( 109 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group), 110 resource.TestCheckResourceAttr( 111 "aws_codedeploy_deployment_group.foo", "app_name", "foo_app_"+rName), 112 resource.TestCheckResourceAttr( 113 "aws_codedeploy_deployment_group.foo", "deployment_group_name", "foo_"+rName), 114 resource.TestCheckResourceAttr( 115 "aws_codedeploy_deployment_group.foo", "deployment_config_name", "CodeDeployDefault.OneAtATime"), 116 117 resource.TestCheckResourceAttr( 118 "aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.#", "1"), 119 resource.TestCheckResourceAttr( 120 "aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.2916377465.key", "filterkey"), 121 resource.TestCheckResourceAttr( 122 "aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.2916377465.type", "KEY_AND_VALUE"), 123 resource.TestCheckResourceAttr( 124 "aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.2916377465.value", "filtervalue"), 125 ), 126 }, 127 }, 128 }) 129 } 130 131 func TestAccAWSCodeDeployDeploymentGroup_disappears(t *testing.T) { 132 var group codedeploy.DeploymentGroupInfo 133 rName := acctest.RandString(5) 134 135 resource.Test(t, resource.TestCase{ 136 PreCheck: func() { testAccPreCheck(t) }, 137 Providers: testAccProviders, 138 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 139 Steps: []resource.TestStep{ 140 resource.TestStep{ 141 Config: testAccAWSCodeDeployDeploymentGroup(rName), 142 Check: resource.ComposeTestCheckFunc( 143 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group), 144 testAccAWSCodeDeployDeploymentGroupDisappears(&group), 145 ), 146 ExpectNonEmptyPlan: true, 147 }, 148 }, 149 }) 150 } 151 152 func TestAccAWSCodeDeployDeploymentGroup_triggerConfiguration_basic(t *testing.T) { 153 var group codedeploy.DeploymentGroupInfo 154 155 rName := acctest.RandString(5) 156 157 resource.Test(t, resource.TestCase{ 158 PreCheck: func() { testAccPreCheck(t) }, 159 Providers: testAccProviders, 160 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 161 Steps: []resource.TestStep{ 162 resource.TestStep{ 163 Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_create(rName), 164 Check: resource.ComposeTestCheckFunc( 165 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 166 resource.TestCheckResourceAttr( 167 "aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName), 168 resource.TestCheckResourceAttr( 169 "aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName), 170 testAccCheckTriggerEvents(&group, "foo-trigger", []string{ 171 "DeploymentFailure", 172 }), 173 ), 174 }, 175 resource.TestStep{ 176 Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_update(rName), 177 Check: resource.ComposeTestCheckFunc( 178 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 179 resource.TestCheckResourceAttr( 180 "aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName), 181 resource.TestCheckResourceAttr( 182 "aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName), 183 testAccCheckTriggerEvents(&group, "foo-trigger", []string{ 184 "DeploymentFailure", 185 "DeploymentSuccess", 186 }), 187 ), 188 }, 189 }, 190 }) 191 } 192 193 func TestAccAWSCodeDeployDeploymentGroup_triggerConfiguration_multiple(t *testing.T) { 194 var group codedeploy.DeploymentGroupInfo 195 196 rName := acctest.RandString(5) 197 198 resource.Test(t, resource.TestCase{ 199 PreCheck: func() { testAccPreCheck(t) }, 200 Providers: testAccProviders, 201 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 202 Steps: []resource.TestStep{ 203 resource.TestStep{ 204 Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_createMultiple(rName), 205 Check: resource.ComposeTestCheckFunc( 206 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 207 resource.TestCheckResourceAttr( 208 "aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName), 209 resource.TestCheckResourceAttr( 210 "aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName), 211 testAccCheckTriggerEvents(&group, "foo-trigger", []string{ 212 "DeploymentFailure", 213 }), 214 testAccCheckTriggerEvents(&group, "bar-trigger", []string{ 215 "InstanceFailure", 216 }), 217 testAccCheckTriggerTargetArn(&group, "bar-trigger", 218 regexp.MustCompile("^arn:aws:sns:[^:]+:[0-9]{12}:bar-topic-"+rName+"$")), 219 ), 220 }, 221 resource.TestStep{ 222 Config: testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_updateMultiple(rName), 223 Check: resource.ComposeTestCheckFunc( 224 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 225 resource.TestCheckResourceAttr( 226 "aws_codedeploy_deployment_group.foo_group", "app_name", "foo-app-"+rName), 227 resource.TestCheckResourceAttr( 228 "aws_codedeploy_deployment_group.foo_group", "deployment_group_name", "foo-group-"+rName), 229 testAccCheckTriggerEvents(&group, "foo-trigger", []string{ 230 "DeploymentFailure", 231 "DeploymentStart", 232 "DeploymentStop", 233 "DeploymentSuccess", 234 }), 235 testAccCheckTriggerEvents(&group, "bar-trigger", []string{ 236 "InstanceFailure", 237 }), 238 testAccCheckTriggerTargetArn(&group, "bar-trigger", 239 regexp.MustCompile("^arn:aws:sns:[^:]+:[0-9]{12}:baz-topic-"+rName+"$")), 240 ), 241 }, 242 }, 243 }) 244 } 245 246 func TestAccAWSCodeDeployDeploymentGroup_autoRollbackConfiguration_create(t *testing.T) { 247 var group codedeploy.DeploymentGroupInfo 248 249 rName := acctest.RandString(5) 250 251 resource.Test(t, resource.TestCase{ 252 PreCheck: func() { testAccPreCheck(t) }, 253 Providers: testAccProviders, 254 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 255 Steps: []resource.TestStep{ 256 resource.TestStep{ 257 Config: test_config_auto_rollback_configuration_delete(rName), 258 Check: resource.ComposeTestCheckFunc( 259 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 260 resource.TestCheckResourceAttr( 261 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.#", "0"), 262 ), 263 }, 264 resource.TestStep{ 265 Config: test_config_auto_rollback_configuration_create(rName), 266 Check: resource.ComposeTestCheckFunc( 267 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 268 resource.TestCheckResourceAttr( 269 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.#", "1"), 270 resource.TestCheckResourceAttr( 271 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.enabled", "true"), 272 resource.TestCheckResourceAttr( 273 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.#", "1"), 274 resource.TestCheckResourceAttr( 275 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.135881253", "DEPLOYMENT_FAILURE"), 276 ), 277 }, 278 }, 279 }) 280 } 281 282 func TestAccAWSCodeDeployDeploymentGroup_autoRollbackConfiguration_update(t *testing.T) { 283 var group codedeploy.DeploymentGroupInfo 284 285 rName := acctest.RandString(5) 286 287 resource.Test(t, resource.TestCase{ 288 PreCheck: func() { testAccPreCheck(t) }, 289 Providers: testAccProviders, 290 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 291 Steps: []resource.TestStep{ 292 resource.TestStep{ 293 Config: test_config_auto_rollback_configuration_create(rName), 294 Check: resource.ComposeTestCheckFunc( 295 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 296 resource.TestCheckResourceAttr( 297 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.#", "1"), 298 resource.TestCheckResourceAttr( 299 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.enabled", "true"), 300 resource.TestCheckResourceAttr( 301 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.#", "1"), 302 resource.TestCheckResourceAttr( 303 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.135881253", "DEPLOYMENT_FAILURE"), 304 ), 305 }, 306 resource.TestStep{ 307 Config: test_config_auto_rollback_configuration_update(rName), 308 Check: resource.ComposeTestCheckFunc( 309 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 310 resource.TestCheckResourceAttr( 311 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.#", "1"), 312 resource.TestCheckResourceAttr( 313 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.enabled", "true"), 314 resource.TestCheckResourceAttr( 315 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.#", "2"), 316 resource.TestCheckResourceAttr( 317 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.104943466", "DEPLOYMENT_STOP_ON_ALARM"), 318 resource.TestCheckResourceAttr( 319 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.135881253", "DEPLOYMENT_FAILURE"), 320 ), 321 }, 322 }, 323 }) 324 } 325 326 func TestAccAWSCodeDeployDeploymentGroup_autoRollbackConfiguration_delete(t *testing.T) { 327 var group codedeploy.DeploymentGroupInfo 328 329 rName := acctest.RandString(5) 330 331 resource.Test(t, resource.TestCase{ 332 PreCheck: func() { testAccPreCheck(t) }, 333 Providers: testAccProviders, 334 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 335 Steps: []resource.TestStep{ 336 resource.TestStep{ 337 Config: test_config_auto_rollback_configuration_create(rName), 338 Check: resource.ComposeTestCheckFunc( 339 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 340 resource.TestCheckResourceAttr( 341 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.#", "1"), 342 resource.TestCheckResourceAttr( 343 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.enabled", "true"), 344 resource.TestCheckResourceAttr( 345 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.#", "1"), 346 resource.TestCheckResourceAttr( 347 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.135881253", "DEPLOYMENT_FAILURE"), 348 ), 349 }, 350 resource.TestStep{ 351 Config: test_config_auto_rollback_configuration_delete(rName), 352 Check: resource.ComposeTestCheckFunc( 353 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 354 resource.TestCheckResourceAttr( 355 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.#", "0"), 356 ), 357 }, 358 }, 359 }) 360 } 361 362 func TestAccAWSCodeDeployDeploymentGroup_autoRollbackConfiguration_disable(t *testing.T) { 363 var group codedeploy.DeploymentGroupInfo 364 365 rName := acctest.RandString(5) 366 367 resource.Test(t, resource.TestCase{ 368 PreCheck: func() { testAccPreCheck(t) }, 369 Providers: testAccProviders, 370 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 371 Steps: []resource.TestStep{ 372 resource.TestStep{ 373 Config: test_config_auto_rollback_configuration_create(rName), 374 Check: resource.ComposeTestCheckFunc( 375 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 376 resource.TestCheckResourceAttr( 377 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.#", "1"), 378 resource.TestCheckResourceAttr( 379 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.enabled", "true"), 380 resource.TestCheckResourceAttr( 381 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.#", "1"), 382 resource.TestCheckResourceAttr( 383 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.135881253", "DEPLOYMENT_FAILURE"), 384 ), 385 }, 386 resource.TestStep{ 387 Config: test_config_auto_rollback_configuration_disable(rName), 388 Check: resource.ComposeTestCheckFunc( 389 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 390 resource.TestCheckResourceAttr( 391 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.#", "1"), 392 resource.TestCheckResourceAttr( 393 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.enabled", "false"), 394 resource.TestCheckResourceAttr( 395 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.#", "1"), 396 resource.TestCheckResourceAttr( 397 "aws_codedeploy_deployment_group.foo_group", "auto_rollback_configuration.0.events.135881253", "DEPLOYMENT_FAILURE"), 398 ), 399 }, 400 }, 401 }) 402 } 403 404 func TestAccAWSCodeDeployDeploymentGroup_alarmConfiguration_create(t *testing.T) { 405 var group codedeploy.DeploymentGroupInfo 406 407 rName := acctest.RandString(5) 408 409 resource.Test(t, resource.TestCase{ 410 PreCheck: func() { testAccPreCheck(t) }, 411 Providers: testAccProviders, 412 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 413 Steps: []resource.TestStep{ 414 resource.TestStep{ 415 Config: test_config_alarm_configuration_delete(rName), 416 Check: resource.ComposeTestCheckFunc( 417 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 418 resource.TestCheckResourceAttr( 419 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.#", "0"), 420 ), 421 }, 422 resource.TestStep{ 423 Config: test_config_alarm_configuration_create(rName), 424 Check: resource.ComposeTestCheckFunc( 425 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 426 resource.TestCheckResourceAttr( 427 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.#", "1"), 428 resource.TestCheckResourceAttr( 429 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.enabled", "true"), 430 resource.TestCheckResourceAttr( 431 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.#", "1"), 432 resource.TestCheckResourceAttr( 433 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.2356372769", "foo"), 434 resource.TestCheckResourceAttr( 435 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.ignore_poll_alarm_failure", "false"), 436 ), 437 }, 438 }, 439 }) 440 } 441 442 func TestAccAWSCodeDeployDeploymentGroup_alarmConfiguration_update(t *testing.T) { 443 var group codedeploy.DeploymentGroupInfo 444 445 rName := acctest.RandString(5) 446 447 resource.Test(t, resource.TestCase{ 448 PreCheck: func() { testAccPreCheck(t) }, 449 Providers: testAccProviders, 450 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 451 Steps: []resource.TestStep{ 452 resource.TestStep{ 453 Config: test_config_alarm_configuration_create(rName), 454 Check: resource.ComposeTestCheckFunc( 455 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 456 resource.TestCheckResourceAttr( 457 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.#", "1"), 458 resource.TestCheckResourceAttr( 459 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.enabled", "true"), 460 resource.TestCheckResourceAttr( 461 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.#", "1"), 462 resource.TestCheckResourceAttr( 463 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.2356372769", "foo"), 464 resource.TestCheckResourceAttr( 465 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.ignore_poll_alarm_failure", "false"), 466 ), 467 }, 468 resource.TestStep{ 469 Config: test_config_alarm_configuration_update(rName), 470 Check: resource.ComposeTestCheckFunc( 471 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 472 resource.TestCheckResourceAttr( 473 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.#", "1"), 474 resource.TestCheckResourceAttr( 475 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.enabled", "true"), 476 resource.TestCheckResourceAttr( 477 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.#", "2"), 478 resource.TestCheckResourceAttr( 479 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.1996459178", "bar"), 480 resource.TestCheckResourceAttr( 481 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.2356372769", "foo"), 482 resource.TestCheckResourceAttr( 483 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.ignore_poll_alarm_failure", "true"), 484 ), 485 }, 486 }, 487 }) 488 } 489 490 func TestAccAWSCodeDeployDeploymentGroup_alarmConfiguration_delete(t *testing.T) { 491 var group codedeploy.DeploymentGroupInfo 492 493 rName := acctest.RandString(5) 494 495 resource.Test(t, resource.TestCase{ 496 PreCheck: func() { testAccPreCheck(t) }, 497 Providers: testAccProviders, 498 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 499 Steps: []resource.TestStep{ 500 resource.TestStep{ 501 Config: test_config_alarm_configuration_create(rName), 502 Check: resource.ComposeTestCheckFunc( 503 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 504 resource.TestCheckResourceAttr( 505 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.#", "1"), 506 resource.TestCheckResourceAttr( 507 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.enabled", "true"), 508 resource.TestCheckResourceAttr( 509 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.#", "1"), 510 resource.TestCheckResourceAttr( 511 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.2356372769", "foo"), 512 resource.TestCheckResourceAttr( 513 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.ignore_poll_alarm_failure", "false"), 514 ), 515 }, 516 resource.TestStep{ 517 Config: test_config_alarm_configuration_delete(rName), 518 Check: resource.ComposeTestCheckFunc( 519 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 520 resource.TestCheckResourceAttr( 521 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.#", "0"), 522 ), 523 }, 524 }, 525 }) 526 } 527 528 func TestAccAWSCodeDeployDeploymentGroup_alarmConfiguration_disable(t *testing.T) { 529 var group codedeploy.DeploymentGroupInfo 530 531 rName := acctest.RandString(5) 532 533 resource.Test(t, resource.TestCase{ 534 PreCheck: func() { testAccPreCheck(t) }, 535 Providers: testAccProviders, 536 CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy, 537 Steps: []resource.TestStep{ 538 resource.TestStep{ 539 Config: test_config_alarm_configuration_create(rName), 540 Check: resource.ComposeTestCheckFunc( 541 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 542 resource.TestCheckResourceAttr( 543 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.#", "1"), 544 resource.TestCheckResourceAttr( 545 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.enabled", "true"), 546 resource.TestCheckResourceAttr( 547 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.#", "1"), 548 resource.TestCheckResourceAttr( 549 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.2356372769", "foo"), 550 resource.TestCheckResourceAttr( 551 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.ignore_poll_alarm_failure", "false"), 552 ), 553 }, 554 resource.TestStep{ 555 Config: test_config_alarm_configuration_disable(rName), 556 Check: resource.ComposeTestCheckFunc( 557 testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo_group", &group), 558 resource.TestCheckResourceAttr( 559 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.#", "1"), 560 resource.TestCheckResourceAttr( 561 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.enabled", "false"), 562 resource.TestCheckResourceAttr( 563 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.#", "1"), 564 resource.TestCheckResourceAttr( 565 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.alarms.2356372769", "foo"), 566 resource.TestCheckResourceAttr( 567 "aws_codedeploy_deployment_group.foo_group", "alarm_configuration.0.ignore_poll_alarm_failure", "false"), 568 ), 569 }, 570 }, 571 }) 572 } 573 574 func TestValidateAWSCodeDeployTriggerEvent(t *testing.T) { 575 cases := []struct { 576 Value string 577 ErrCount int 578 }{ 579 { 580 Value: "DeploymentStart", 581 ErrCount: 0, 582 }, 583 { 584 Value: "DeploymentStop", 585 ErrCount: 0, 586 }, 587 { 588 Value: "DeploymentSuccess", 589 ErrCount: 0, 590 }, 591 { 592 Value: "DeploymentFailure", 593 ErrCount: 0, 594 }, 595 { 596 Value: "DeploymentRollback", 597 ErrCount: 0, 598 }, 599 { 600 Value: "InstanceStart", 601 ErrCount: 0, 602 }, 603 { 604 Value: "InstanceSuccess", 605 ErrCount: 0, 606 }, 607 { 608 Value: "InstanceFailure", 609 ErrCount: 0, 610 }, 611 { 612 Value: "DeploymentStarts", 613 ErrCount: 1, 614 }, 615 { 616 Value: "InstanceFail", 617 ErrCount: 1, 618 }, 619 { 620 Value: "Foo", 621 ErrCount: 1, 622 }, 623 { 624 Value: "", 625 ErrCount: 1, 626 }, 627 } 628 629 for _, tc := range cases { 630 _, errors := validateTriggerEvent(tc.Value, "trigger_event") 631 if len(errors) != tc.ErrCount { 632 t.Fatalf("Trigger event validation failed for event type %q: %q", tc.Value, errors) 633 } 634 } 635 } 636 637 func TestBuildTriggerConfigs(t *testing.T) { 638 input := []interface{}{ 639 map[string]interface{}{ 640 "trigger_events": schema.NewSet(schema.HashString, []interface{}{ 641 "DeploymentFailure", 642 }), 643 "trigger_name": "foo-trigger", 644 "trigger_target_arn": "arn:aws:sns:us-west-2:123456789012:foo-topic", 645 }, 646 } 647 648 expected := []*codedeploy.TriggerConfig{ 649 &codedeploy.TriggerConfig{ 650 TriggerEvents: []*string{ 651 aws.String("DeploymentFailure"), 652 }, 653 TriggerName: aws.String("foo-trigger"), 654 TriggerTargetArn: aws.String("arn:aws:sns:us-west-2:123456789012:foo-topic"), 655 }, 656 } 657 658 actual := buildTriggerConfigs(input) 659 660 if !reflect.DeepEqual(actual, expected) { 661 t.Fatalf("buildTriggerConfigs output is not correct.\nGot:\n%#v\nExpected:\n%#v\n", 662 actual, expected) 663 } 664 } 665 666 func TestTriggerConfigsToMap(t *testing.T) { 667 input := []*codedeploy.TriggerConfig{ 668 &codedeploy.TriggerConfig{ 669 TriggerEvents: []*string{ 670 aws.String("DeploymentFailure"), 671 aws.String("InstanceFailure"), 672 }, 673 TriggerName: aws.String("bar-trigger"), 674 TriggerTargetArn: aws.String("arn:aws:sns:us-west-2:123456789012:bar-topic"), 675 }, 676 } 677 678 expected := map[string]interface{}{ 679 "trigger_events": schema.NewSet(schema.HashString, []interface{}{ 680 "DeploymentFailure", 681 "InstanceFailure", 682 }), 683 "trigger_name": "bar-trigger", 684 "trigger_target_arn": "arn:aws:sns:us-west-2:123456789012:bar-topic", 685 } 686 687 actual := triggerConfigsToMap(input)[0] 688 689 fatal := false 690 691 if actual["trigger_name"] != expected["trigger_name"] { 692 fatal = true 693 } 694 695 if actual["trigger_target_arn"] != expected["trigger_target_arn"] { 696 fatal = true 697 } 698 699 actualEvents := actual["trigger_events"].(*schema.Set) 700 expectedEvents := expected["trigger_events"].(*schema.Set) 701 if !actualEvents.Equal(expectedEvents) { 702 fatal = true 703 } 704 705 if fatal { 706 t.Fatalf("triggerConfigsToMap output is not correct.\nGot:\n%#v\nExpected:\n%#v\n", 707 actual, expected) 708 } 709 } 710 711 func TestBuildAutoRollbackConfig(t *testing.T) { 712 input := []interface{}{ 713 map[string]interface{}{ 714 "events": schema.NewSet(schema.HashString, []interface{}{ 715 "DEPLOYMENT_FAILURE", 716 }), 717 "enabled": true, 718 }, 719 } 720 721 expected := &codedeploy.AutoRollbackConfiguration{ 722 Events: []*string{ 723 aws.String("DEPLOYMENT_FAILURE"), 724 }, 725 Enabled: aws.Bool(true), 726 } 727 728 actual := buildAutoRollbackConfig(input) 729 730 if !reflect.DeepEqual(actual, expected) { 731 t.Fatalf("buildAutoRollbackConfig output is not correct.\nGot:\n%#v\nExpected:\n%#v\n", 732 actual, expected) 733 } 734 } 735 736 func TestAutoRollbackConfigToMap(t *testing.T) { 737 input := &codedeploy.AutoRollbackConfiguration{ 738 Events: []*string{ 739 aws.String("DEPLOYMENT_FAILURE"), 740 aws.String("DEPLOYMENT_STOP_ON_ALARM"), 741 }, 742 Enabled: aws.Bool(false), 743 } 744 745 expected := map[string]interface{}{ 746 "events": schema.NewSet(schema.HashString, []interface{}{ 747 "DEPLOYMENT_FAILURE", 748 "DEPLOYMENT_STOP_ON_ALARM", 749 }), 750 "enabled": false, 751 } 752 753 actual := autoRollbackConfigToMap(input)[0] 754 755 fatal := false 756 757 if actual["enabled"] != expected["enabled"] { 758 fatal = true 759 } 760 761 actualEvents := actual["events"].(*schema.Set) 762 expectedEvents := expected["events"].(*schema.Set) 763 if !actualEvents.Equal(expectedEvents) { 764 fatal = true 765 } 766 767 if fatal { 768 t.Fatalf("autoRollbackConfigToMap output is not correct.\nGot:\n%#v\nExpected:\n%#v\n", 769 actual, expected) 770 } 771 } 772 773 func TestBuildAlarmConfig(t *testing.T) { 774 input := []interface{}{ 775 map[string]interface{}{ 776 "alarms": schema.NewSet(schema.HashString, []interface{}{ 777 "foo-alarm", 778 }), 779 "enabled": true, 780 "ignore_poll_alarm_failure": false, 781 }, 782 } 783 784 expected := &codedeploy.AlarmConfiguration{ 785 Alarms: []*codedeploy.Alarm{ 786 { 787 Name: aws.String("foo-alarm"), 788 }, 789 }, 790 Enabled: aws.Bool(true), 791 IgnorePollAlarmFailure: aws.Bool(false), 792 } 793 794 actual := buildAlarmConfig(input) 795 796 if !reflect.DeepEqual(actual, expected) { 797 t.Fatalf("buildAlarmConfig output is not correct.\nGot:\n%#v\nExpected:\n%#v\n", 798 actual, expected) 799 } 800 } 801 802 func TestAlarmConfigToMap(t *testing.T) { 803 input := &codedeploy.AlarmConfiguration{ 804 Alarms: []*codedeploy.Alarm{ 805 { 806 Name: aws.String("bar-alarm"), 807 }, 808 { 809 Name: aws.String("foo-alarm"), 810 }, 811 }, 812 Enabled: aws.Bool(false), 813 IgnorePollAlarmFailure: aws.Bool(true), 814 } 815 816 expected := map[string]interface{}{ 817 "alarms": schema.NewSet(schema.HashString, []interface{}{ 818 "bar-alarm", 819 "foo-alarm", 820 }), 821 "enabled": false, 822 "ignore_poll_alarm_failure": true, 823 } 824 825 actual := alarmConfigToMap(input)[0] 826 827 fatal := false 828 829 if actual["enabled"] != expected["enabled"] { 830 fatal = true 831 } 832 833 if actual["ignore_poll_alarm_failure"] != expected["ignore_poll_alarm_failure"] { 834 fatal = true 835 } 836 837 actualAlarms := actual["alarms"].(*schema.Set) 838 expectedAlarms := expected["alarms"].(*schema.Set) 839 if !actualAlarms.Equal(expectedAlarms) { 840 fatal = true 841 } 842 843 if fatal { 844 t.Fatalf("alarmConfigToMap output is not correct.\nGot:\n%#v\nExpected:\n%#v\n", 845 actual, expected) 846 } 847 } 848 849 func testAccCheckTriggerEvents(group *codedeploy.DeploymentGroupInfo, triggerName string, expectedEvents []string) resource.TestCheckFunc { 850 return func(s *terraform.State) error { 851 852 for _, actual := range group.TriggerConfigurations { 853 if *actual.TriggerName == triggerName { 854 855 numberOfEvents := len(actual.TriggerEvents) 856 if numberOfEvents != len(expectedEvents) { 857 return fmt.Errorf("Trigger events do not match. Expected: %d. Got: %d.", 858 len(expectedEvents), numberOfEvents) 859 } 860 861 actualEvents := make([]string, 0, numberOfEvents) 862 for _, event := range actual.TriggerEvents { 863 actualEvents = append(actualEvents, *event) 864 } 865 sort.Strings(actualEvents) 866 867 if !reflect.DeepEqual(actualEvents, expectedEvents) { 868 return fmt.Errorf("Trigger events do not match.\nExpected: %v\nGot: %v\n", 869 expectedEvents, actualEvents) 870 } 871 break 872 } 873 } 874 return nil 875 } 876 } 877 878 func testAccCheckTriggerTargetArn(group *codedeploy.DeploymentGroupInfo, triggerName string, r *regexp.Regexp) resource.TestCheckFunc { 879 return func(s *terraform.State) error { 880 for _, actual := range group.TriggerConfigurations { 881 if *actual.TriggerName == triggerName { 882 if !r.MatchString(*actual.TriggerTargetArn) { 883 return fmt.Errorf("Trigger target arn does not match regular expression.\nRegex: %v\nTriggerTargetArn: %v\n", 884 r, *actual.TriggerTargetArn) 885 } 886 break 887 } 888 } 889 return nil 890 } 891 } 892 893 func testAccCheckAWSCodeDeployDeploymentGroupDestroy(s *terraform.State) error { 894 conn := testAccProvider.Meta().(*AWSClient).codedeployconn 895 896 for _, rs := range s.RootModule().Resources { 897 if rs.Type != "aws_codedeploy_deployment_group" { 898 continue 899 } 900 901 resp, err := conn.GetDeploymentGroup(&codedeploy.GetDeploymentGroupInput{ 902 ApplicationName: aws.String(rs.Primary.Attributes["app_name"]), 903 DeploymentGroupName: aws.String(rs.Primary.Attributes["deployment_group_name"]), 904 }) 905 906 if ae, ok := err.(awserr.Error); ok && ae.Code() == "ApplicationDoesNotExistException" { 907 continue 908 } 909 910 if err == nil { 911 if resp.DeploymentGroupInfo.DeploymentGroupName != nil { 912 return fmt.Errorf("CodeDeploy deployment group still exists:\n%#v", *resp.DeploymentGroupInfo.DeploymentGroupName) 913 } 914 } 915 916 return err 917 } 918 919 return nil 920 } 921 922 func testAccAWSCodeDeployDeploymentGroupDisappears(group *codedeploy.DeploymentGroupInfo) resource.TestCheckFunc { 923 return func(s *terraform.State) error { 924 conn := testAccProvider.Meta().(*AWSClient).codedeployconn 925 opts := &codedeploy.DeleteDeploymentGroupInput{ 926 ApplicationName: group.ApplicationName, 927 DeploymentGroupName: group.DeploymentGroupName, 928 } 929 if _, err := conn.DeleteDeploymentGroup(opts); err != nil { 930 return err 931 } 932 return resource.Retry(40*time.Minute, func() *resource.RetryError { 933 opts := &codedeploy.GetDeploymentGroupInput{ 934 ApplicationName: group.ApplicationName, 935 DeploymentGroupName: group.DeploymentGroupName, 936 } 937 _, err := conn.GetDeploymentGroup(opts) 938 if err != nil { 939 codedeploy, ok := err.(awserr.Error) 940 if ok && codedeploy.Code() == "DeploymentGroupDoesNotExistException" { 941 return nil 942 } 943 return resource.NonRetryableError( 944 fmt.Errorf("Error retrieving CodeDeploy Deployment Group: %s", err)) 945 } 946 return resource.RetryableError(fmt.Errorf( 947 "Waiting for CodeDeploy Deployment Group: %v", group.DeploymentGroupName)) 948 }) 949 } 950 } 951 952 func testAccCheckAWSCodeDeployDeploymentGroupExists(name string, group *codedeploy.DeploymentGroupInfo) resource.TestCheckFunc { 953 return func(s *terraform.State) error { 954 rs, ok := s.RootModule().Resources[name] 955 if !ok { 956 return fmt.Errorf("Not found: %s", name) 957 } 958 959 conn := testAccProvider.Meta().(*AWSClient).codedeployconn 960 961 resp, err := conn.GetDeploymentGroup(&codedeploy.GetDeploymentGroupInput{ 962 ApplicationName: aws.String(rs.Primary.Attributes["app_name"]), 963 DeploymentGroupName: aws.String(rs.Primary.Attributes["deployment_group_name"]), 964 }) 965 966 if err != nil { 967 return err 968 } 969 970 *group = *resp.DeploymentGroupInfo 971 972 return nil 973 } 974 } 975 976 func testAccAWSCodeDeployDeploymentGroup(rName string) string { 977 return fmt.Sprintf(` 978 resource "aws_codedeploy_app" "foo_app" { 979 name = "foo_app_%s" 980 } 981 982 resource "aws_iam_role_policy" "foo_policy" { 983 name = "foo_policy_%s" 984 role = "${aws_iam_role.foo_role.id}" 985 policy = <<EOF 986 { 987 "Version": "2012-10-17", 988 "Statement": [ 989 { 990 "Effect": "Allow", 991 "Action": [ 992 "autoscaling:CompleteLifecycleAction", 993 "autoscaling:DeleteLifecycleHook", 994 "autoscaling:DescribeAutoScalingGroups", 995 "autoscaling:DescribeLifecycleHooks", 996 "autoscaling:PutLifecycleHook", 997 "autoscaling:RecordLifecycleActionHeartbeat", 998 "ec2:DescribeInstances", 999 "ec2:DescribeInstanceStatus", 1000 "tag:GetTags", 1001 "tag:GetResources" 1002 ], 1003 "Resource": "*" 1004 } 1005 ] 1006 } 1007 EOF 1008 } 1009 1010 resource "aws_iam_role" "foo_role" { 1011 name = "foo_role_%s" 1012 assume_role_policy = <<EOF 1013 { 1014 "Version": "2012-10-17", 1015 "Statement": [ 1016 { 1017 "Sid": "", 1018 "Effect": "Allow", 1019 "Principal": { 1020 "Service": [ 1021 "codedeploy.amazonaws.com" 1022 ] 1023 }, 1024 "Action": "sts:AssumeRole" 1025 } 1026 ] 1027 } 1028 EOF 1029 } 1030 1031 resource "aws_codedeploy_deployment_group" "foo" { 1032 app_name = "${aws_codedeploy_app.foo_app.name}" 1033 deployment_group_name = "foo_%s" 1034 service_role_arn = "${aws_iam_role.foo_role.arn}" 1035 ec2_tag_filter { 1036 key = "filterkey" 1037 type = "KEY_AND_VALUE" 1038 value = "filtervalue" 1039 } 1040 }`, rName, rName, rName, rName) 1041 } 1042 1043 func testAccAWSCodeDeployDeploymentGroupModified(rName string) string { 1044 return fmt.Sprintf(` 1045 resource "aws_codedeploy_app" "foo_app" { 1046 name = "foo_app_%s" 1047 } 1048 1049 resource "aws_iam_role_policy" "foo_policy" { 1050 name = "foo_policy_%s" 1051 role = "${aws_iam_role.bar_role.id}" 1052 policy = <<EOF 1053 { 1054 "Version": "2012-10-17", 1055 "Statement": [ 1056 { 1057 "Effect": "Allow", 1058 "Action": [ 1059 "autoscaling:CompleteLifecycleAction", 1060 "autoscaling:DeleteLifecycleHook", 1061 "autoscaling:DescribeAutoScalingGroups", 1062 "autoscaling:DescribeLifecycleHooks", 1063 "autoscaling:PutLifecycleHook", 1064 "autoscaling:RecordLifecycleActionHeartbeat", 1065 "ec2:DescribeInstances", 1066 "ec2:DescribeInstanceStatus", 1067 "tag:GetTags", 1068 "tag:GetResources" 1069 ], 1070 "Resource": "*" 1071 } 1072 ] 1073 } 1074 EOF 1075 } 1076 1077 resource "aws_iam_role" "bar_role" { 1078 name = "bar_role_%s" 1079 assume_role_policy = <<EOF 1080 { 1081 "Version": "2012-10-17", 1082 "Statement": [ 1083 { 1084 "Sid": "", 1085 "Effect": "Allow", 1086 "Principal": { 1087 "Service": [ 1088 "codedeploy.amazonaws.com" 1089 ] 1090 }, 1091 "Action": "sts:AssumeRole" 1092 } 1093 ] 1094 } 1095 EOF 1096 } 1097 1098 resource "aws_codedeploy_deployment_group" "foo" { 1099 app_name = "${aws_codedeploy_app.foo_app.name}" 1100 deployment_group_name = "bar_%s" 1101 service_role_arn = "${aws_iam_role.bar_role.arn}" 1102 ec2_tag_filter { 1103 key = "filterkey" 1104 type = "KEY_AND_VALUE" 1105 value = "anotherfiltervalue" 1106 } 1107 }`, rName, rName, rName, rName) 1108 } 1109 1110 func testAccAWSCodeDeployDeploymentGroupOnPremiseTags(rName string) string { 1111 return fmt.Sprintf(` 1112 resource "aws_codedeploy_app" "foo_app" { 1113 name = "foo_app_%s" 1114 } 1115 1116 resource "aws_iam_role_policy" "foo_policy" { 1117 name = "foo_policy_%s" 1118 role = "${aws_iam_role.foo_role.id}" 1119 policy = <<EOF 1120 { 1121 "Version": "2012-10-17", 1122 "Statement": [ 1123 { 1124 "Effect": "Allow", 1125 "Action": [ 1126 "autoscaling:CompleteLifecycleAction", 1127 "autoscaling:DeleteLifecycleHook", 1128 "autoscaling:DescribeAutoScalingGroups", 1129 "autoscaling:DescribeLifecycleHooks", 1130 "autoscaling:PutLifecycleHook", 1131 "autoscaling:RecordLifecycleActionHeartbeat", 1132 "ec2:DescribeInstances", 1133 "ec2:DescribeInstanceStatus", 1134 "tag:GetTags", 1135 "tag:GetResources" 1136 ], 1137 "Resource": "*" 1138 } 1139 ] 1140 } 1141 EOF 1142 } 1143 1144 resource "aws_iam_role" "foo_role" { 1145 name = "foo_role_%s" 1146 assume_role_policy = <<EOF 1147 { 1148 "Version": "2012-10-17", 1149 "Statement": [ 1150 { 1151 "Sid": "", 1152 "Effect": "Allow", 1153 "Principal": { 1154 "Service": [ 1155 "codedeploy.amazonaws.com" 1156 ] 1157 }, 1158 "Action": "sts:AssumeRole" 1159 } 1160 ] 1161 } 1162 EOF 1163 } 1164 1165 resource "aws_codedeploy_deployment_group" "foo" { 1166 app_name = "${aws_codedeploy_app.foo_app.name}" 1167 deployment_group_name = "foo_%s" 1168 service_role_arn = "${aws_iam_role.foo_role.arn}" 1169 on_premises_instance_tag_filter { 1170 key = "filterkey" 1171 type = "KEY_AND_VALUE" 1172 value = "filtervalue" 1173 } 1174 }`, rName, rName, rName, rName) 1175 } 1176 1177 func baseCodeDeployConfig(rName string) string { 1178 return fmt.Sprintf(` 1179 resource "aws_codedeploy_app" "foo_app" { 1180 name = "foo-app-%s" 1181 } 1182 1183 resource "aws_iam_role_policy" "foo_policy" { 1184 name = "foo-policy-%s" 1185 role = "${aws_iam_role.foo_role.id}" 1186 policy = <<EOF 1187 { 1188 "Version": "2012-10-17", 1189 "Statement": [ 1190 { 1191 "Effect": "Allow", 1192 "Action": [ 1193 "autoscaling:CompleteLifecycleAction", 1194 "autoscaling:DeleteLifecycleHook", 1195 "autoscaling:DescribeAutoScalingGroups", 1196 "autoscaling:DescribeLifecycleHooks", 1197 "autoscaling:PutLifecycleHook", 1198 "autoscaling:RecordLifecycleActionHeartbeat", 1199 "codedeploy:*", 1200 "ec2:DescribeInstances", 1201 "ec2:DescribeInstanceStatus", 1202 "tag:GetTags", 1203 "tag:GetResources", 1204 "sns:Publish" 1205 ], 1206 "Resource": "*" 1207 } 1208 ] 1209 } 1210 EOF 1211 } 1212 1213 resource "aws_iam_role" "foo_role" { 1214 name = "foo-role-%s" 1215 assume_role_policy = <<EOF 1216 { 1217 "Version": "2012-10-17", 1218 "Statement": [ 1219 { 1220 "Sid": "", 1221 "Effect": "Allow", 1222 "Principal": { 1223 "Service": "codedeploy.amazonaws.com" 1224 }, 1225 "Action": "sts:AssumeRole" 1226 } 1227 ] 1228 } 1229 EOF 1230 } 1231 1232 resource "aws_sns_topic" "foo_topic" { 1233 name = "foo-topic-%s" 1234 }`, rName, rName, rName, rName) 1235 } 1236 1237 func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_create(rName string) string { 1238 return fmt.Sprintf(` 1239 1240 %s 1241 1242 resource "aws_codedeploy_deployment_group" "foo_group" { 1243 app_name = "${aws_codedeploy_app.foo_app.name}" 1244 deployment_group_name = "foo-group-%s" 1245 service_role_arn = "${aws_iam_role.foo_role.arn}" 1246 1247 trigger_configuration { 1248 trigger_events = ["DeploymentFailure"] 1249 trigger_name = "foo-trigger" 1250 trigger_target_arn = "${aws_sns_topic.foo_topic.arn}" 1251 } 1252 }`, baseCodeDeployConfig(rName), rName) 1253 } 1254 1255 func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_update(rName string) string { 1256 return fmt.Sprintf(` 1257 1258 %s 1259 1260 resource "aws_codedeploy_deployment_group" "foo_group" { 1261 app_name = "${aws_codedeploy_app.foo_app.name}" 1262 deployment_group_name = "foo-group-%s" 1263 service_role_arn = "${aws_iam_role.foo_role.arn}" 1264 1265 trigger_configuration { 1266 trigger_events = ["DeploymentSuccess", "DeploymentFailure"] 1267 trigger_name = "foo-trigger" 1268 trigger_target_arn = "${aws_sns_topic.foo_topic.arn}" 1269 } 1270 }`, baseCodeDeployConfig(rName), rName) 1271 } 1272 1273 func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_createMultiple(rName string) string { 1274 return fmt.Sprintf(` 1275 1276 %s 1277 1278 resource "aws_sns_topic" "bar_topic" { 1279 name = "bar-topic-%s" 1280 } 1281 1282 resource "aws_codedeploy_deployment_group" "foo_group" { 1283 app_name = "${aws_codedeploy_app.foo_app.name}" 1284 deployment_group_name = "foo-group-%s" 1285 service_role_arn = "${aws_iam_role.foo_role.arn}" 1286 1287 trigger_configuration { 1288 trigger_events = ["DeploymentFailure"] 1289 trigger_name = "foo-trigger" 1290 trigger_target_arn = "${aws_sns_topic.foo_topic.arn}" 1291 } 1292 1293 trigger_configuration { 1294 trigger_events = ["InstanceFailure"] 1295 trigger_name = "bar-trigger" 1296 trigger_target_arn = "${aws_sns_topic.bar_topic.arn}" 1297 } 1298 }`, baseCodeDeployConfig(rName), rName, rName) 1299 } 1300 1301 func testAccAWSCodeDeployDeploymentGroup_triggerConfiguration_updateMultiple(rName string) string { 1302 return fmt.Sprintf(` 1303 1304 %s 1305 1306 resource "aws_sns_topic" "bar_topic" { 1307 name = "bar-topic-%s" 1308 } 1309 1310 resource "aws_sns_topic" "baz_topic" { 1311 name = "baz-topic-%s" 1312 } 1313 1314 resource "aws_codedeploy_deployment_group" "foo_group" { 1315 app_name = "${aws_codedeploy_app.foo_app.name}" 1316 deployment_group_name = "foo-group-%s" 1317 service_role_arn = "${aws_iam_role.foo_role.arn}" 1318 1319 trigger_configuration { 1320 trigger_events = ["DeploymentStart", "DeploymentSuccess", "DeploymentFailure", "DeploymentStop"] 1321 trigger_name = "foo-trigger" 1322 trigger_target_arn = "${aws_sns_topic.foo_topic.arn}" 1323 } 1324 1325 trigger_configuration { 1326 trigger_events = ["InstanceFailure"] 1327 trigger_name = "bar-trigger" 1328 trigger_target_arn = "${aws_sns_topic.baz_topic.arn}" 1329 } 1330 }`, baseCodeDeployConfig(rName), rName, rName, rName) 1331 } 1332 1333 func test_config_auto_rollback_configuration_create(rName string) string { 1334 return fmt.Sprintf(` 1335 1336 %s 1337 1338 resource "aws_codedeploy_deployment_group" "foo_group" { 1339 app_name = "${aws_codedeploy_app.foo_app.name}" 1340 deployment_group_name = "foo-group-%s" 1341 service_role_arn = "${aws_iam_role.foo_role.arn}" 1342 1343 auto_rollback_configuration { 1344 enabled = true 1345 events = ["DEPLOYMENT_FAILURE"] 1346 } 1347 }`, baseCodeDeployConfig(rName), rName) 1348 } 1349 1350 func test_config_auto_rollback_configuration_update(rName string) string { 1351 return fmt.Sprintf(` 1352 1353 %s 1354 1355 resource "aws_codedeploy_deployment_group" "foo_group" { 1356 app_name = "${aws_codedeploy_app.foo_app.name}" 1357 deployment_group_name = "foo-group-%s" 1358 service_role_arn = "${aws_iam_role.foo_role.arn}" 1359 1360 auto_rollback_configuration { 1361 enabled = true 1362 events = ["DEPLOYMENT_FAILURE", "DEPLOYMENT_STOP_ON_ALARM"] 1363 } 1364 }`, baseCodeDeployConfig(rName), rName) 1365 } 1366 1367 func test_config_auto_rollback_configuration_delete(rName string) string { 1368 return fmt.Sprintf(` 1369 1370 %s 1371 1372 resource "aws_codedeploy_deployment_group" "foo_group" { 1373 app_name = "${aws_codedeploy_app.foo_app.name}" 1374 deployment_group_name = "foo-group-%s" 1375 service_role_arn = "${aws_iam_role.foo_role.arn}" 1376 }`, baseCodeDeployConfig(rName), rName) 1377 } 1378 1379 func test_config_auto_rollback_configuration_disable(rName string) string { 1380 return fmt.Sprintf(` 1381 1382 %s 1383 1384 resource "aws_codedeploy_deployment_group" "foo_group" { 1385 app_name = "${aws_codedeploy_app.foo_app.name}" 1386 deployment_group_name = "foo-group-%s" 1387 service_role_arn = "${aws_iam_role.foo_role.arn}" 1388 1389 auto_rollback_configuration { 1390 enabled = false 1391 events = ["DEPLOYMENT_FAILURE"] 1392 } 1393 }`, baseCodeDeployConfig(rName), rName) 1394 } 1395 1396 func test_config_alarm_configuration_create(rName string) string { 1397 return fmt.Sprintf(` 1398 1399 %s 1400 1401 resource "aws_codedeploy_deployment_group" "foo_group" { 1402 app_name = "${aws_codedeploy_app.foo_app.name}" 1403 deployment_group_name = "foo-group-%s" 1404 service_role_arn = "${aws_iam_role.foo_role.arn}" 1405 1406 alarm_configuration { 1407 alarms = ["foo"] 1408 enabled = true 1409 } 1410 }`, baseCodeDeployConfig(rName), rName) 1411 } 1412 1413 func test_config_alarm_configuration_update(rName string) string { 1414 return fmt.Sprintf(` 1415 1416 %s 1417 1418 resource "aws_codedeploy_deployment_group" "foo_group" { 1419 app_name = "${aws_codedeploy_app.foo_app.name}" 1420 deployment_group_name = "foo-group-%s" 1421 service_role_arn = "${aws_iam_role.foo_role.arn}" 1422 1423 alarm_configuration { 1424 alarms = ["foo", "bar"] 1425 enabled = true 1426 ignore_poll_alarm_failure = true 1427 } 1428 }`, baseCodeDeployConfig(rName), rName) 1429 } 1430 1431 func test_config_alarm_configuration_delete(rName string) string { 1432 return fmt.Sprintf(` 1433 1434 %s 1435 1436 resource "aws_codedeploy_deployment_group" "foo_group" { 1437 app_name = "${aws_codedeploy_app.foo_app.name}" 1438 deployment_group_name = "foo-group-%s" 1439 service_role_arn = "${aws_iam_role.foo_role.arn}" 1440 }`, baseCodeDeployConfig(rName), rName) 1441 } 1442 1443 func test_config_alarm_configuration_disable(rName string) string { 1444 return fmt.Sprintf(` 1445 1446 %s 1447 1448 resource "aws_codedeploy_deployment_group" "foo_group" { 1449 app_name = "${aws_codedeploy_app.foo_app.name}" 1450 deployment_group_name = "foo-group-%s" 1451 service_role_arn = "${aws_iam_role.foo_role.arn}" 1452 1453 alarm_configuration { 1454 alarms = ["foo"] 1455 enabled = false 1456 } 1457 }`, baseCodeDeployConfig(rName), rName) 1458 }