Skip to content

Commit

Permalink
providers/aws: Add website_endpoint to S3 output
Browse files Browse the repository at this point in the history
  • Loading branch information
justincampbell committed Apr 30, 2015
1 parent 15f6ed0 commit e3dee1b
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions builtin/providers/aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func resourceAwsS3Bucket() *schema.Resource {
ForceNew: false,
},

"website_endpoint": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -118,6 +124,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
Expand Down Expand Up @@ -184,3 +199,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
}

0 comments on commit e3dee1b

Please sign in to comment.