github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_s3_bucket_test.go (about) 1 package aws 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "reflect" 7 "regexp" 8 "strconv" 9 "testing" 10 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 15 "strings" 16 17 "github.com/aws/aws-sdk-go/aws" 18 "github.com/aws/aws-sdk-go/aws/awserr" 19 "github.com/aws/aws-sdk-go/service/s3" 20 "github.com/hashicorp/terraform/helper/schema" 21 ) 22 23 func TestAccAWSS3Bucket_basic(t *testing.T) { 24 rInt := acctest.RandInt() 25 arnRegexp := regexp.MustCompile( 26 "^arn:aws:s3:::") 27 28 resource.Test(t, resource.TestCase{ 29 PreCheck: func() { testAccPreCheck(t) }, 30 /* 31 IDRefreshName: "aws_s3_bucket.bucket", 32 IDRefreshIgnore: []string{"force_destroy"}, 33 */ 34 Providers: testAccProviders, 35 CheckDestroy: testAccCheckAWSS3BucketDestroy, 36 Steps: []resource.TestStep{ 37 { 38 Config: testAccAWSS3BucketConfig(rInt), 39 Check: resource.ComposeTestCheckFunc( 40 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 41 resource.TestCheckResourceAttr( 42 "aws_s3_bucket.bucket", "hosted_zone_id", HostedZoneIDForRegion("us-west-2")), 43 resource.TestCheckResourceAttr( 44 "aws_s3_bucket.bucket", "region", "us-west-2"), 45 resource.TestCheckNoResourceAttr( 46 "aws_s3_bucket.bucket", "website_endpoint"), 47 resource.TestMatchResourceAttr( 48 "aws_s3_bucket.bucket", "arn", arnRegexp), 49 resource.TestCheckResourceAttr( 50 "aws_s3_bucket.bucket", "bucket", testAccBucketName(rInt)), 51 resource.TestCheckResourceAttr( 52 "aws_s3_bucket.bucket", "bucket_domain_name", testAccBucketDomainName(rInt)), 53 ), 54 }, 55 }, 56 }) 57 } 58 59 func TestAccAWSS3Bucket_namePrefix(t *testing.T) { 60 resource.Test(t, resource.TestCase{ 61 PreCheck: func() { testAccPreCheck(t) }, 62 Providers: testAccProviders, 63 CheckDestroy: testAccCheckAWSS3BucketDestroy, 64 Steps: []resource.TestStep{ 65 { 66 Config: testAccAWSS3BucketConfig_namePrefix, 67 Check: resource.ComposeTestCheckFunc( 68 testAccCheckAWSS3BucketExists("aws_s3_bucket.test"), 69 resource.TestMatchResourceAttr( 70 "aws_s3_bucket.test", "bucket", regexp.MustCompile("^tf-test-")), 71 ), 72 }, 73 }, 74 }) 75 } 76 77 func TestAccAWSS3Bucket_generatedName(t *testing.T) { 78 resource.Test(t, resource.TestCase{ 79 PreCheck: func() { testAccPreCheck(t) }, 80 Providers: testAccProviders, 81 CheckDestroy: testAccCheckAWSS3BucketDestroy, 82 Steps: []resource.TestStep{ 83 { 84 Config: testAccAWSS3BucketConfig_generatedName, 85 Check: resource.ComposeTestCheckFunc( 86 testAccCheckAWSS3BucketExists("aws_s3_bucket.test"), 87 ), 88 }, 89 }, 90 }) 91 } 92 93 func TestAccAWSS3Bucket_region(t *testing.T) { 94 rInt := acctest.RandInt() 95 96 resource.Test(t, resource.TestCase{ 97 PreCheck: func() { testAccPreCheck(t) }, 98 Providers: testAccProviders, 99 CheckDestroy: testAccCheckAWSS3BucketDestroy, 100 Steps: []resource.TestStep{ 101 { 102 Config: testAccAWSS3BucketConfigWithRegion(rInt), 103 Check: resource.ComposeTestCheckFunc( 104 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 105 resource.TestCheckResourceAttr("aws_s3_bucket.bucket", "region", "eu-west-1"), 106 ), 107 }, 108 }, 109 }) 110 } 111 112 func TestAccAWSS3Bucket_acceleration(t *testing.T) { 113 rInt := acctest.RandInt() 114 115 resource.Test(t, resource.TestCase{ 116 PreCheck: func() { testAccPreCheck(t) }, 117 Providers: testAccProviders, 118 CheckDestroy: testAccCheckAWSS3BucketDestroy, 119 Steps: []resource.TestStep{ 120 { 121 Config: testAccAWSS3BucketConfigWithAcceleration(rInt), 122 Check: resource.ComposeTestCheckFunc( 123 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 124 resource.TestCheckResourceAttr( 125 "aws_s3_bucket.bucket", "acceleration_status", "Enabled"), 126 ), 127 }, 128 { 129 Config: testAccAWSS3BucketConfigWithoutAcceleration(rInt), 130 Check: resource.ComposeTestCheckFunc( 131 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 132 resource.TestCheckResourceAttr( 133 "aws_s3_bucket.bucket", "acceleration_status", "Suspended"), 134 ), 135 }, 136 }, 137 }) 138 } 139 140 func TestAccAWSS3Bucket_RequestPayer(t *testing.T) { 141 rInt := acctest.RandInt() 142 143 resource.Test(t, resource.TestCase{ 144 PreCheck: func() { testAccPreCheck(t) }, 145 Providers: testAccProviders, 146 CheckDestroy: testAccCheckAWSS3BucketDestroy, 147 Steps: []resource.TestStep{ 148 { 149 Config: testAccAWSS3BucketConfigRequestPayerBucketOwner(rInt), 150 Check: resource.ComposeTestCheckFunc( 151 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 152 resource.TestCheckResourceAttr( 153 "aws_s3_bucket.bucket", 154 "request_payer", 155 "BucketOwner"), 156 testAccCheckAWSS3RequestPayer( 157 "aws_s3_bucket.bucket", 158 "BucketOwner"), 159 ), 160 }, 161 { 162 Config: testAccAWSS3BucketConfigRequestPayerRequester(rInt), 163 Check: resource.ComposeTestCheckFunc( 164 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 165 resource.TestCheckResourceAttr( 166 "aws_s3_bucket.bucket", 167 "request_payer", 168 "Requester"), 169 testAccCheckAWSS3RequestPayer( 170 "aws_s3_bucket.bucket", 171 "Requester"), 172 ), 173 }, 174 }, 175 }) 176 } 177 178 func TestResourceAWSS3BucketRequestPayer_validation(t *testing.T) { 179 _, errors := validateS3BucketRequestPayerType("incorrect", "request_payer") 180 if len(errors) == 0 { 181 t.Fatalf("Expected to trigger a validation error") 182 } 183 184 var testCases = []struct { 185 Value string 186 ErrCount int 187 }{ 188 { 189 Value: "Requester", 190 ErrCount: 0, 191 }, 192 { 193 Value: "BucketOwner", 194 ErrCount: 0, 195 }, 196 } 197 198 for _, tc := range testCases { 199 _, errors := validateS3BucketRequestPayerType(tc.Value, "request_payer") 200 if len(errors) != tc.ErrCount { 201 t.Fatalf("Expected not to trigger a validation error") 202 } 203 } 204 } 205 206 func TestAccAWSS3Bucket_Policy(t *testing.T) { 207 rInt := acctest.RandInt() 208 209 resource.Test(t, resource.TestCase{ 210 PreCheck: func() { testAccPreCheck(t) }, 211 Providers: testAccProviders, 212 CheckDestroy: testAccCheckAWSS3BucketDestroy, 213 Steps: []resource.TestStep{ 214 { 215 Config: testAccAWSS3BucketConfigWithPolicy(rInt), 216 Check: resource.ComposeTestCheckFunc( 217 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 218 testAccCheckAWSS3BucketPolicy( 219 "aws_s3_bucket.bucket", testAccAWSS3BucketPolicy(rInt)), 220 ), 221 }, 222 { 223 Config: testAccAWSS3BucketConfig(rInt), 224 Check: resource.ComposeTestCheckFunc( 225 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 226 testAccCheckAWSS3BucketPolicy( 227 "aws_s3_bucket.bucket", ""), 228 ), 229 }, 230 { 231 Config: testAccAWSS3BucketConfigWithEmptyPolicy(rInt), 232 Check: resource.ComposeTestCheckFunc( 233 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 234 testAccCheckAWSS3BucketPolicy( 235 "aws_s3_bucket.bucket", ""), 236 ), 237 }, 238 }, 239 }) 240 } 241 242 func TestAccAWSS3Bucket_UpdateAcl(t *testing.T) { 243 ri := acctest.RandInt() 244 preConfig := fmt.Sprintf(testAccAWSS3BucketConfigWithAcl, ri) 245 postConfig := fmt.Sprintf(testAccAWSS3BucketConfigWithAclUpdate, ri) 246 247 resource.Test(t, resource.TestCase{ 248 PreCheck: func() { testAccPreCheck(t) }, 249 Providers: testAccProviders, 250 CheckDestroy: testAccCheckAWSS3BucketDestroy, 251 Steps: []resource.TestStep{ 252 { 253 Config: preConfig, 254 Check: resource.ComposeTestCheckFunc( 255 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 256 resource.TestCheckResourceAttr( 257 "aws_s3_bucket.bucket", "acl", "public-read"), 258 ), 259 }, 260 { 261 Config: postConfig, 262 Check: resource.ComposeTestCheckFunc( 263 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 264 resource.TestCheckResourceAttr( 265 "aws_s3_bucket.bucket", "acl", "private"), 266 ), 267 }, 268 }, 269 }) 270 } 271 272 func TestAccAWSS3Bucket_Website_Simple(t *testing.T) { 273 rInt := acctest.RandInt() 274 resource.Test(t, resource.TestCase{ 275 PreCheck: func() { testAccPreCheck(t) }, 276 Providers: testAccProviders, 277 CheckDestroy: testAccCheckAWSS3BucketDestroy, 278 Steps: []resource.TestStep{ 279 { 280 Config: testAccAWSS3BucketWebsiteConfig(rInt), 281 Check: resource.ComposeTestCheckFunc( 282 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 283 testAccCheckAWSS3BucketWebsite( 284 "aws_s3_bucket.bucket", "index.html", "", "", ""), 285 resource.TestCheckResourceAttr( 286 "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt)), 287 ), 288 }, 289 { 290 Config: testAccAWSS3BucketWebsiteConfigWithError(rInt), 291 Check: resource.ComposeTestCheckFunc( 292 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 293 testAccCheckAWSS3BucketWebsite( 294 "aws_s3_bucket.bucket", "index.html", "error.html", "", ""), 295 resource.TestCheckResourceAttr( 296 "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt)), 297 ), 298 }, 299 { 300 Config: testAccAWSS3BucketConfig(rInt), 301 Check: resource.ComposeTestCheckFunc( 302 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 303 testAccCheckAWSS3BucketWebsite( 304 "aws_s3_bucket.bucket", "", "", "", ""), 305 resource.TestCheckResourceAttr( 306 "aws_s3_bucket.bucket", "website_endpoint", ""), 307 ), 308 }, 309 }, 310 }) 311 } 312 313 func TestAccAWSS3Bucket_WebsiteRedirect(t *testing.T) { 314 rInt := acctest.RandInt() 315 resource.Test(t, resource.TestCase{ 316 PreCheck: func() { testAccPreCheck(t) }, 317 Providers: testAccProviders, 318 CheckDestroy: testAccCheckAWSS3BucketDestroy, 319 Steps: []resource.TestStep{ 320 { 321 Config: testAccAWSS3BucketWebsiteConfigWithRedirect(rInt), 322 Check: resource.ComposeTestCheckFunc( 323 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 324 testAccCheckAWSS3BucketWebsite( 325 "aws_s3_bucket.bucket", "", "", "", "hashicorp.com"), 326 resource.TestCheckResourceAttr( 327 "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt)), 328 ), 329 }, 330 { 331 Config: testAccAWSS3BucketWebsiteConfigWithHttpsRedirect(rInt), 332 Check: resource.ComposeTestCheckFunc( 333 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 334 testAccCheckAWSS3BucketWebsite( 335 "aws_s3_bucket.bucket", "", "", "https", "hashicorp.com"), 336 resource.TestCheckResourceAttr( 337 "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt)), 338 ), 339 }, 340 { 341 Config: testAccAWSS3BucketConfig(rInt), 342 Check: resource.ComposeTestCheckFunc( 343 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 344 testAccCheckAWSS3BucketWebsite( 345 "aws_s3_bucket.bucket", "", "", "", ""), 346 resource.TestCheckResourceAttr( 347 "aws_s3_bucket.bucket", "website_endpoint", ""), 348 ), 349 }, 350 }, 351 }) 352 } 353 354 func TestAccAWSS3Bucket_WebsiteRoutingRules(t *testing.T) { 355 rInt := acctest.RandInt() 356 resource.Test(t, resource.TestCase{ 357 PreCheck: func() { testAccPreCheck(t) }, 358 Providers: testAccProviders, 359 CheckDestroy: testAccCheckAWSS3BucketDestroy, 360 Steps: []resource.TestStep{ 361 { 362 Config: testAccAWSS3BucketWebsiteConfigWithRoutingRules(rInt), 363 Check: resource.ComposeTestCheckFunc( 364 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 365 testAccCheckAWSS3BucketWebsite( 366 "aws_s3_bucket.bucket", "index.html", "error.html", "", ""), 367 testAccCheckAWSS3BucketWebsiteRoutingRules( 368 "aws_s3_bucket.bucket", 369 []*s3.RoutingRule{ 370 { 371 Condition: &s3.Condition{ 372 KeyPrefixEquals: aws.String("docs/"), 373 }, 374 Redirect: &s3.Redirect{ 375 ReplaceKeyPrefixWith: aws.String("documents/"), 376 }, 377 }, 378 }, 379 ), 380 resource.TestCheckResourceAttr( 381 "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt)), 382 ), 383 }, 384 { 385 Config: testAccAWSS3BucketConfig(rInt), 386 Check: resource.ComposeTestCheckFunc( 387 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 388 testAccCheckAWSS3BucketWebsite( 389 "aws_s3_bucket.bucket", "", "", "", ""), 390 testAccCheckAWSS3BucketWebsiteRoutingRules("aws_s3_bucket.bucket", nil), 391 resource.TestCheckResourceAttr( 392 "aws_s3_bucket.bucket", "website_endpoint", ""), 393 ), 394 }, 395 }, 396 }) 397 } 398 399 // Test TestAccAWSS3Bucket_shouldFailNotFound is designed to fail with a "plan 400 // not empty" error in Terraform, to check against regresssions. 401 // See https://github.com/hashicorp/terraform/pull/2925 402 func TestAccAWSS3Bucket_shouldFailNotFound(t *testing.T) { 403 rInt := acctest.RandInt() 404 resource.Test(t, resource.TestCase{ 405 PreCheck: func() { testAccPreCheck(t) }, 406 Providers: testAccProviders, 407 CheckDestroy: testAccCheckAWSS3BucketDestroy, 408 Steps: []resource.TestStep{ 409 { 410 Config: testAccAWSS3BucketDestroyedConfig(rInt), 411 Check: resource.ComposeTestCheckFunc( 412 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 413 testAccCheckAWSS3DestroyBucket("aws_s3_bucket.bucket"), 414 ), 415 ExpectNonEmptyPlan: true, 416 }, 417 }, 418 }) 419 } 420 421 func TestAccAWSS3Bucket_Versioning(t *testing.T) { 422 rInt := acctest.RandInt() 423 resource.Test(t, resource.TestCase{ 424 PreCheck: func() { testAccPreCheck(t) }, 425 Providers: testAccProviders, 426 CheckDestroy: testAccCheckAWSS3BucketDestroy, 427 Steps: []resource.TestStep{ 428 { 429 Config: testAccAWSS3BucketConfig(rInt), 430 Check: resource.ComposeTestCheckFunc( 431 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 432 testAccCheckAWSS3BucketVersioning( 433 "aws_s3_bucket.bucket", ""), 434 ), 435 }, 436 { 437 Config: testAccAWSS3BucketConfigWithVersioning(rInt), 438 Check: resource.ComposeTestCheckFunc( 439 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 440 testAccCheckAWSS3BucketVersioning( 441 "aws_s3_bucket.bucket", s3.BucketVersioningStatusEnabled), 442 ), 443 }, 444 { 445 Config: testAccAWSS3BucketConfigWithDisableVersioning(rInt), 446 Check: resource.ComposeTestCheckFunc( 447 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 448 testAccCheckAWSS3BucketVersioning( 449 "aws_s3_bucket.bucket", s3.BucketVersioningStatusSuspended), 450 ), 451 }, 452 }, 453 }) 454 } 455 456 func TestAccAWSS3Bucket_Cors(t *testing.T) { 457 rInt := acctest.RandInt() 458 459 updateBucketCors := func(n string) resource.TestCheckFunc { 460 return func(s *terraform.State) error { 461 rs, ok := s.RootModule().Resources[n] 462 if !ok { 463 return fmt.Errorf("Not found: %s", n) 464 } 465 466 conn := testAccProvider.Meta().(*AWSClient).s3conn 467 _, err := conn.PutBucketCors(&s3.PutBucketCorsInput{ 468 Bucket: aws.String(rs.Primary.ID), 469 CORSConfiguration: &s3.CORSConfiguration{ 470 CORSRules: []*s3.CORSRule{ 471 { 472 AllowedHeaders: []*string{aws.String("*")}, 473 AllowedMethods: []*string{aws.String("GET")}, 474 AllowedOrigins: []*string{aws.String("https://www.example.com")}, 475 }, 476 }, 477 }, 478 }) 479 if err != nil { 480 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() != "NoSuchCORSConfiguration" { 481 return err 482 } 483 } 484 return nil 485 } 486 } 487 488 resource.Test(t, resource.TestCase{ 489 PreCheck: func() { testAccPreCheck(t) }, 490 Providers: testAccProviders, 491 CheckDestroy: testAccCheckAWSS3BucketDestroy, 492 Steps: []resource.TestStep{ 493 { 494 Config: testAccAWSS3BucketConfigWithCORS(rInt), 495 Check: resource.ComposeTestCheckFunc( 496 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 497 testAccCheckAWSS3BucketCors( 498 "aws_s3_bucket.bucket", 499 []*s3.CORSRule{ 500 { 501 AllowedHeaders: []*string{aws.String("*")}, 502 AllowedMethods: []*string{aws.String("PUT"), aws.String("POST")}, 503 AllowedOrigins: []*string{aws.String("https://www.example.com")}, 504 ExposeHeaders: []*string{aws.String("x-amz-server-side-encryption"), aws.String("ETag")}, 505 MaxAgeSeconds: aws.Int64(3000), 506 }, 507 }, 508 ), 509 updateBucketCors("aws_s3_bucket.bucket"), 510 ), 511 ExpectNonEmptyPlan: true, 512 }, 513 { 514 Config: testAccAWSS3BucketConfigWithCORS(rInt), 515 Check: resource.ComposeTestCheckFunc( 516 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 517 testAccCheckAWSS3BucketCors( 518 "aws_s3_bucket.bucket", 519 []*s3.CORSRule{ 520 { 521 AllowedHeaders: []*string{aws.String("*")}, 522 AllowedMethods: []*string{aws.String("PUT"), aws.String("POST")}, 523 AllowedOrigins: []*string{aws.String("https://www.example.com")}, 524 ExposeHeaders: []*string{aws.String("x-amz-server-side-encryption"), aws.String("ETag")}, 525 MaxAgeSeconds: aws.Int64(3000), 526 }, 527 }, 528 ), 529 ), 530 }, 531 }, 532 }) 533 } 534 535 func TestAccAWSS3Bucket_Logging(t *testing.T) { 536 rInt := acctest.RandInt() 537 resource.Test(t, resource.TestCase{ 538 PreCheck: func() { testAccPreCheck(t) }, 539 Providers: testAccProviders, 540 CheckDestroy: testAccCheckAWSS3BucketDestroy, 541 Steps: []resource.TestStep{ 542 { 543 Config: testAccAWSS3BucketConfigWithLogging(rInt), 544 Check: resource.ComposeTestCheckFunc( 545 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 546 testAccCheckAWSS3BucketLogging( 547 "aws_s3_bucket.bucket", "aws_s3_bucket.log_bucket", "log/"), 548 ), 549 }, 550 }, 551 }) 552 } 553 554 func TestAccAWSS3Bucket_Lifecycle(t *testing.T) { 555 rInt := acctest.RandInt() 556 resource.Test(t, resource.TestCase{ 557 PreCheck: func() { testAccPreCheck(t) }, 558 Providers: testAccProviders, 559 CheckDestroy: testAccCheckAWSS3BucketDestroy, 560 Steps: []resource.TestStep{ 561 { 562 Config: testAccAWSS3BucketConfigWithLifecycle(rInt), 563 Check: resource.ComposeTestCheckFunc( 564 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 565 resource.TestCheckResourceAttr( 566 "aws_s3_bucket.bucket", "lifecycle_rule.0.id", "id1"), 567 resource.TestCheckResourceAttr( 568 "aws_s3_bucket.bucket", "lifecycle_rule.0.prefix", "path1/"), 569 resource.TestCheckResourceAttr( 570 "aws_s3_bucket.bucket", "lifecycle_rule.0.expiration.2613713285.days", "365"), 571 resource.TestCheckResourceAttr( 572 "aws_s3_bucket.bucket", "lifecycle_rule.0.expiration.2613713285.date", ""), 573 resource.TestCheckResourceAttr( 574 "aws_s3_bucket.bucket", "lifecycle_rule.0.expiration.2613713285.expired_object_delete_marker", "false"), 575 resource.TestCheckResourceAttr( 576 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.2000431762.date", ""), 577 resource.TestCheckResourceAttr( 578 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.2000431762.days", "30"), 579 resource.TestCheckResourceAttr( 580 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.2000431762.storage_class", "STANDARD_IA"), 581 resource.TestCheckResourceAttr( 582 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.6450812.date", ""), 583 resource.TestCheckResourceAttr( 584 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.6450812.days", "60"), 585 resource.TestCheckResourceAttr( 586 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.6450812.storage_class", "GLACIER"), 587 resource.TestCheckResourceAttr( 588 "aws_s3_bucket.bucket", "lifecycle_rule.1.id", "id2"), 589 resource.TestCheckResourceAttr( 590 "aws_s3_bucket.bucket", "lifecycle_rule.1.prefix", "path2/"), 591 resource.TestCheckResourceAttr( 592 "aws_s3_bucket.bucket", "lifecycle_rule.1.expiration.2855832418.date", "2016-01-12"), 593 resource.TestCheckResourceAttr( 594 "aws_s3_bucket.bucket", "lifecycle_rule.1.expiration.2855832418.days", "0"), 595 resource.TestCheckResourceAttr( 596 "aws_s3_bucket.bucket", "lifecycle_rule.1.expiration.2855832418.expired_object_delete_marker", "false"), 597 ), 598 }, 599 { 600 Config: testAccAWSS3BucketConfigWithVersioningLifecycle(rInt), 601 Check: resource.ComposeTestCheckFunc( 602 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 603 resource.TestCheckResourceAttr( 604 "aws_s3_bucket.bucket", "lifecycle_rule.0.id", "id1"), 605 resource.TestCheckResourceAttr( 606 "aws_s3_bucket.bucket", "lifecycle_rule.0.prefix", "path1/"), 607 resource.TestCheckResourceAttr( 608 "aws_s3_bucket.bucket", "lifecycle_rule.0.enabled", "true"), 609 resource.TestCheckResourceAttr( 610 "aws_s3_bucket.bucket", "lifecycle_rule.0.noncurrent_version_expiration.80908210.days", "365"), 611 resource.TestCheckResourceAttr( 612 "aws_s3_bucket.bucket", "lifecycle_rule.0.noncurrent_version_transition.1377917700.days", "30"), 613 resource.TestCheckResourceAttr( 614 "aws_s3_bucket.bucket", "lifecycle_rule.0.noncurrent_version_transition.1377917700.storage_class", "STANDARD_IA"), 615 resource.TestCheckResourceAttr( 616 "aws_s3_bucket.bucket", "lifecycle_rule.0.noncurrent_version_transition.2528035817.days", "60"), 617 resource.TestCheckResourceAttr( 618 "aws_s3_bucket.bucket", "lifecycle_rule.0.noncurrent_version_transition.2528035817.storage_class", "GLACIER"), 619 resource.TestCheckResourceAttr( 620 "aws_s3_bucket.bucket", "lifecycle_rule.1.id", "id2"), 621 resource.TestCheckResourceAttr( 622 "aws_s3_bucket.bucket", "lifecycle_rule.1.prefix", "path2/"), 623 resource.TestCheckResourceAttr( 624 "aws_s3_bucket.bucket", "lifecycle_rule.1.enabled", "false"), 625 resource.TestCheckResourceAttr( 626 "aws_s3_bucket.bucket", "lifecycle_rule.1.noncurrent_version_expiration.80908210.days", "365"), 627 ), 628 }, 629 { 630 Config: testAccAWSS3BucketConfig(rInt), 631 Check: resource.ComposeTestCheckFunc( 632 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 633 ), 634 }, 635 }, 636 }) 637 } 638 639 func TestAccAWSS3Bucket_Replication(t *testing.T) { 640 rInt := acctest.RandInt() 641 642 // record the initialized providers so that we can use them to check for the instances in each region 643 var providers []*schema.Provider 644 providerFactories := map[string]terraform.ResourceProviderFactory{ 645 "aws": func() (terraform.ResourceProvider, error) { 646 p := Provider() 647 providers = append(providers, p.(*schema.Provider)) 648 return p, nil 649 }, 650 } 651 652 resource.Test(t, resource.TestCase{ 653 PreCheck: func() { testAccPreCheck(t) }, 654 ProviderFactories: providerFactories, 655 CheckDestroy: testAccCheckAWSS3BucketDestroyWithProviders(&providers), 656 Steps: []resource.TestStep{ 657 { 658 Config: testAccAWSS3BucketConfigReplication(rInt), 659 Check: resource.ComposeTestCheckFunc( 660 testAccCheckAWSS3BucketExistsWithProviders("aws_s3_bucket.bucket", &providers), 661 ), 662 }, 663 { 664 Config: testAccAWSS3BucketConfigReplicationWithConfiguration(rInt), 665 Check: resource.ComposeTestCheckFunc( 666 testAccCheckAWSS3BucketExistsWithProviders("aws_s3_bucket.bucket", &providers), 667 resource.TestCheckResourceAttr("aws_s3_bucket.bucket", "replication_configuration.#", "1"), 668 resource.TestCheckResourceAttr("aws_s3_bucket.bucket", "replication_configuration.0.rules.#", "1"), 669 resource.TestCheckResourceAttr("aws_s3_bucket.bucket", "replication_configuration.0.rules.2229345141.id", "foobar"), 670 resource.TestCheckResourceAttr("aws_s3_bucket.bucket", "replication_configuration.0.rules.2229345141.prefix", "foo"), 671 resource.TestCheckResourceAttr("aws_s3_bucket.bucket", "replication_configuration.0.rules.2229345141.status", s3.ReplicationRuleStatusEnabled), 672 ), 673 }, 674 }, 675 }) 676 } 677 678 // StorageClass issue: https://github.com/hashicorp/terraform/issues/10909 679 func TestAccAWSS3Bucket_ReplicationWithoutStorageClass(t *testing.T) { 680 rInt := acctest.RandInt() 681 682 // record the initialized providers so that we can use them to check for the instances in each region 683 var providers []*schema.Provider 684 providerFactories := map[string]terraform.ResourceProviderFactory{ 685 "aws": func() (terraform.ResourceProvider, error) { 686 p := Provider() 687 providers = append(providers, p.(*schema.Provider)) 688 return p, nil 689 }, 690 } 691 692 resource.Test(t, resource.TestCase{ 693 PreCheck: func() { testAccPreCheck(t) }, 694 ProviderFactories: providerFactories, 695 CheckDestroy: testAccCheckAWSS3BucketDestroyWithProviders(&providers), 696 Steps: []resource.TestStep{ 697 { 698 Config: testAccAWSS3BucketConfigReplicationWithoutStorageClass(rInt), 699 Check: resource.ComposeTestCheckFunc( 700 testAccCheckAWSS3BucketExistsWithProviders("aws_s3_bucket.bucket", &providers), 701 ), 702 }, 703 }, 704 }) 705 } 706 707 func TestAccAWSS3Bucket_ReplicationExpectVersioningValidationError(t *testing.T) { 708 rInt := acctest.RandInt() 709 710 // record the initialized providers so that we can use them to check for the instances in each region 711 var providers []*schema.Provider 712 providerFactories := map[string]terraform.ResourceProviderFactory{ 713 "aws": func() (terraform.ResourceProvider, error) { 714 p := Provider() 715 providers = append(providers, p.(*schema.Provider)) 716 return p, nil 717 }, 718 } 719 720 resource.Test(t, resource.TestCase{ 721 PreCheck: func() { testAccPreCheck(t) }, 722 ProviderFactories: providerFactories, 723 CheckDestroy: testAccCheckAWSS3BucketDestroyWithProviders(&providers), 724 Steps: []resource.TestStep{ 725 { 726 Config: testAccAWSS3BucketConfigReplicationNoVersioning(rInt), 727 ExpectError: regexp.MustCompile(`versioning must be enabled to allow S3 bucket replication`), 728 }, 729 }, 730 }) 731 } 732 733 func TestAWSS3BucketName(t *testing.T) { 734 validDnsNames := []string{ 735 "foobar", 736 "foo.bar", 737 "foo.bar.baz", 738 "1234", 739 "foo-bar", 740 strings.Repeat("x", 63), 741 } 742 743 for _, v := range validDnsNames { 744 if err := validateS3BucketName(v, "us-west-2"); err != nil { 745 t.Fatalf("%q should be a valid S3 bucket name", v) 746 } 747 } 748 749 invalidDnsNames := []string{ 750 "foo..bar", 751 "Foo.Bar", 752 "192.168.0.1", 753 "127.0.0.1", 754 ".foo", 755 "bar.", 756 "foo_bar", 757 strings.Repeat("x", 64), 758 } 759 760 for _, v := range invalidDnsNames { 761 if err := validateS3BucketName(v, "us-west-2"); err == nil { 762 t.Fatalf("%q should not be a valid S3 bucket name", v) 763 } 764 } 765 766 validEastNames := []string{ 767 "foobar", 768 "foo_bar", 769 "127.0.0.1", 770 "foo..bar", 771 "foo_bar_baz", 772 "foo.bar.baz", 773 "Foo.Bar", 774 strings.Repeat("x", 255), 775 } 776 777 for _, v := range validEastNames { 778 if err := validateS3BucketName(v, "us-east-1"); err != nil { 779 t.Fatalf("%q should be a valid S3 bucket name", v) 780 } 781 } 782 783 invalidEastNames := []string{ 784 "foo;bar", 785 strings.Repeat("x", 256), 786 } 787 788 for _, v := range invalidEastNames { 789 if err := validateS3BucketName(v, "us-east-1"); err == nil { 790 t.Fatalf("%q should not be a valid S3 bucket name", v) 791 } 792 } 793 } 794 795 func testAccCheckAWSS3BucketDestroy(s *terraform.State) error { 796 return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) 797 } 798 799 func testAccCheckAWSS3BucketDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 800 return func(s *terraform.State) error { 801 for _, provider := range *providers { 802 if provider.Meta() == nil { 803 continue 804 } 805 if err := testAccCheckAWSS3BucketDestroyWithProvider(s, provider); err != nil { 806 return err 807 } 808 } 809 return nil 810 } 811 } 812 813 func testAccCheckAWSS3BucketDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 814 conn := provider.Meta().(*AWSClient).s3conn 815 816 for _, rs := range s.RootModule().Resources { 817 if rs.Type != "aws_s3_bucket" { 818 continue 819 } 820 _, err := conn.DeleteBucket(&s3.DeleteBucketInput{ 821 Bucket: aws.String(rs.Primary.ID), 822 }) 823 if err != nil { 824 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoSuchBucket" { 825 return nil 826 } 827 return err 828 } 829 } 830 return nil 831 } 832 833 func testAccCheckAWSS3BucketExists(n string) resource.TestCheckFunc { 834 providers := []*schema.Provider{testAccProvider} 835 return testAccCheckAWSS3BucketExistsWithProviders(n, &providers) 836 } 837 838 func testAccCheckAWSS3BucketExistsWithProviders(n string, providers *[]*schema.Provider) resource.TestCheckFunc { 839 return func(s *terraform.State) error { 840 rs, ok := s.RootModule().Resources[n] 841 if !ok { 842 return fmt.Errorf("Not found: %s", n) 843 } 844 845 if rs.Primary.ID == "" { 846 return fmt.Errorf("No ID is set") 847 } 848 for _, provider := range *providers { 849 // Ignore if Meta is empty, this can happen for validation providers 850 if provider.Meta() == nil { 851 continue 852 } 853 854 conn := provider.Meta().(*AWSClient).s3conn 855 _, err := conn.HeadBucket(&s3.HeadBucketInput{ 856 Bucket: aws.String(rs.Primary.ID), 857 }) 858 859 if err != nil { 860 return fmt.Errorf("S3Bucket error: %v", err) 861 } 862 return nil 863 } 864 865 return fmt.Errorf("Instance not found") 866 } 867 } 868 869 func testAccCheckAWSS3DestroyBucket(n string) resource.TestCheckFunc { 870 return func(s *terraform.State) error { 871 rs, ok := s.RootModule().Resources[n] 872 if !ok { 873 return fmt.Errorf("Not found: %s", n) 874 } 875 876 if rs.Primary.ID == "" { 877 return fmt.Errorf("No S3 Bucket ID is set") 878 } 879 880 conn := testAccProvider.Meta().(*AWSClient).s3conn 881 _, err := conn.DeleteBucket(&s3.DeleteBucketInput{ 882 Bucket: aws.String(rs.Primary.ID), 883 }) 884 885 if err != nil { 886 return fmt.Errorf("Error destroying Bucket (%s) in testAccCheckAWSS3DestroyBucket: %s", rs.Primary.ID, err) 887 } 888 return nil 889 } 890 } 891 892 func testAccCheckAWSS3BucketPolicy(n string, policy string) resource.TestCheckFunc { 893 return func(s *terraform.State) error { 894 rs, _ := s.RootModule().Resources[n] 895 conn := testAccProvider.Meta().(*AWSClient).s3conn 896 897 out, err := conn.GetBucketPolicy(&s3.GetBucketPolicyInput{ 898 Bucket: aws.String(rs.Primary.ID), 899 }) 900 901 if policy == "" { 902 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoSuchBucketPolicy" { 903 // expected 904 return nil 905 } 906 if err == nil { 907 return fmt.Errorf("Expected no policy, got: %#v", *out.Policy) 908 } else { 909 return fmt.Errorf("GetBucketPolicy error: %v, expected %s", err, policy) 910 } 911 } 912 if err != nil { 913 return fmt.Errorf("GetBucketPolicy error: %v, expected %s", err, policy) 914 } 915 916 if v := out.Policy; v == nil { 917 if policy != "" { 918 return fmt.Errorf("bad policy, found nil, expected: %s", policy) 919 } 920 } else { 921 expected := make(map[string]interface{}) 922 if err := json.Unmarshal([]byte(policy), &expected); err != nil { 923 return err 924 } 925 actual := make(map[string]interface{}) 926 if err := json.Unmarshal([]byte(*v), &actual); err != nil { 927 return err 928 } 929 930 if !reflect.DeepEqual(expected, actual) { 931 return fmt.Errorf("bad policy, expected: %#v, got %#v", expected, actual) 932 } 933 } 934 935 return nil 936 } 937 } 938 939 func testAccCheckAWSS3BucketWebsite(n string, indexDoc string, errorDoc string, redirectProtocol string, redirectTo string) resource.TestCheckFunc { 940 return func(s *terraform.State) error { 941 rs, _ := s.RootModule().Resources[n] 942 conn := testAccProvider.Meta().(*AWSClient).s3conn 943 944 out, err := conn.GetBucketWebsite(&s3.GetBucketWebsiteInput{ 945 Bucket: aws.String(rs.Primary.ID), 946 }) 947 948 if err != nil { 949 if indexDoc == "" { 950 // If we want to assert that the website is not there, than 951 // this error is expected 952 return nil 953 } else { 954 return fmt.Errorf("S3BucketWebsite error: %v", err) 955 } 956 } 957 958 if v := out.IndexDocument; v == nil { 959 if indexDoc != "" { 960 return fmt.Errorf("bad index doc, found nil, expected: %s", indexDoc) 961 } 962 } else { 963 if *v.Suffix != indexDoc { 964 return fmt.Errorf("bad index doc, expected: %s, got %#v", indexDoc, out.IndexDocument) 965 } 966 } 967 968 if v := out.ErrorDocument; v == nil { 969 if errorDoc != "" { 970 return fmt.Errorf("bad error doc, found nil, expected: %s", errorDoc) 971 } 972 } else { 973 if *v.Key != errorDoc { 974 return fmt.Errorf("bad error doc, expected: %s, got %#v", errorDoc, out.ErrorDocument) 975 } 976 } 977 978 if v := out.RedirectAllRequestsTo; v == nil { 979 if redirectTo != "" { 980 return fmt.Errorf("bad redirect to, found nil, expected: %s", redirectTo) 981 } 982 } else { 983 if *v.HostName != redirectTo { 984 return fmt.Errorf("bad redirect to, expected: %s, got %#v", redirectTo, out.RedirectAllRequestsTo) 985 } 986 if redirectProtocol != "" && v.Protocol != nil && *v.Protocol != redirectProtocol { 987 return fmt.Errorf("bad redirect protocol to, expected: %s, got %#v", redirectProtocol, out.RedirectAllRequestsTo) 988 } 989 } 990 991 return nil 992 } 993 } 994 995 func testAccCheckAWSS3BucketWebsiteRoutingRules(n string, routingRules []*s3.RoutingRule) resource.TestCheckFunc { 996 return func(s *terraform.State) error { 997 rs, _ := s.RootModule().Resources[n] 998 conn := testAccProvider.Meta().(*AWSClient).s3conn 999 1000 out, err := conn.GetBucketWebsite(&s3.GetBucketWebsiteInput{ 1001 Bucket: aws.String(rs.Primary.ID), 1002 }) 1003 1004 if err != nil { 1005 if routingRules == nil { 1006 return nil 1007 } 1008 return fmt.Errorf("GetBucketWebsite error: %v", err) 1009 } 1010 1011 if !reflect.DeepEqual(out.RoutingRules, routingRules) { 1012 return fmt.Errorf("bad routing rule, expected: %v, got %v", routingRules, out.RoutingRules) 1013 } 1014 1015 return nil 1016 } 1017 } 1018 1019 func testAccCheckAWSS3BucketVersioning(n string, versioningStatus string) resource.TestCheckFunc { 1020 return func(s *terraform.State) error { 1021 rs, _ := s.RootModule().Resources[n] 1022 conn := testAccProvider.Meta().(*AWSClient).s3conn 1023 1024 out, err := conn.GetBucketVersioning(&s3.GetBucketVersioningInput{ 1025 Bucket: aws.String(rs.Primary.ID), 1026 }) 1027 1028 if err != nil { 1029 return fmt.Errorf("GetBucketVersioning error: %v", err) 1030 } 1031 1032 if v := out.Status; v == nil { 1033 if versioningStatus != "" { 1034 return fmt.Errorf("bad error versioning status, found nil, expected: %s", versioningStatus) 1035 } 1036 } else { 1037 if *v != versioningStatus { 1038 return fmt.Errorf("bad error versioning status, expected: %s, got %s", versioningStatus, *v) 1039 } 1040 } 1041 1042 return nil 1043 } 1044 } 1045 1046 func testAccCheckAWSS3BucketCors(n string, corsRules []*s3.CORSRule) resource.TestCheckFunc { 1047 return func(s *terraform.State) error { 1048 rs, _ := s.RootModule().Resources[n] 1049 conn := testAccProvider.Meta().(*AWSClient).s3conn 1050 1051 out, err := conn.GetBucketCors(&s3.GetBucketCorsInput{ 1052 Bucket: aws.String(rs.Primary.ID), 1053 }) 1054 1055 if err != nil { 1056 return fmt.Errorf("GetBucketCors error: %v", err) 1057 } 1058 1059 if !reflect.DeepEqual(out.CORSRules, corsRules) { 1060 return fmt.Errorf("bad error cors rule, expected: %v, got %v", corsRules, out.CORSRules) 1061 } 1062 1063 return nil 1064 } 1065 } 1066 1067 func testAccCheckAWSS3RequestPayer(n, expectedPayer string) resource.TestCheckFunc { 1068 return func(s *terraform.State) error { 1069 rs, _ := s.RootModule().Resources[n] 1070 conn := testAccProvider.Meta().(*AWSClient).s3conn 1071 1072 out, err := conn.GetBucketRequestPayment(&s3.GetBucketRequestPaymentInput{ 1073 Bucket: aws.String(rs.Primary.ID), 1074 }) 1075 1076 if err != nil { 1077 return fmt.Errorf("GetBucketRequestPayment error: %v", err) 1078 } 1079 1080 if *out.Payer != expectedPayer { 1081 return fmt.Errorf("bad error request payer type, expected: %v, got %v", 1082 expectedPayer, out.Payer) 1083 } 1084 1085 return nil 1086 } 1087 } 1088 1089 func testAccCheckAWSS3BucketLogging(n, b, p string) resource.TestCheckFunc { 1090 return func(s *terraform.State) error { 1091 rs, _ := s.RootModule().Resources[n] 1092 conn := testAccProvider.Meta().(*AWSClient).s3conn 1093 1094 out, err := conn.GetBucketLogging(&s3.GetBucketLoggingInput{ 1095 Bucket: aws.String(rs.Primary.ID), 1096 }) 1097 1098 if err != nil { 1099 return fmt.Errorf("GetBucketLogging error: %v", err) 1100 } 1101 1102 tb, _ := s.RootModule().Resources[b] 1103 1104 if v := out.LoggingEnabled.TargetBucket; v == nil { 1105 if tb.Primary.ID != "" { 1106 return fmt.Errorf("bad target bucket, found nil, expected: %s", tb.Primary.ID) 1107 } 1108 } else { 1109 if *v != tb.Primary.ID { 1110 return fmt.Errorf("bad target bucket, expected: %s, got %s", tb.Primary.ID, *v) 1111 } 1112 } 1113 1114 if v := out.LoggingEnabled.TargetPrefix; v == nil { 1115 if p != "" { 1116 return fmt.Errorf("bad target prefix, found nil, expected: %s", p) 1117 } 1118 } else { 1119 if *v != p { 1120 return fmt.Errorf("bad target prefix, expected: %s, got %s", p, *v) 1121 } 1122 } 1123 1124 return nil 1125 } 1126 } 1127 1128 // These need a bit of randomness as the name can only be used once globally 1129 // within AWS 1130 func testAccBucketName(randInt int) string { 1131 return fmt.Sprintf("tf-test-bucket-%d", randInt) 1132 } 1133 1134 func testAccBucketDomainName(randInt int) string { 1135 return fmt.Sprintf("tf-test-bucket-%d.s3.amazonaws.com", randInt) 1136 } 1137 1138 func testAccWebsiteEndpoint(randInt int) string { 1139 return fmt.Sprintf("tf-test-bucket-%d.s3-website-us-west-2.amazonaws.com", randInt) 1140 } 1141 1142 func testAccAWSS3BucketPolicy(randInt int) string { 1143 return fmt.Sprintf(`{ "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::tf-test-bucket-%d/*" } ] }`, randInt) 1144 } 1145 1146 func testAccAWSS3BucketConfig(randInt int) string { 1147 return fmt.Sprintf(` 1148 resource "aws_s3_bucket" "bucket" { 1149 bucket = "tf-test-bucket-%d" 1150 acl = "public-read" 1151 } 1152 `, randInt) 1153 } 1154 1155 func testAccAWSS3BucketConfigWithRegion(randInt int) string { 1156 return fmt.Sprintf(` 1157 provider "aws" { 1158 alias = "west" 1159 region = "eu-west-1" 1160 } 1161 1162 resource "aws_s3_bucket" "bucket" { 1163 provider = "aws.west" 1164 bucket = "tf-test-bucket-%d" 1165 region = "eu-west-1" 1166 } 1167 `, randInt) 1168 } 1169 1170 func testAccAWSS3BucketWebsiteConfig(randInt int) string { 1171 return fmt.Sprintf(` 1172 resource "aws_s3_bucket" "bucket" { 1173 bucket = "tf-test-bucket-%d" 1174 acl = "public-read" 1175 1176 website { 1177 index_document = "index.html" 1178 } 1179 } 1180 `, randInt) 1181 } 1182 1183 func testAccAWSS3BucketWebsiteConfigWithError(randInt int) string { 1184 return fmt.Sprintf(` 1185 resource "aws_s3_bucket" "bucket" { 1186 bucket = "tf-test-bucket-%d" 1187 acl = "public-read" 1188 1189 website { 1190 index_document = "index.html" 1191 error_document = "error.html" 1192 } 1193 } 1194 `, randInt) 1195 } 1196 1197 func testAccAWSS3BucketWebsiteConfigWithRedirect(randInt int) string { 1198 return fmt.Sprintf(` 1199 resource "aws_s3_bucket" "bucket" { 1200 bucket = "tf-test-bucket-%d" 1201 acl = "public-read" 1202 1203 website { 1204 redirect_all_requests_to = "hashicorp.com" 1205 } 1206 } 1207 `, randInt) 1208 } 1209 1210 func testAccAWSS3BucketWebsiteConfigWithHttpsRedirect(randInt int) string { 1211 return fmt.Sprintf(` 1212 resource "aws_s3_bucket" "bucket" { 1213 bucket = "tf-test-bucket-%d" 1214 acl = "public-read" 1215 1216 website { 1217 redirect_all_requests_to = "https://hashicorp.com" 1218 } 1219 } 1220 `, randInt) 1221 } 1222 1223 func testAccAWSS3BucketWebsiteConfigWithRoutingRules(randInt int) string { 1224 return fmt.Sprintf(` 1225 resource "aws_s3_bucket" "bucket" { 1226 bucket = "tf-test-bucket-%d" 1227 acl = "public-read" 1228 1229 website { 1230 index_document = "index.html" 1231 error_document = "error.html" 1232 routing_rules = <<EOF 1233 [{ 1234 "Condition": { 1235 "KeyPrefixEquals": "docs/" 1236 }, 1237 "Redirect": { 1238 "ReplaceKeyPrefixWith": "documents/" 1239 } 1240 }] 1241 EOF 1242 } 1243 } 1244 `, randInt) 1245 } 1246 1247 func testAccAWSS3BucketConfigWithAcceleration(randInt int) string { 1248 return fmt.Sprintf(` 1249 provider "aws" { 1250 alias = "west" 1251 region = "eu-west-1" 1252 } 1253 1254 resource "aws_s3_bucket" "bucket" { 1255 provider = "aws.west" 1256 bucket = "tf-test-bucket-%d" 1257 region = "eu-west-1" 1258 acl = "public-read" 1259 acceleration_status = "Enabled" 1260 } 1261 `, randInt) 1262 } 1263 1264 func testAccAWSS3BucketConfigWithoutAcceleration(randInt int) string { 1265 return fmt.Sprintf(` 1266 provider "aws" { 1267 alias = "west" 1268 region = "eu-west-1" 1269 } 1270 1271 resource "aws_s3_bucket" "bucket" { 1272 provider = "aws.west" 1273 bucket = "tf-test-bucket-%d" 1274 region = "eu-west-1" 1275 acl = "public-read" 1276 acceleration_status = "Suspended" 1277 } 1278 `, randInt) 1279 } 1280 1281 func testAccAWSS3BucketConfigRequestPayerBucketOwner(randInt int) string { 1282 return fmt.Sprintf(` 1283 resource "aws_s3_bucket" "bucket" { 1284 bucket = "tf-test-bucket-%d" 1285 acl = "public-read" 1286 request_payer = "BucketOwner" 1287 } 1288 `, randInt) 1289 } 1290 1291 func testAccAWSS3BucketConfigRequestPayerRequester(randInt int) string { 1292 return fmt.Sprintf(` 1293 resource "aws_s3_bucket" "bucket" { 1294 bucket = "tf-test-bucket-%d" 1295 acl = "public-read" 1296 request_payer = "Requester" 1297 } 1298 `, randInt) 1299 } 1300 1301 func testAccAWSS3BucketConfigWithPolicy(randInt int) string { 1302 return fmt.Sprintf(` 1303 resource "aws_s3_bucket" "bucket" { 1304 bucket = "tf-test-bucket-%d" 1305 acl = "public-read" 1306 policy = %s 1307 } 1308 `, randInt, strconv.Quote(testAccAWSS3BucketPolicy(randInt))) 1309 } 1310 1311 func testAccAWSS3BucketDestroyedConfig(randInt int) string { 1312 return fmt.Sprintf(` 1313 resource "aws_s3_bucket" "bucket" { 1314 bucket = "tf-test-bucket-%d" 1315 acl = "public-read" 1316 } 1317 `, randInt) 1318 } 1319 1320 func testAccAWSS3BucketConfigWithEmptyPolicy(randInt int) string { 1321 return fmt.Sprintf(` 1322 resource "aws_s3_bucket" "bucket" { 1323 bucket = "tf-test-bucket-%d" 1324 acl = "public-read" 1325 policy = "" 1326 } 1327 `, randInt) 1328 } 1329 1330 func testAccAWSS3BucketConfigWithVersioning(randInt int) string { 1331 return fmt.Sprintf(` 1332 resource "aws_s3_bucket" "bucket" { 1333 bucket = "tf-test-bucket-%d" 1334 acl = "public-read" 1335 versioning { 1336 enabled = true 1337 } 1338 } 1339 `, randInt) 1340 } 1341 1342 func testAccAWSS3BucketConfigWithDisableVersioning(randInt int) string { 1343 return fmt.Sprintf(` 1344 resource "aws_s3_bucket" "bucket" { 1345 bucket = "tf-test-bucket-%d" 1346 acl = "public-read" 1347 versioning { 1348 enabled = false 1349 } 1350 } 1351 `, randInt) 1352 } 1353 1354 func testAccAWSS3BucketConfigWithCORS(randInt int) string { 1355 return fmt.Sprintf(` 1356 resource "aws_s3_bucket" "bucket" { 1357 bucket = "tf-test-bucket-%d" 1358 acl = "public-read" 1359 cors_rule { 1360 allowed_headers = ["*"] 1361 allowed_methods = ["PUT","POST"] 1362 allowed_origins = ["https://www.example.com"] 1363 expose_headers = ["x-amz-server-side-encryption","ETag"] 1364 max_age_seconds = 3000 1365 } 1366 } 1367 `, randInt) 1368 } 1369 1370 var testAccAWSS3BucketConfigWithAcl = ` 1371 resource "aws_s3_bucket" "bucket" { 1372 bucket = "tf-test-bucket-%d" 1373 acl = "public-read" 1374 } 1375 ` 1376 1377 var testAccAWSS3BucketConfigWithAclUpdate = ` 1378 resource "aws_s3_bucket" "bucket" { 1379 bucket = "tf-test-bucket-%d" 1380 acl = "private" 1381 } 1382 ` 1383 1384 func testAccAWSS3BucketConfigWithLogging(randInt int) string { 1385 return fmt.Sprintf(` 1386 resource "aws_s3_bucket" "log_bucket" { 1387 bucket = "tf-test-log-bucket-%d" 1388 acl = "log-delivery-write" 1389 } 1390 resource "aws_s3_bucket" "bucket" { 1391 bucket = "tf-test-bucket-%d" 1392 acl = "private" 1393 logging { 1394 target_bucket = "${aws_s3_bucket.log_bucket.id}" 1395 target_prefix = "log/" 1396 } 1397 } 1398 `, randInt, randInt) 1399 } 1400 1401 func testAccAWSS3BucketConfigWithLifecycle(randInt int) string { 1402 return fmt.Sprintf(` 1403 resource "aws_s3_bucket" "bucket" { 1404 bucket = "tf-test-bucket-%d" 1405 acl = "private" 1406 lifecycle_rule { 1407 id = "id1" 1408 prefix = "path1/" 1409 enabled = true 1410 1411 expiration { 1412 days = 365 1413 } 1414 1415 transition { 1416 days = 30 1417 storage_class = "STANDARD_IA" 1418 } 1419 transition { 1420 days = 60 1421 storage_class = "GLACIER" 1422 } 1423 } 1424 lifecycle_rule { 1425 id = "id2" 1426 prefix = "path2/" 1427 enabled = true 1428 1429 expiration { 1430 date = "2016-01-12" 1431 } 1432 } 1433 } 1434 `, randInt) 1435 } 1436 1437 func testAccAWSS3BucketConfigWithVersioningLifecycle(randInt int) string { 1438 return fmt.Sprintf(` 1439 resource "aws_s3_bucket" "bucket" { 1440 bucket = "tf-test-bucket-%d" 1441 acl = "private" 1442 versioning { 1443 enabled = false 1444 } 1445 lifecycle_rule { 1446 id = "id1" 1447 prefix = "path1/" 1448 enabled = true 1449 1450 noncurrent_version_expiration { 1451 days = 365 1452 } 1453 noncurrent_version_transition { 1454 days = 30 1455 storage_class = "STANDARD_IA" 1456 } 1457 noncurrent_version_transition { 1458 days = 60 1459 storage_class = "GLACIER" 1460 } 1461 } 1462 lifecycle_rule { 1463 id = "id2" 1464 prefix = "path2/" 1465 enabled = false 1466 1467 noncurrent_version_expiration { 1468 days = 365 1469 } 1470 } 1471 } 1472 `, randInt) 1473 } 1474 1475 const testAccAWSS3BucketConfigReplicationBasic = ` 1476 provider "aws" { 1477 alias = "euwest" 1478 region = "eu-west-1" 1479 } 1480 1481 provider "aws" { 1482 alias = "uswest2" 1483 region = "us-west-2" 1484 } 1485 1486 resource "aws_iam_role" "role" { 1487 name = "tf-iam-role-replication-%d" 1488 assume_role_policy = <<POLICY 1489 { 1490 "Version": "2012-10-17", 1491 "Statement": [ 1492 { 1493 "Action": "sts:AssumeRole", 1494 "Principal": { 1495 "Service": "s3.amazonaws.com" 1496 }, 1497 "Effect": "Allow", 1498 "Sid": "" 1499 } 1500 ] 1501 } 1502 POLICY 1503 } 1504 ` 1505 1506 func testAccAWSS3BucketConfigReplication(randInt int) string { 1507 return fmt.Sprintf(testAccAWSS3BucketConfigReplicationBasic+` 1508 resource "aws_s3_bucket" "bucket" { 1509 provider = "aws.uswest2" 1510 bucket = "tf-test-bucket-%d" 1511 acl = "private" 1512 1513 versioning { 1514 enabled = true 1515 } 1516 } 1517 1518 resource "aws_s3_bucket" "destination" { 1519 provider = "aws.euwest" 1520 bucket = "tf-test-bucket-destination-%d" 1521 region = "eu-west-1" 1522 1523 versioning { 1524 enabled = true 1525 } 1526 } 1527 `, randInt, randInt, randInt) 1528 } 1529 1530 func testAccAWSS3BucketConfigReplicationWithConfiguration(randInt int) string { 1531 return fmt.Sprintf(testAccAWSS3BucketConfigReplicationBasic+` 1532 resource "aws_s3_bucket" "bucket" { 1533 provider = "aws.uswest2" 1534 bucket = "tf-test-bucket-%d" 1535 acl = "private" 1536 1537 versioning { 1538 enabled = true 1539 } 1540 1541 replication_configuration { 1542 role = "${aws_iam_role.role.arn}" 1543 rules { 1544 id = "foobar" 1545 prefix = "foo" 1546 status = "Enabled" 1547 1548 destination { 1549 bucket = "${aws_s3_bucket.destination.arn}" 1550 storage_class = "STANDARD" 1551 } 1552 } 1553 } 1554 } 1555 1556 resource "aws_s3_bucket" "destination" { 1557 provider = "aws.euwest" 1558 bucket = "tf-test-bucket-destination-%d" 1559 region = "eu-west-1" 1560 1561 versioning { 1562 enabled = true 1563 } 1564 } 1565 `, randInt, randInt, randInt) 1566 } 1567 1568 func testAccAWSS3BucketConfigReplicationWithoutStorageClass(randInt int) string { 1569 return fmt.Sprintf(testAccAWSS3BucketConfigReplicationBasic+` 1570 resource "aws_s3_bucket" "bucket" { 1571 provider = "aws.uswest2" 1572 bucket = "tf-test-bucket-%d" 1573 acl = "private" 1574 1575 versioning { 1576 enabled = true 1577 } 1578 1579 replication_configuration { 1580 role = "${aws_iam_role.role.arn}" 1581 rules { 1582 id = "foobar" 1583 prefix = "foo" 1584 status = "Enabled" 1585 1586 destination { 1587 bucket = "${aws_s3_bucket.destination.arn}" 1588 } 1589 } 1590 } 1591 } 1592 1593 resource "aws_s3_bucket" "destination" { 1594 provider = "aws.euwest" 1595 bucket = "tf-test-bucket-destination-%d" 1596 region = "eu-west-1" 1597 1598 versioning { 1599 enabled = true 1600 } 1601 } 1602 `, randInt, randInt, randInt) 1603 } 1604 1605 func testAccAWSS3BucketConfigReplicationNoVersioning(randInt int) string { 1606 return fmt.Sprintf(testAccAWSS3BucketConfigReplicationBasic+` 1607 resource "aws_s3_bucket" "bucket" { 1608 provider = "aws.uswest2" 1609 bucket = "tf-test-bucket-%d" 1610 acl = "private" 1611 1612 replication_configuration { 1613 role = "${aws_iam_role.role.arn}" 1614 rules { 1615 id = "foobar" 1616 prefix = "foo" 1617 status = "Enabled" 1618 1619 destination { 1620 bucket = "${aws_s3_bucket.destination.arn}" 1621 storage_class = "STANDARD" 1622 } 1623 } 1624 } 1625 } 1626 1627 resource "aws_s3_bucket" "destination" { 1628 provider = "aws.euwest" 1629 bucket = "tf-test-bucket-destination-%d" 1630 region = "eu-west-1" 1631 1632 versioning { 1633 enabled = true 1634 } 1635 } 1636 `, randInt, randInt, randInt) 1637 } 1638 1639 const testAccAWSS3BucketConfig_namePrefix = ` 1640 resource "aws_s3_bucket" "test" { 1641 bucket_prefix = "tf-test-" 1642 } 1643 ` 1644 1645 const testAccAWSS3BucketConfig_generatedName = ` 1646 resource "aws_s3_bucket" "test" { 1647 bucket_prefix = "tf-test-" 1648 } 1649 `