2014-07-21 20:34:47 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-05-16 11:49:04 +02:00
|
|
|
"encoding/json"
|
2014-07-21 20:34:47 +02:00
|
|
|
"fmt"
|
2014-11-24 21:22:18 +01:00
|
|
|
"math/rand"
|
2015-05-16 11:49:04 +02:00
|
|
|
"reflect"
|
2015-10-29 14:33:35 +01:00
|
|
|
"regexp"
|
2015-05-03 02:23:45 +02:00
|
|
|
"strconv"
|
2014-07-21 20:34:47 +02:00
|
|
|
"testing"
|
2014-11-24 21:22:18 +01:00
|
|
|
"time"
|
2014-07-21 20:34:47 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-02-19 22:38:56 +01:00
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-12-16 18:07:15 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
2014-07-21 20:34:47 +02:00
|
|
|
)
|
|
|
|
|
2015-05-07 17:13:08 +02:00
|
|
|
func TestAccAWSS3Bucket_basic(t *testing.T) {
|
2015-10-29 14:33:35 +01:00
|
|
|
|
|
|
|
arnRegexp := regexp.MustCompile(
|
|
|
|
"^arn:aws:s3:::")
|
|
|
|
|
2014-07-21 20:34:47 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSS3BucketDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-04-26 01:36:00 +02:00
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
2015-05-07 18:00:39 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-05-08 16:29:26 +02:00
|
|
|
"aws_s3_bucket.bucket", "hosted_zone_id", HostedZoneIDForRegion("us-west-2")),
|
2015-05-07 18:09:19 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_s3_bucket.bucket", "region", "us-west-2"),
|
2015-05-06 14:55:10 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_s3_bucket.bucket", "website_endpoint", ""),
|
2015-10-29 14:33:35 +01:00
|
|
|
resource.TestMatchResourceAttr(
|
|
|
|
"aws_s3_bucket.bucket", "arn", arnRegexp),
|
2015-04-26 01:36:00 +02:00
|
|
|
),
|
|
|
|
},
|
2015-05-02 03:04:22 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-03 02:23:45 +02:00
|
|
|
func TestAccAWSS3Bucket_Policy(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSS3BucketDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketConfigWithPolicy,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3BucketPolicy(
|
|
|
|
"aws_s3_bucket.bucket", testAccAWSS3BucketPolicy),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3BucketPolicy(
|
|
|
|
"aws_s3_bucket.bucket", ""),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-26 22:45:48 +01:00
|
|
|
func TestAccAWSS3Bucket_UpdateAcl(t *testing.T) {
|
|
|
|
|
|
|
|
ri := genRandInt()
|
|
|
|
preConfig := fmt.Sprintf(testAccAWSS3BucketConfigWithAcl, ri)
|
|
|
|
postConfig := fmt.Sprintf(testAccAWSS3BucketConfigWithAclUpdate, ri)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSS3BucketDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: preConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_s3_bucket.bucket", "acl", "public-read"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: postConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_s3_bucket.bucket", "acl", "private"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-14 20:55:19 +02:00
|
|
|
func TestAccAWSS3Bucket_Website_Simple(t *testing.T) {
|
2015-05-02 03:04:22 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSS3BucketDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2015-04-26 01:36:00 +02:00
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketWebsiteConfig,
|
2015-05-06 14:40:32 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3BucketWebsite(
|
2015-05-11 16:53:33 +02:00
|
|
|
"aws_s3_bucket.bucket", "index.html", "", ""),
|
2015-05-06 14:55:10 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint),
|
2015-05-06 14:40:32 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketWebsiteConfigWithError,
|
2015-04-26 01:36:00 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-05-06 14:15:07 +02:00
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3BucketWebsite(
|
2015-05-11 16:53:33 +02:00
|
|
|
"aws_s3_bucket.bucket", "index.html", "error.html", ""),
|
2015-05-06 14:55:10 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint),
|
2015-05-06 14:15:07 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
2015-05-02 03:04:22 +02:00
|
|
|
testAccCheckAWSS3BucketWebsite(
|
2015-05-11 16:53:33 +02:00
|
|
|
"aws_s3_bucket.bucket", "", "", ""),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_s3_bucket.bucket", "website_endpoint", ""),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSS3Bucket_WebsiteRedirect(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSS3BucketDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketWebsiteConfigWithRedirect,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3BucketWebsite(
|
|
|
|
"aws_s3_bucket.bucket", "", "", "hashicorp.com"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3BucketWebsite(
|
|
|
|
"aws_s3_bucket.bucket", "", "", ""),
|
2015-05-06 14:55:10 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_s3_bucket.bucket", "website_endpoint", ""),
|
2014-07-21 20:34:47 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-03 23:30:54 +02:00
|
|
|
// Test TestAccAWSS3Bucket_shouldFailNotFound is designed to fail with a "plan
|
|
|
|
// not empty" error in Terraform, to check against regresssions.
|
|
|
|
// See https://github.com/hashicorp/terraform/pull/2925
|
|
|
|
func TestAccAWSS3Bucket_shouldFailNotFound(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSS3BucketDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketDestroyedConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3DestroyBucket("aws_s3_bucket.bucket"),
|
|
|
|
),
|
2016-01-06 00:37:54 +01:00
|
|
|
ExpectNonEmptyPlan: true,
|
2015-08-03 23:30:54 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-05 04:09:31 +02:00
|
|
|
func TestAccAWSS3Bucket_Versioning(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSS3BucketDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3BucketVersioning(
|
|
|
|
"aws_s3_bucket.bucket", ""),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketConfigWithVersioning,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3BucketVersioning(
|
|
|
|
"aws_s3_bucket.bucket", s3.BucketVersioningStatusEnabled),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketConfigWithDisableVersioning,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3BucketVersioning(
|
|
|
|
"aws_s3_bucket.bucket", s3.BucketVersioningStatusSuspended),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-01 18:49:32 +02:00
|
|
|
func TestAccAWSS3Bucket_Cors(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSS3BucketDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketConfigWithCORS,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3BucketCors(
|
|
|
|
"aws_s3_bucket.bucket",
|
|
|
|
[]*s3.CORSRule{
|
|
|
|
&s3.CORSRule{
|
|
|
|
AllowedHeaders: []*string{aws.String("*")},
|
|
|
|
AllowedMethods: []*string{aws.String("PUT"), aws.String("POST")},
|
|
|
|
AllowedOrigins: []*string{aws.String("https://www.example.com")},
|
|
|
|
ExposeHeaders: []*string{aws.String("x-amz-server-side-encryption"), aws.String("ETag")},
|
|
|
|
MaxAgeSeconds: aws.Int64(3000),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-02 04:45:40 +01:00
|
|
|
func TestAccAWSS3Bucket_Logging(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSS3BucketDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketConfigWithLogging,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
testAccCheckAWSS3BucketLogging(
|
|
|
|
"aws_s3_bucket.bucket", "aws_s3_bucket.log_bucket", "log/"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-21 20:34:47 +02:00
|
|
|
func testAccCheckAWSS3BucketDestroy(s *terraform.State) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).s3conn
|
2014-07-21 20:34:47 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-21 20:34:47 +02:00
|
|
|
if rs.Type != "aws_s3_bucket" {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-14 18:07:05 +02:00
|
|
|
_, err := conn.DeleteBucket(&s3.DeleteBucketInput{
|
2015-02-19 22:38:56 +01:00
|
|
|
Bucket: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-12-16 18:07:15 +01:00
|
|
|
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoSuchBucket" {
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-19 22:38:56 +01:00
|
|
|
return err
|
2014-07-21 20:34:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSS3BucketExists(n string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-21 20:34:47 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-21 20:34:47 +02:00
|
|
|
return fmt.Errorf("No S3 Bucket ID is set")
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).s3conn
|
2015-04-14 18:07:05 +02:00
|
|
|
_, err := conn.HeadBucket(&s3.HeadBucketInput{
|
2015-02-19 22:38:56 +01:00
|
|
|
Bucket: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
2014-07-21 20:34:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("S3Bucket error: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 23:30:54 +02:00
|
|
|
func testAccCheckAWSS3DestroyBucket(n string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No S3 Bucket ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).s3conn
|
|
|
|
_, err := conn.DeleteBucket(&s3.DeleteBucketInput{
|
|
|
|
Bucket: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error destroying Bucket (%s) in testAccCheckAWSS3DestroyBucket: %s", rs.Primary.ID, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 02:23:45 +02:00
|
|
|
func testAccCheckAWSS3BucketPolicy(n string, policy string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, _ := s.RootModule().Resources[n]
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).s3conn
|
|
|
|
|
|
|
|
out, err := conn.GetBucketPolicy(&s3.GetBucketPolicyInput{
|
|
|
|
Bucket: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if policy == "" {
|
|
|
|
// expected
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("GetBucketPolicy error: %v, expected %s", err, policy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := out.Policy; v == nil {
|
|
|
|
if policy != "" {
|
|
|
|
return fmt.Errorf("bad policy, found nil, expected: %s", policy)
|
|
|
|
}
|
|
|
|
} else {
|
2015-05-16 11:49:04 +02:00
|
|
|
expected := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(policy), &expected); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actual := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(*v), &actual); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
return fmt.Errorf("bad policy, expected: %#v, got %#v", expected, actual)
|
2015-05-03 02:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-05-11 16:53:33 +02:00
|
|
|
func testAccCheckAWSS3BucketWebsite(n string, indexDoc string, errorDoc string, redirectTo string) resource.TestCheckFunc {
|
2015-05-02 03:04:22 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, _ := s.RootModule().Resources[n]
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).s3conn
|
|
|
|
|
|
|
|
out, err := conn.GetBucketWebsite(&s3.GetBucketWebsiteInput{
|
|
|
|
Bucket: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2015-05-06 14:15:07 +02:00
|
|
|
if indexDoc == "" {
|
|
|
|
// If we want to assert that the website is not there, than
|
|
|
|
// this error is expected
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("S3BucketWebsite error: %v", err)
|
|
|
|
}
|
2015-05-02 03:04:22 +02:00
|
|
|
}
|
|
|
|
|
2015-05-11 16:53:33 +02:00
|
|
|
if v := out.IndexDocument; v == nil {
|
|
|
|
if indexDoc != "" {
|
|
|
|
return fmt.Errorf("bad index doc, found nil, expected: %s", indexDoc)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if *v.Suffix != indexDoc {
|
|
|
|
return fmt.Errorf("bad index doc, expected: %s, got %#v", indexDoc, out.IndexDocument)
|
2015-05-07 17:03:28 +02:00
|
|
|
}
|
2015-05-02 03:04:22 +02:00
|
|
|
}
|
|
|
|
|
2015-05-06 14:40:32 +02:00
|
|
|
if v := out.ErrorDocument; v == nil {
|
|
|
|
if errorDoc != "" {
|
2015-05-07 17:03:28 +02:00
|
|
|
return fmt.Errorf("bad error doc, found nil, expected: %s", errorDoc)
|
2015-05-06 14:40:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if *v.Key != errorDoc {
|
2015-05-07 17:03:28 +02:00
|
|
|
return fmt.Errorf("bad error doc, expected: %s, got %#v", errorDoc, out.ErrorDocument)
|
2015-05-06 14:40:32 +02:00
|
|
|
}
|
2015-05-02 03:04:22 +02:00
|
|
|
}
|
|
|
|
|
2015-05-11 16:53:33 +02:00
|
|
|
if v := out.RedirectAllRequestsTo; v == nil {
|
|
|
|
if redirectTo != "" {
|
|
|
|
return fmt.Errorf("bad redirect to, found nil, expected: %s", redirectTo)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if *v.HostName != redirectTo {
|
|
|
|
return fmt.Errorf("bad redirect to, expected: %s, got %#v", redirectTo, out.RedirectAllRequestsTo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 03:04:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-05 04:09:31 +02:00
|
|
|
func testAccCheckAWSS3BucketVersioning(n string, versioningStatus string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, _ := s.RootModule().Resources[n]
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).s3conn
|
|
|
|
|
|
|
|
out, err := conn.GetBucketVersioning(&s3.GetBucketVersioningInput{
|
|
|
|
Bucket: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetBucketVersioning error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := out.Status; v == nil {
|
|
|
|
if versioningStatus != "" {
|
|
|
|
return fmt.Errorf("bad error versioning status, found nil, expected: %s", versioningStatus)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if *v != versioningStatus {
|
|
|
|
return fmt.Errorf("bad error versioning status, expected: %s, got %s", versioningStatus, *v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 18:49:32 +02:00
|
|
|
func testAccCheckAWSS3BucketCors(n string, corsRules []*s3.CORSRule) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, _ := s.RootModule().Resources[n]
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).s3conn
|
|
|
|
|
|
|
|
out, err := conn.GetBucketCors(&s3.GetBucketCorsInput{
|
|
|
|
Bucket: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetBucketCors error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(out.CORSRules, corsRules) {
|
|
|
|
return fmt.Errorf("bad error cors rule, expected: %v, got %v", corsRules, out.CORSRules)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-08-05 04:09:31 +02:00
|
|
|
|
2016-01-02 04:45:40 +01:00
|
|
|
func testAccCheckAWSS3BucketLogging(n, b, p string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, _ := s.RootModule().Resources[n]
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).s3conn
|
|
|
|
|
|
|
|
out, err := conn.GetBucketLogging(&s3.GetBucketLoggingInput{
|
|
|
|
Bucket: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetBucketLogging error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tb, _ := s.RootModule().Resources[b]
|
|
|
|
|
|
|
|
if v := out.LoggingEnabled.TargetBucket; v == nil {
|
|
|
|
if tb.Primary.ID != "" {
|
|
|
|
return fmt.Errorf("bad target bucket, found nil, expected: %s", tb.Primary.ID)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if *v != tb.Primary.ID {
|
|
|
|
return fmt.Errorf("bad target bucket, expected: %s, got %s", tb.Primary.ID, *v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v := out.LoggingEnabled.TargetPrefix; v == nil {
|
|
|
|
if p != "" {
|
|
|
|
return fmt.Errorf("bad target prefix, found nil, expected: %s", p)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if *v != p {
|
|
|
|
return fmt.Errorf("bad target prefix, expected: %s, got %s", p, *v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 14:15:07 +02:00
|
|
|
// These need a bit of randomness as the name can only be used once globally
|
2015-04-26 01:36:00 +02:00
|
|
|
// within AWS
|
2015-05-06 14:55:10 +02:00
|
|
|
var randInt = rand.New(rand.NewSource(time.Now().UnixNano())).Int()
|
2015-05-07 17:13:08 +02:00
|
|
|
var testAccWebsiteEndpoint = fmt.Sprintf("tf-test-bucket-%d.s3-website-us-west-2.amazonaws.com", randInt)
|
2015-11-12 16:32:45 +01:00
|
|
|
var testAccAWSS3BucketPolicy = fmt.Sprintf(`{ "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::tf-test-bucket-%d/*" } ] }`, randInt)
|
2015-05-03 02:23:45 +02:00
|
|
|
|
2014-11-24 21:22:18 +01:00
|
|
|
var testAccAWSS3BucketConfig = fmt.Sprintf(`
|
2015-04-26 01:36:00 +02:00
|
|
|
resource "aws_s3_bucket" "bucket" {
|
2014-11-24 21:22:18 +01:00
|
|
|
bucket = "tf-test-bucket-%d"
|
2014-07-21 20:34:47 +02:00
|
|
|
acl = "public-read"
|
|
|
|
}
|
2015-05-06 14:55:10 +02:00
|
|
|
`, randInt)
|
2015-04-26 01:36:00 +02:00
|
|
|
|
|
|
|
var testAccAWSS3BucketWebsiteConfig = fmt.Sprintf(`
|
2015-05-06 14:15:07 +02:00
|
|
|
resource "aws_s3_bucket" "bucket" {
|
|
|
|
bucket = "tf-test-bucket-%d"
|
2015-04-26 01:36:00 +02:00
|
|
|
acl = "public-read"
|
|
|
|
|
2015-05-06 14:40:32 +02:00
|
|
|
website {
|
|
|
|
index_document = "index.html"
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 14:55:10 +02:00
|
|
|
`, randInt)
|
2015-05-06 14:40:32 +02:00
|
|
|
|
|
|
|
var testAccAWSS3BucketWebsiteConfigWithError = fmt.Sprintf(`
|
|
|
|
resource "aws_s3_bucket" "bucket" {
|
|
|
|
bucket = "tf-test-bucket-%d"
|
|
|
|
acl = "public-read"
|
|
|
|
|
2015-05-01 15:19:54 +02:00
|
|
|
website {
|
|
|
|
index_document = "index.html"
|
2015-05-02 03:04:22 +02:00
|
|
|
error_document = "error.html"
|
2015-05-01 15:19:54 +02:00
|
|
|
}
|
2015-04-26 01:36:00 +02:00
|
|
|
}
|
2015-05-06 14:55:10 +02:00
|
|
|
`, randInt)
|
2015-05-11 16:53:33 +02:00
|
|
|
|
|
|
|
var testAccAWSS3BucketWebsiteConfigWithRedirect = fmt.Sprintf(`
|
|
|
|
resource "aws_s3_bucket" "bucket" {
|
|
|
|
bucket = "tf-test-bucket-%d"
|
|
|
|
acl = "public-read"
|
|
|
|
|
|
|
|
website {
|
|
|
|
redirect_all_requests_to = "hashicorp.com"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, randInt)
|
2015-05-03 02:23:45 +02:00
|
|
|
|
|
|
|
var testAccAWSS3BucketConfigWithPolicy = fmt.Sprintf(`
|
|
|
|
resource "aws_s3_bucket" "bucket" {
|
|
|
|
bucket = "tf-test-bucket-%d"
|
|
|
|
acl = "public-read"
|
|
|
|
policy = %s
|
|
|
|
}
|
|
|
|
`, randInt, strconv.Quote(testAccAWSS3BucketPolicy))
|
2015-08-03 23:30:54 +02:00
|
|
|
|
|
|
|
var destroyedName = fmt.Sprintf("tf-test-bucket-%d", randInt)
|
|
|
|
var testAccAWSS3BucketDestroyedConfig = fmt.Sprintf(`
|
|
|
|
resource "aws_s3_bucket" "bucket" {
|
|
|
|
bucket = "%s"
|
|
|
|
acl = "public-read"
|
|
|
|
}
|
|
|
|
`, destroyedName)
|
2015-08-05 04:09:31 +02:00
|
|
|
var testAccAWSS3BucketConfigWithVersioning = fmt.Sprintf(`
|
|
|
|
resource "aws_s3_bucket" "bucket" {
|
|
|
|
bucket = "tf-test-bucket-%d"
|
|
|
|
acl = "public-read"
|
2015-09-05 10:19:52 +02:00
|
|
|
versioning {
|
|
|
|
enabled = true
|
|
|
|
}
|
2015-08-05 04:09:31 +02:00
|
|
|
}
|
|
|
|
`, randInt)
|
|
|
|
|
|
|
|
var testAccAWSS3BucketConfigWithDisableVersioning = fmt.Sprintf(`
|
|
|
|
resource "aws_s3_bucket" "bucket" {
|
|
|
|
bucket = "tf-test-bucket-%d"
|
|
|
|
acl = "public-read"
|
2015-09-05 10:19:52 +02:00
|
|
|
versioning {
|
|
|
|
enabled = false
|
|
|
|
}
|
2015-08-05 04:09:31 +02:00
|
|
|
}
|
|
|
|
`, randInt)
|
2015-10-01 18:49:32 +02:00
|
|
|
|
|
|
|
var testAccAWSS3BucketConfigWithCORS = fmt.Sprintf(`
|
|
|
|
resource "aws_s3_bucket" "bucket" {
|
|
|
|
bucket = "tf-test-bucket-%d"
|
|
|
|
acl = "public-read"
|
|
|
|
cors_rule {
|
|
|
|
allowed_headers = ["*"]
|
|
|
|
allowed_methods = ["PUT","POST"]
|
|
|
|
allowed_origins = ["https://www.example.com"]
|
|
|
|
expose_headers = ["x-amz-server-side-encryption","ETag"]
|
|
|
|
max_age_seconds = 3000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, randInt)
|
2015-11-26 22:45:48 +01:00
|
|
|
|
|
|
|
var testAccAWSS3BucketConfigWithAcl = `
|
|
|
|
resource "aws_s3_bucket" "bucket" {
|
|
|
|
bucket = "tf-test-bucket-%d"
|
|
|
|
acl = "public-read"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var testAccAWSS3BucketConfigWithAclUpdate = `
|
|
|
|
resource "aws_s3_bucket" "bucket" {
|
|
|
|
bucket = "tf-test-bucket-%d"
|
|
|
|
acl = "private"
|
|
|
|
}
|
|
|
|
`
|
2016-01-02 04:45:40 +01:00
|
|
|
|
|
|
|
var testAccAWSS3BucketConfigWithLogging = fmt.Sprintf(`
|
|
|
|
resource "aws_s3_bucket" "log_bucket" {
|
|
|
|
bucket = "tf-test-log-bucket-%d"
|
|
|
|
acl = "log-delivery-write"
|
|
|
|
}
|
|
|
|
resource "aws_s3_bucket" "bucket" {
|
|
|
|
bucket = "tf-test-bucket-%d"
|
|
|
|
acl = "private"
|
|
|
|
logging {
|
|
|
|
target_bucket = "${aws_s3_bucket.log_bucket.id}"
|
|
|
|
target_prefix = "log/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, randInt, randInt)
|