From 8f6404a0e13549d3c5f35348809e073fb2a7b822 Mon Sep 17 00:00:00 2001 From: Csaba Palfi Date: Sat, 28 Nov 2015 21:32:19 +0000 Subject: [PATCH] provider/aws enable specifying aws s3 redirect protocol Signed-off-by: Csaba Palfi --- .../providers/aws/resource_aws_s3_bucket.go | 17 +++++++-- .../aws/resource_aws_s3_bucket_test.go | 36 +++++++++++++++---- .../providers/aws/r/s3_bucket.html.markdown | 2 +- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/builtin/providers/aws/resource_aws_s3_bucket.go b/builtin/providers/aws/resource_aws_s3_bucket.go index 4b8cb69110..9c357976e6 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket.go +++ b/builtin/providers/aws/resource_aws_s3_bucket.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "log" + "net/url" "time" "github.com/hashicorp/terraform/helper/resource" @@ -340,7 +341,14 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { } if v := ws.RedirectAllRequestsTo; v != nil { - w["redirect_all_requests_to"] = *v.HostName + if v.Protocol == nil { + w["redirect_all_requests_to"] = *v.HostName + } else { + w["redirect_all_requests_to"] = (&url.URL{ + Host: *v.HostName, + Scheme: *v.Protocol, + }).String() + } } websites = append(websites, w) @@ -652,7 +660,12 @@ func resourceAwsS3BucketWebsitePut(s3conn *s3.S3, d *schema.ResourceData, websit } if redirectAllRequestsTo != "" { - websiteConfiguration.RedirectAllRequestsTo = &s3.RedirectAllRequestsTo{HostName: aws.String(redirectAllRequestsTo)} + redirect, err := url.Parse(redirectAllRequestsTo) + if err == nil && redirect.Scheme != "" { + websiteConfiguration.RedirectAllRequestsTo = &s3.RedirectAllRequestsTo{HostName: aws.String(redirect.Host), Protocol: aws.String(redirect.Scheme)} + } else { + websiteConfiguration.RedirectAllRequestsTo = &s3.RedirectAllRequestsTo{HostName: aws.String(redirectAllRequestsTo)} + } } putInput := &s3.PutBucketWebsiteInput{ diff --git a/builtin/providers/aws/resource_aws_s3_bucket_test.go b/builtin/providers/aws/resource_aws_s3_bucket_test.go index 2f3ca1dd8d..40d53586c4 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket_test.go +++ b/builtin/providers/aws/resource_aws_s3_bucket_test.go @@ -114,7 +114,7 @@ func TestAccAWSS3Bucket_Website_Simple(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), testAccCheckAWSS3BucketWebsite( - "aws_s3_bucket.bucket", "index.html", "", ""), + "aws_s3_bucket.bucket", "index.html", "", "", ""), resource.TestCheckResourceAttr( "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint), ), @@ -124,7 +124,7 @@ func TestAccAWSS3Bucket_Website_Simple(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), testAccCheckAWSS3BucketWebsite( - "aws_s3_bucket.bucket", "index.html", "error.html", ""), + "aws_s3_bucket.bucket", "index.html", "error.html", "", ""), resource.TestCheckResourceAttr( "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint), ), @@ -134,7 +134,7 @@ func TestAccAWSS3Bucket_Website_Simple(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), testAccCheckAWSS3BucketWebsite( - "aws_s3_bucket.bucket", "", "", ""), + "aws_s3_bucket.bucket", "", "", "", ""), resource.TestCheckResourceAttr( "aws_s3_bucket.bucket", "website_endpoint", ""), ), @@ -154,7 +154,17 @@ func TestAccAWSS3Bucket_WebsiteRedirect(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), testAccCheckAWSS3BucketWebsite( - "aws_s3_bucket.bucket", "", "", "hashicorp.com"), + "aws_s3_bucket.bucket", "", "", "", "hashicorp.com"), + resource.TestCheckResourceAttr( + "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint), + ), + }, + resource.TestStep{ + Config: testAccAWSS3BucketWebsiteConfigWithHttpsRedirect, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), + testAccCheckAWSS3BucketWebsite( + "aws_s3_bucket.bucket", "", "", "https", "hashicorp.com"), resource.TestCheckResourceAttr( "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint), ), @@ -164,7 +174,7 @@ func TestAccAWSS3Bucket_WebsiteRedirect(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), testAccCheckAWSS3BucketWebsite( - "aws_s3_bucket.bucket", "", "", ""), + "aws_s3_bucket.bucket", "", "", "", ""), resource.TestCheckResourceAttr( "aws_s3_bucket.bucket", "website_endpoint", ""), ), @@ -380,7 +390,7 @@ func testAccCheckAWSS3BucketPolicy(n string, policy string) resource.TestCheckFu return nil } } -func testAccCheckAWSS3BucketWebsite(n string, indexDoc string, errorDoc string, redirectTo string) resource.TestCheckFunc { +func testAccCheckAWSS3BucketWebsite(n string, indexDoc string, errorDoc string, redirectProtocol string, redirectTo string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, _ := s.RootModule().Resources[n] conn := testAccProvider.Meta().(*AWSClient).s3conn @@ -427,6 +437,9 @@ func testAccCheckAWSS3BucketWebsite(n string, indexDoc string, errorDoc string, if *v.HostName != redirectTo { return fmt.Errorf("bad redirect to, expected: %s, got %#v", redirectTo, out.RedirectAllRequestsTo) } + if redirectProtocol != "" && *v.Protocol != redirectProtocol { + return fmt.Errorf("bad redirect protocol to, expected: %s, got %#v", redirectProtocol, out.RedirectAllRequestsTo) + } } return nil @@ -566,6 +579,17 @@ resource "aws_s3_bucket" "bucket" { } `, randInt) +var testAccAWSS3BucketWebsiteConfigWithHttpsRedirect = fmt.Sprintf(` +resource "aws_s3_bucket" "bucket" { + bucket = "tf-test-bucket-%d" + acl = "public-read" + + website { + redirect_all_requests_to = "https://hashicorp.com" + } +} +`, randInt) + var testAccAWSS3BucketConfigWithPolicy = fmt.Sprintf(` resource "aws_s3_bucket" "bucket" { bucket = "tf-test-bucket-%d" diff --git a/website/source/docs/providers/aws/r/s3_bucket.html.markdown b/website/source/docs/providers/aws/r/s3_bucket.html.markdown index 75216ff99d..1e923c462f 100644 --- a/website/source/docs/providers/aws/r/s3_bucket.html.markdown +++ b/website/source/docs/providers/aws/r/s3_bucket.html.markdown @@ -106,7 +106,7 @@ The `website` object supports the following: * `index_document` - (Required, unless using `redirect_all_requests_to`) Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders. * `error_document` - (Optional) An absolute path to the document to return in case of a 4XX error. -* `redirect_all_requests_to` - (Optional) A hostname to redirect all website requests for this bucket to. +* `redirect_all_requests_to` - (Optional) A hostname to redirect all website requests for this bucket to. Hostname can optionally be prefixed with a protocol (`http://` or `https://`) to use when redirecting requests. The default is the protocol that is used in the original request. The `CORS` object supports the following: