github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_elastic_transcoder_pipeline_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "reflect" 6 "sort" 7 "testing" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/aws/awserr" 11 "github.com/aws/aws-sdk-go/service/elastictranscoder" 12 "github.com/hashicorp/terraform/helper/acctest" 13 "github.com/hashicorp/terraform/helper/resource" 14 "github.com/hashicorp/terraform/terraform" 15 "regexp" 16 ) 17 18 func TestAccAWSElasticTranscoderPipeline_basic(t *testing.T) { 19 pipeline := &elastictranscoder.Pipeline{} 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 IDRefreshName: "aws_elastictranscoder_pipeline.bar", 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckElasticTranscoderPipelineDestroy, 26 Steps: []resource.TestStep{ 27 { 28 Config: awsElasticTranscoderPipelineConfigBasic, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccAWSElasticTranscoderPipeline_kmsKey(t *testing.T) { 38 pipeline := &elastictranscoder.Pipeline{} 39 ri := acctest.RandInt() 40 config := fmt.Sprintf(awsElasticTranscoderPipelineConfigKmsKey, ri) 41 keyRegex := regexp.MustCompile("^arn:aws:([a-zA-Z0-9\\-])+:([a-z]{2}-[a-z]+-\\d{1})?:(\\d{12})?:(.*)$") 42 43 resource.Test(t, resource.TestCase{ 44 PreCheck: func() { testAccPreCheck(t) }, 45 IDRefreshName: "aws_elastictranscoder_pipeline.bar", 46 Providers: testAccProviders, 47 CheckDestroy: testAccCheckElasticTranscoderPipelineDestroy, 48 Steps: []resource.TestStep{ 49 { 50 Config: config, 51 Check: resource.ComposeTestCheckFunc( 52 testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline), 53 resource.TestMatchResourceAttr("aws_elastictranscoder_pipeline.bar", "aws_kms_key_arn", keyRegex), 54 ), 55 }, 56 }, 57 }) 58 } 59 60 func TestAccAWSElasticTranscoderPipeline_notifications(t *testing.T) { 61 pipeline := elastictranscoder.Pipeline{} 62 63 rInt := acctest.RandInt() 64 65 resource.Test(t, resource.TestCase{ 66 PreCheck: func() { testAccPreCheck(t) }, 67 IDRefreshName: "aws_elastictranscoder_pipeline.bar", 68 Providers: testAccProviders, 69 CheckDestroy: testAccCheckElasticTranscoderPipelineDestroy, 70 Steps: []resource.TestStep{ 71 { 72 Config: awsElasticTranscoderNotifications(rInt), 73 Check: resource.ComposeTestCheckFunc( 74 testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", &pipeline), 75 testAccCheckAWSElasticTranscoderPipeline_notifications(&pipeline, []string{"warning", "completed"}), 76 ), 77 }, 78 79 // update and check that we have 1 less notification 80 resource.TestStep{ 81 Config: awsElasticTranscoderNotifications_update(rInt), 82 Check: resource.ComposeTestCheckFunc( 83 testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", &pipeline), 84 testAccCheckAWSElasticTranscoderPipeline_notifications(&pipeline, []string{"completed"}), 85 ), 86 }, 87 }, 88 }) 89 } 90 91 // testAccCheckTags can be used to check the tags on a resource. 92 func testAccCheckAWSElasticTranscoderPipeline_notifications( 93 p *elastictranscoder.Pipeline, notifications []string) resource.TestCheckFunc { 94 return func(s *terraform.State) error { 95 96 var notes []string 97 if p.Notifications.Completed != nil && *p.Notifications.Completed != "" { 98 notes = append(notes, "completed") 99 } 100 if p.Notifications.Error != nil && *p.Notifications.Error != "" { 101 notes = append(notes, "error") 102 } 103 if p.Notifications.Progressing != nil && *p.Notifications.Progressing != "" { 104 notes = append(notes, "progressing") 105 } 106 if p.Notifications.Warning != nil && *p.Notifications.Warning != "" { 107 notes = append(notes, "warning") 108 } 109 110 if len(notes) != len(notifications) { 111 return fmt.Errorf("ETC notifications didn't match:\n\texpected: %#v\n\tgot: %#v\n\n", notifications, notes) 112 } 113 114 sort.Strings(notes) 115 sort.Strings(notifications) 116 117 if !reflect.DeepEqual(notes, notifications) { 118 return fmt.Errorf("ETC notifications were not equal:\n\texpected: %#v\n\tgot: %#v\n\n", notifications, notes) 119 } 120 121 return nil 122 } 123 } 124 125 func TestAccAWSElasticTranscoderPipeline_withContentConfig(t *testing.T) { 126 pipeline := &elastictranscoder.Pipeline{} 127 128 resource.Test(t, resource.TestCase{ 129 PreCheck: func() { testAccPreCheck(t) }, 130 IDRefreshName: "aws_elastictranscoder_pipeline.bar", 131 Providers: testAccProviders, 132 CheckDestroy: testAccCheckElasticTranscoderPipelineDestroy, 133 Steps: []resource.TestStep{ 134 { 135 Config: awsElasticTranscoderPipelineWithContentConfig, 136 Check: resource.ComposeTestCheckFunc( 137 testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline), 138 ), 139 }, 140 { 141 Config: awsElasticTranscoderPipelineWithContentConfigUpdate, 142 Check: resource.ComposeTestCheckFunc( 143 testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.bar", pipeline), 144 ), 145 }, 146 }, 147 }) 148 } 149 150 func TestAccAWSElasticTranscoderPipeline_withPermissions(t *testing.T) { 151 pipeline := &elastictranscoder.Pipeline{} 152 153 resource.Test(t, resource.TestCase{ 154 PreCheck: func() { testAccPreCheck(t) }, 155 IDRefreshName: "aws_elastictranscoder_pipeline.baz", 156 Providers: testAccProviders, 157 CheckDestroy: testAccCheckElasticTranscoderPipelineDestroy, 158 Steps: []resource.TestStep{ 159 { 160 Config: awsElasticTranscoderPipelineWithPerms, 161 Check: resource.ComposeTestCheckFunc( 162 testAccCheckAWSElasticTranscoderPipelineExists("aws_elastictranscoder_pipeline.baz", pipeline), 163 ), 164 }, 165 }, 166 }) 167 } 168 169 func testAccCheckAWSElasticTranscoderPipelineExists(n string, res *elastictranscoder.Pipeline) resource.TestCheckFunc { 170 return func(s *terraform.State) error { 171 rs, ok := s.RootModule().Resources[n] 172 if !ok { 173 return fmt.Errorf("Not found: %s", n) 174 } 175 176 if rs.Primary.ID == "" { 177 return fmt.Errorf("No Pipeline ID is set") 178 } 179 180 conn := testAccProvider.Meta().(*AWSClient).elastictranscoderconn 181 182 out, err := conn.ReadPipeline(&elastictranscoder.ReadPipelineInput{ 183 Id: aws.String(rs.Primary.ID), 184 }) 185 186 if err != nil { 187 return err 188 } 189 190 *res = *out.Pipeline 191 192 return nil 193 } 194 } 195 196 func testAccCheckElasticTranscoderPipelineDestroy(s *terraform.State) error { 197 conn := testAccProvider.Meta().(*AWSClient).elastictranscoderconn 198 199 for _, rs := range s.RootModule().Resources { 200 if rs.Type != "aws_elastictranscoder_pipline" { 201 continue 202 } 203 204 out, err := conn.ReadPipeline(&elastictranscoder.ReadPipelineInput{ 205 Id: aws.String(rs.Primary.ID), 206 }) 207 208 if err == nil { 209 if out.Pipeline != nil && *out.Pipeline.Id == rs.Primary.ID { 210 return fmt.Errorf("Elastic Transcoder Pipeline still exists") 211 } 212 } 213 214 awsErr, ok := err.(awserr.Error) 215 if !ok { 216 return err 217 } 218 219 if awsErr.Code() != "ResourceNotFoundException" { 220 return fmt.Errorf("unexpected error: %s", awsErr) 221 } 222 223 } 224 return nil 225 } 226 227 const awsElasticTranscoderPipelineConfigBasic = ` 228 resource "aws_elastictranscoder_pipeline" "bar" { 229 input_bucket = "${aws_s3_bucket.test_bucket.bucket}" 230 output_bucket = "${aws_s3_bucket.test_bucket.bucket}" 231 name = "aws_elastictranscoder_pipeline_tf_test_" 232 role = "${aws_iam_role.test_role.arn}" 233 } 234 235 resource "aws_iam_role" "test_role" { 236 name = "aws_elastictranscoder_pipeline_tf_test_role_" 237 238 assume_role_policy = <<EOF 239 { 240 "Version": "2012-10-17", 241 "Statement": [ 242 { 243 "Action": "sts:AssumeRole", 244 "Principal": { 245 "Service": "ec2.amazonaws.com" 246 }, 247 "Effect": "Allow", 248 "Sid": "" 249 } 250 ] 251 } 252 EOF 253 } 254 255 resource "aws_s3_bucket" "test_bucket" { 256 bucket = "aws-elasticencoder-pipeline-tf-test-bucket" 257 acl = "private" 258 } 259 ` 260 261 const awsElasticTranscoderPipelineConfigKmsKey = ` 262 resource "aws_kms_key" "foo" { 263 description = "Terraform acc test %d" 264 policy = <<POLICY 265 { 266 "Version": "2012-10-17", 267 "Id": "kms-tf-1", 268 "Statement": [ 269 { 270 "Sid": "Enable IAM User Permissions", 271 "Effect": "Allow", 272 "Principal": { 273 "AWS": "*" 274 }, 275 "Action": "kms:*", 276 "Resource": "*" 277 } 278 ] 279 } 280 POLICY 281 } 282 283 resource "aws_elastictranscoder_pipeline" "bar" { 284 input_bucket = "${aws_s3_bucket.test_bucket.bucket}" 285 output_bucket = "${aws_s3_bucket.test_bucket.bucket}" 286 name = "aws_elastictranscoder_pipeline_tf_test_" 287 role = "${aws_iam_role.test_role.arn}" 288 aws_kms_key_arn = "${aws_kms_key.foo.arn}" 289 } 290 291 resource "aws_iam_role" "test_role" { 292 name = "aws_elastictranscoder_pipeline_tf_test_role_" 293 294 assume_role_policy = <<EOF 295 { 296 "Version": "2012-10-17", 297 "Statement": [ 298 { 299 "Action": "sts:AssumeRole", 300 "Principal": { 301 "Service": "ec2.amazonaws.com" 302 }, 303 "Effect": "Allow", 304 "Sid": "" 305 } 306 ] 307 } 308 EOF 309 } 310 311 resource "aws_s3_bucket" "test_bucket" { 312 bucket = "aws-elasticencoder-pipeline-tf-test-bucket" 313 acl = "private" 314 } 315 ` 316 317 const awsElasticTranscoderPipelineWithContentConfig = ` 318 resource "aws_elastictranscoder_pipeline" "bar" { 319 input_bucket = "${aws_s3_bucket.content_bucket.bucket}" 320 name = "aws_elastictranscoder_pipeline_tf_test_" 321 role = "${aws_iam_role.test_role.arn}" 322 323 content_config { 324 bucket = "${aws_s3_bucket.content_bucket.bucket}" 325 storage_class = "Standard" 326 } 327 328 thumbnail_config { 329 bucket = "${aws_s3_bucket.content_bucket.bucket}" 330 storage_class = "Standard" 331 } 332 } 333 334 resource "aws_iam_role" "test_role" { 335 name = "aws_elastictranscoder_pipeline_tf_test_role_" 336 337 assume_role_policy = <<EOF 338 { 339 "Version": "2012-10-17", 340 "Statement": [ 341 { 342 "Action": "sts:AssumeRole", 343 "Principal": { 344 "Service": "ec2.amazonaws.com" 345 }, 346 "Effect": "Allow", 347 "Sid": "" 348 } 349 ] 350 } 351 EOF 352 } 353 354 resource "aws_s3_bucket" "content_bucket" { 355 bucket = "aws-elasticencoder-pipeline-tf-content-bucket" 356 acl = "private" 357 } 358 359 resource "aws_s3_bucket" "input_bucket" { 360 bucket = "aws-elasticencoder-pipeline-tf-input-bucket" 361 acl = "private" 362 } 363 364 resource "aws_s3_bucket" "thumb_bucket" { 365 bucket = "aws-elasticencoder-pipeline-tf-thumb-bucket" 366 acl = "private" 367 } 368 ` 369 370 const awsElasticTranscoderPipelineWithContentConfigUpdate = ` 371 resource "aws_elastictranscoder_pipeline" "bar" { 372 input_bucket = "${aws_s3_bucket.input_bucket.bucket}" 373 name = "aws_elastictranscoder_pipeline_tf_test_" 374 role = "${aws_iam_role.test_role.arn}" 375 376 content_config { 377 bucket = "${aws_s3_bucket.content_bucket.bucket}" 378 storage_class = "Standard" 379 } 380 381 thumbnail_config { 382 bucket = "${aws_s3_bucket.thumb_bucket.bucket}" 383 storage_class = "Standard" 384 } 385 } 386 387 resource "aws_iam_role" "test_role" { 388 name = "aws_elastictranscoder_pipeline_tf_test_role_" 389 390 assume_role_policy = <<EOF 391 { 392 "Version": "2012-10-17", 393 "Statement": [ 394 { 395 "Action": "sts:AssumeRole", 396 "Principal": { 397 "Service": "ec2.amazonaws.com" 398 }, 399 "Effect": "Allow", 400 "Sid": "" 401 } 402 ] 403 } 404 EOF 405 } 406 407 resource "aws_s3_bucket" "content_bucket" { 408 bucket = "aws-elasticencoder-pipeline-tf-content-bucket" 409 acl = "private" 410 } 411 412 resource "aws_s3_bucket" "input_bucket" { 413 bucket = "aws-elasticencoder-pipeline-tf-input-bucket" 414 acl = "private" 415 } 416 417 resource "aws_s3_bucket" "thumb_bucket" { 418 bucket = "aws-elasticencoder-pipeline-tf-thumb-bucket" 419 acl = "private" 420 } 421 ` 422 423 const awsElasticTranscoderPipelineWithPerms = ` 424 resource "aws_elastictranscoder_pipeline" "baz" { 425 input_bucket = "${aws_s3_bucket.content_bucket.bucket}" 426 name = "aws_elastictranscoder_pipeline_tf_test_" 427 role = "${aws_iam_role.test_role.arn}" 428 429 content_config { 430 bucket = "${aws_s3_bucket.content_bucket.bucket}" 431 storage_class = "Standard" 432 } 433 434 content_config_permissions = { 435 grantee_type = "Group" 436 grantee = "AuthenticatedUsers" 437 access = ["FullControl"] 438 } 439 440 thumbnail_config { 441 bucket = "${aws_s3_bucket.content_bucket.bucket}" 442 storage_class = "Standard" 443 } 444 445 thumbnail_config_permissions = { 446 grantee_type = "Group" 447 grantee = "AuthenticatedUsers" 448 access = ["FullControl"] 449 } 450 } 451 452 resource "aws_iam_role" "test_role" { 453 name = "aws_elastictranscoder_pipeline_tf_test_role_" 454 455 assume_role_policy = <<EOF 456 { 457 "Version": "2012-10-17", 458 "Statement": [ 459 { 460 "Action": "sts:AssumeRole", 461 "Principal": { 462 "Service": "ec2.amazonaws.com" 463 }, 464 "Effect": "Allow", 465 "Sid": "" 466 } 467 ] 468 } 469 EOF 470 } 471 472 resource "aws_s3_bucket" "content_bucket" { 473 bucket = "aws-elasticencoder-pipeline-tf-content-bucket" 474 acl = "private" 475 } 476 ` 477 478 func awsElasticTranscoderNotifications(r int) string { 479 return fmt.Sprintf(` 480 resource "aws_elastictranscoder_pipeline" "bar" { 481 input_bucket = "${aws_s3_bucket.test_bucket.bucket}" 482 output_bucket = "${aws_s3_bucket.test_bucket.bucket}" 483 name = "tf-transcoder-%d" 484 role = "${aws_iam_role.test_role.arn}" 485 486 notifications { 487 completed = "${aws_sns_topic.topic_example.arn}" 488 warning = "${aws_sns_topic.topic_example.arn}" 489 } 490 } 491 492 resource "aws_iam_role" "test_role" { 493 name = "tf-transcoder-%d" 494 495 assume_role_policy = <<EOF 496 { 497 "Version": "2012-10-17", 498 "Statement": [ 499 { 500 "Action": "sts:AssumeRole", 501 "Principal": { 502 "Service": "ec2.amazonaws.com" 503 }, 504 "Effect": "Allow", 505 "Sid": "" 506 } 507 ] 508 } 509 EOF 510 } 511 512 resource "aws_s3_bucket" "test_bucket" { 513 bucket = "tf-transcoder-%d" 514 acl = "private" 515 } 516 517 resource "aws_sns_topic" "topic_example" { 518 name = "tf-transcoder-%d" 519 520 policy = <<EOF 521 { 522 "Version": "2012-10-17", 523 "Id": "AWSAccountTopicAccess", 524 "Statement": [ 525 { 526 "Sid": "*", 527 "Effect": "Allow", 528 "Principal": "*", 529 "Action": "sns:Publish", 530 "Resource": "*" 531 } 532 ] 533 } 534 EOF 535 }`, r, r, r, r) 536 } 537 538 func awsElasticTranscoderNotifications_update(r int) string { 539 return fmt.Sprintf(` 540 resource "aws_elastictranscoder_pipeline" "bar" { 541 input_bucket = "${aws_s3_bucket.test_bucket.bucket}" 542 output_bucket = "${aws_s3_bucket.test_bucket.bucket}" 543 name = "tf-transcoder-%d" 544 role = "${aws_iam_role.test_role.arn}" 545 546 notifications { 547 completed = "${aws_sns_topic.topic_example.arn}" 548 } 549 } 550 551 resource "aws_iam_role" "test_role" { 552 name = "tf-transcoder-%d" 553 554 assume_role_policy = <<EOF 555 { 556 "Version": "2012-10-17", 557 "Statement": [ 558 { 559 "Action": "sts:AssumeRole", 560 "Principal": { 561 "Service": "ec2.amazonaws.com" 562 }, 563 "Effect": "Allow", 564 "Sid": "" 565 } 566 ] 567 } 568 EOF 569 } 570 571 resource "aws_s3_bucket" "test_bucket" { 572 bucket = "tf-transcoder-%d" 573 acl = "private" 574 } 575 576 resource "aws_sns_topic" "topic_example" { 577 name = "tf-transcoder-%d" 578 579 policy = <<EOF 580 { 581 "Version": "2012-10-17", 582 "Id": "AWSAccountTopicAccess", 583 "Statement": [ 584 { 585 "Sid": "*", 586 "Effect": "Allow", 587 "Principal": "*", 588 "Action": "sns:Publish", 589 "Resource": "*" 590 } 591 ] 592 } 593 EOF 594 }`, r, r, r, r) 595 }