From d7c9d8702cf307137cda7b0838acfea2d0b94401 Mon Sep 17 00:00:00 2001 From: Justin Campbell Date: Wed, 6 May 2015 11:12:16 -0400 Subject: [PATCH] providers/aws: Extract website endpoint logic --- builtin/providers/aws/resource_aws_s3_bucket.go | 8 +++++--- .../providers/aws/website_endpoint_url_test.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 builtin/providers/aws/website_endpoint_url_test.go diff --git a/builtin/providers/aws/resource_aws_s3_bucket.go b/builtin/providers/aws/resource_aws_s3_bucket.go index ddeab6a19..36d843fdc 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket.go +++ b/builtin/providers/aws/resource_aws_s3_bucket.go @@ -260,13 +260,15 @@ func websiteEndpoint(s3conn *s3.S3, d *schema.ResourceData) (string, error) { region = *location.LocationConstraint } + return WebsiteEndpointUrl(bucket, region), nil +} + +func WebsiteEndpointUrl(bucket string, region string) string { // Default to us-east-1 if the bucket doesn't have a region: // http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html if region == "" { region = "us-east-1" } - endpoint := fmt.Sprintf("%s.s3-website-%s.amazonaws.com", bucket, region) - - return endpoint, nil + return fmt.Sprintf("%s.s3-website-%s.amazonaws.com", bucket, region) } diff --git a/builtin/providers/aws/website_endpoint_url_test.go b/builtin/providers/aws/website_endpoint_url_test.go new file mode 100644 index 000000000..38b11d329 --- /dev/null +++ b/builtin/providers/aws/website_endpoint_url_test.go @@ -0,0 +1,17 @@ +package aws + +import "testing" + +func TestWebsiteEndpointUrl_withoutRegion(t *testing.T) { + u := WebsiteEndpointUrl("buck.et", "") + if u != "buck.et.s3-website-us-east-1.amazonaws.com" { + t.Fatalf("bad: %s", u) + } +} + +func TestWebsiteEndpointUrl_withRegion(t *testing.T) { + u := WebsiteEndpointUrl("buck.et", "us-west-1") + if u != "buck.et.s3-website-us-west-1.amazonaws.com" { + t.Fatalf("bad: %s", u) + } +}