providers/aws: Add region to S3 attrs

This commit is contained in:
Justin Campbell 2015-05-07 12:09:19 -04:00
parent 839688d477
commit 64d2b495c3
3 changed files with 16 additions and 1 deletions

View File

@ -55,6 +55,12 @@ func resourceAwsS3Bucket() *schema.Resource {
Computed: true,
},
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"website_endpoint": &schema.Schema{
Type: schema.TypeString,
Optional: true,
@ -149,7 +155,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
return err
}
// Fetch the region
// Add the region as an attribute
location, err := s3conn.GetBucketLocation(
&s3.GetBucketLocationInput{
Bucket: aws.String(d.Id()),
@ -162,6 +168,12 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
if location.LocationConstraint != nil {
region = *location.LocationConstraint
}
if region == "" {
region = "us-east-1"
}
if err := d.Set("region", region); err != nil {
return err
}
// Add the hosted zone ID for this bucket's region as an attribute
hostedZoneID := s3.HostedZoneIDForRegion(region)

View File

@ -25,6 +25,8 @@ func TestAccAWSS3Bucket_basic(t *testing.T) {
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", "region", "us-west-2"),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "website_endpoint", ""),
),

View File

@ -60,4 +60,5 @@ 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.
* `region` - The AWS region this bucket resides in.
* `website_endpoint` - The website endpoint, if the bucket is configured with a website. If not, this will be an empty string.