From a0b3b5a766e2404906be5477117fabba2b72ef76 Mon Sep 17 00:00:00 2001 From: Justin Campbell Date: Wed, 29 Apr 2015 15:47:21 -0400 Subject: [PATCH] providers/aws: Add website_endpoint to S3 output --- .../providers/aws/resource_aws_s3_bucket.go | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/builtin/providers/aws/resource_aws_s3_bucket.go b/builtin/providers/aws/resource_aws_s3_bucket.go index 9eb8c9adb0a5..2ab8fb81a33b 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket.go +++ b/builtin/providers/aws/resource_aws_s3_bucket.go @@ -35,19 +35,22 @@ func resourceAwsS3Bucket() *schema.Resource { Type: schema.TypeBool, Default: false, Optional: true, - ForceNew: false, }, "index_document": &schema.Schema{ Type: schema.TypeString, Optional: true, - ForceNew: false, }, "error_document": &schema.Schema{ Type: schema.TypeString, Optional: true, - ForceNew: false, + }, + + "website_endpoint": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, }, "tags": tagsSchema(), @@ -118,6 +121,15 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { } } + // Add website_endpoint as an output + endpoint, err := websiteEndpoint(s3conn, d) + if err != nil { + return err + } + if err := d.Set("website_endpoint", endpoint); err != nil { + return err + } + tagSet, err := getTagSetS3(s3conn, d.Id()) if err != nil { return err @@ -184,3 +196,31 @@ func updateWebsite(s3conn *s3.S3, d *schema.ResourceData) error { return nil } + +func websiteEndpoint(s3conn *s3.S3, d *schema.ResourceData) (endpoint string, err error) { + // If the bucket doess't have a website configuration, return an empty endpoint + if !d.Get("website").(bool) { + return + } + + bucket := d.Get("bucket").(string) + + // Lookup the region for this bucket + location, err := s3conn.GetBucketLocation(&s3.GetBucketLocationInput{Bucket: aws.String(bucket)}) + if err != nil { + return + } + var region string + if location.LocationConstraint != nil { + region = *location.LocationConstraint + } + + // Default to us-east-1 if the bucket doesn't have a region + if region == "" { + region = "us-east-1" + } + + endpoint = fmt.Sprintf("%s.s3-website-%s.amazonaws.com", bucket, region) + + return +}