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