providers/aws: Extract website endpoint logic

This commit is contained in:
Justin Campbell 2015-05-06 11:12:16 -04:00
parent f1ae920aa9
commit d7c9d8702c
2 changed files with 22 additions and 3 deletions

View File

@ -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)
}

View File

@ -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)
}
}