From 839688d477b4bf5d0dc9c6d5d023ef5500505c85 Mon Sep 17 00:00:00 2001 From: Justin Campbell Date: Thu, 7 May 2015 12:00:39 -0400 Subject: [PATCH] providers/aws: Add hosted_zone_id to S3 attrs --- .../providers/aws/resource_aws_s3_bucket.go | 28 ++++++++++++++++++- .../aws/resource_aws_s3_bucket_test.go | 2 ++ .../providers/aws/r/s3_bucket.html.markdown | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_s3_bucket.go b/builtin/providers/aws/resource_aws_s3_bucket.go index 36d843fdc..21672198f 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket.go +++ b/builtin/providers/aws/resource_aws_s3_bucket.go @@ -49,6 +49,12 @@ func resourceAwsS3Bucket() *schema.Resource { }, }, + "hosted_zone_id": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "website_endpoint": &schema.Schema{ Type: schema.TypeString, Optional: true, @@ -143,7 +149,27 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { return err } - // Add website_endpoint as an output + // Fetch the region + location, err := s3conn.GetBucketLocation( + &s3.GetBucketLocationInput{ + Bucket: aws.String(d.Id()), + }, + ) + if err != nil { + return err + } + var region string + if location.LocationConstraint != nil { + region = *location.LocationConstraint + } + + // Add the hosted zone ID for this bucket's region as an attribute + hostedZoneID := s3.HostedZoneIDForRegion(region) + if err := d.Set("hosted_zone_id", hostedZoneID); err != nil { + return err + } + + // Add website_endpoint as an attribute endpoint, err := websiteEndpoint(s3conn, d) if err != nil { return err diff --git a/builtin/providers/aws/resource_aws_s3_bucket_test.go b/builtin/providers/aws/resource_aws_s3_bucket_test.go index 965216188..ba0974f68 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket_test.go +++ b/builtin/providers/aws/resource_aws_s3_bucket_test.go @@ -23,6 +23,8 @@ func TestAccAWSS3Bucket_basic(t *testing.T) { Config: testAccAWSS3BucketConfig, Check: resource.ComposeTestCheckFunc( testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), + resource.TestCheckResourceAttr( + "aws_s3_bucket.bucket", "hosted_zone_id", s3.HostedZoneIDForRegion("us-west-2")), resource.TestCheckResourceAttr( "aws_s3_bucket.bucket", "website_endpoint", ""), ), diff --git a/website/source/docs/providers/aws/r/s3_bucket.html.markdown b/website/source/docs/providers/aws/r/s3_bucket.html.markdown index 821918804..07bc54644 100644 --- a/website/source/docs/providers/aws/r/s3_bucket.html.markdown +++ b/website/source/docs/providers/aws/r/s3_bucket.html.markdown @@ -59,4 +59,5 @@ The website object supports the following: The following attributes are exported: * `id` - The name of the bucket. +* `hosted_zone_id` - The [Route 53 Hosted Zone ID](http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_website_region_endpoints) for this bucket's region. * `website_endpoint` - The website endpoint, if the bucket is configured with a website. If not, this will be an empty string.