provider/aws: Added a S3 Bucket domain name attribute (#10088)

This commit is contained in:
Gauthier Wallet 2017-01-24 14:54:46 +01:00 committed by Paul Stack
parent 4f7f048f76
commit 92d723609e
5 changed files with 29 additions and 5 deletions

View File

@ -18,11 +18,11 @@ func TestAccAWSS3Bucket_importBasic(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccAWSS3BucketConfig(rInt),
},
resource.TestStep{
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
@ -63,11 +63,11 @@ func TestAccAWSS3Bucket_importWithPolicy(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccAWSS3BucketConfigWithPolicy(rInt),
},
resource.TestStep{
{
ResourceName: "aws_s3_bucket.bucket",
ImportState: true,
ImportStateCheck: checkFn,

View File

@ -36,6 +36,11 @@ func resourceAwsS3Bucket() *schema.Resource {
ForceNew: true,
},
"bucket_domain_name": {
Type: schema.TypeString,
Computed: true,
},
"arn": {
Type: schema.TypeString,
Optional: true,
@ -534,6 +539,8 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
d.Set("bucket", d.Id())
}
d.Set("bucket_domain_name", bucketDomainName(d.Get("bucket").(string)))
// Read the policy
if _, ok := d.GetOk("policy"); ok {
pol, err := s3conn.GetBucketPolicy(&s3.GetBucketPolicyInput{
@ -1207,6 +1214,10 @@ func websiteEndpoint(s3conn *s3.S3, d *schema.ResourceData) (*S3Website, error)
return WebsiteEndpoint(bucket, region), nil
}
func bucketDomainName(bucket string) string {
return fmt.Sprintf("%s.s3.amazonaws.com", bucket)
}
func WebsiteEndpoint(bucket string, region string) *S3Website {
domain := WebsiteDomainUrl(region)
return &S3Website{Endpoint: fmt.Sprintf("%s.%s", bucket, domain), Domain: domain}

View File

@ -46,6 +46,10 @@ func TestAccAWSS3Bucket_basic(t *testing.T) {
"aws_s3_bucket.bucket", "website_endpoint"),
resource.TestMatchResourceAttr(
"aws_s3_bucket.bucket", "arn", arnRegexp),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "bucket", testAccBucketName(rInt)),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "bucket_domain_name", testAccBucketDomainName(rInt)),
),
},
},
@ -1089,6 +1093,14 @@ func testAccCheckAWSS3BucketLogging(n, b, p string) resource.TestCheckFunc {
// These need a bit of randomness as the name can only be used once globally
// within AWS
func testAccBucketName(randInt int) string {
return fmt.Sprintf("tf-test-bucket-%d", randInt)
}
func testAccBucketDomainName(randInt int) string {
return fmt.Sprintf("tf-test-bucket-%d.s3.amazonaws.com", randInt)
}
func testAccWebsiteEndpoint(randInt int) string {
return fmt.Sprintf("tf-test-bucket-%d.s3-website-us-west-2.amazonaws.com", randInt)
}

View File

@ -36,7 +36,7 @@ resource "aws_s3_bucket" "b" {
resource "aws_cloudfront_distribution" "s3_distribution" {
origin {
domain_name = "${aws_s3_bucket.b.bucket}.s3.amazonaws.com"
domain_name = "${aws_s3_bucket.b.bucket_domain_name}"
origin_id = "myS3Origin"
s3_origin_config {

View File

@ -380,6 +380,7 @@ The following attributes are exported:
* `id` - The name of the bucket.
* `arn` - The ARN of the bucket. Will be of format `arn:aws:s3:::bucketname`.
* `bucket_domain_name` - The bucket domain name. Will be of format `bucketname.s3.amazonaws.com`.
* `hosted_zone_id` - The [Route 53 Hosted Zone ID](https://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.