Merge pull request #4098 from csabapalfi/f-aws-s3-redirect-protocol

Enable specifying aws s3 redirect protocol
This commit is contained in:
Clint 2016-01-22 15:51:40 -06:00
commit 4708e66328
3 changed files with 46 additions and 9 deletions

View File

@ -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{

View File

@ -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"

View File

@ -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: