From f3b5a883b77cd503a04d24e8e57dce6eca9c2507 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Fri, 21 Apr 2017 06:28:40 +1000 Subject: [PATCH] Add `bucket_prefix` to `aws_s3_bucket` (#13274) Adds a `bucket_prefix` parameter to the `aws_s3_bucket` resource. --- .../providers/aws/resource_aws_s3_bucket.go | 19 +++++++- .../aws/resource_aws_s3_bucket_test.go | 46 +++++++++++++++++++ .../providers/aws/r/s3_bucket.html.markdown | 3 +- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/builtin/providers/aws/resource_aws_s3_bucket.go b/builtin/providers/aws/resource_aws_s3_bucket.go index 6e4ff7aab..7da1ac18f 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket.go +++ b/builtin/providers/aws/resource_aws_s3_bucket.go @@ -31,8 +31,15 @@ func resourceAwsS3Bucket() *schema.Resource { Schema: map[string]*schema.Schema{ "bucket": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ConflictsWith: []string{"bucket_prefix"}, + }, + "bucket_prefix": { Type: schema.TypeString, - Required: true, + Optional: true, ForceNew: true, }, @@ -389,7 +396,15 @@ func resourceAwsS3BucketCreate(d *schema.ResourceData, meta interface{}) error { s3conn := meta.(*AWSClient).s3conn // Get the bucket and acl - bucket := d.Get("bucket").(string) + var bucket string + if v, ok := d.GetOk("bucket"); ok { + bucket = v.(string) + } else if v, ok := d.GetOk("bucket_prefix"); ok { + bucket = resource.PrefixedUniqueId(v.(string)) + } else { + bucket = resource.UniqueId() + } + d.Set("bucket", bucket) acl := d.Get("acl").(string) log.Printf("[DEBUG] S3 bucket create: %s, ACL: %s", bucket, acl) diff --git a/builtin/providers/aws/resource_aws_s3_bucket_test.go b/builtin/providers/aws/resource_aws_s3_bucket_test.go index ff18b7810..a12c8ec54 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket_test.go +++ b/builtin/providers/aws/resource_aws_s3_bucket_test.go @@ -56,6 +56,40 @@ func TestAccAWSS3Bucket_basic(t *testing.T) { }) } +func TestAccAWSS3Bucket_namePrefix(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSS3BucketDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSS3BucketConfig_namePrefix, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSS3BucketExists("aws_s3_bucket.test"), + resource.TestMatchResourceAttr( + "aws_s3_bucket.test", "bucket", regexp.MustCompile("^tf-test-")), + ), + }, + }, + }) +} + +func TestAccAWSS3Bucket_generatedName(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSS3BucketDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSS3BucketConfig_generatedName, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSS3BucketExists("aws_s3_bucket.test"), + ), + }, + }, + }) +} + func TestAccAWSS3Bucket_region(t *testing.T) { rInt := acctest.RandInt() @@ -1601,3 +1635,15 @@ resource "aws_s3_bucket" "destination" { } `, randInt, randInt, randInt) } + +const testAccAWSS3BucketConfig_namePrefix = ` +resource "aws_s3_bucket" "test" { + bucket_prefix = "tf-test-" +} +` + +const testAccAWSS3BucketConfig_generatedName = ` +resource "aws_s3_bucket" "test" { + bucket_prefix = "tf-test-" +} +` 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 16f527798..e63f93f91 100644 --- a/website/source/docs/providers/aws/r/s3_bucket.html.markdown +++ b/website/source/docs/providers/aws/r/s3_bucket.html.markdown @@ -286,7 +286,8 @@ resource "aws_s3_bucket" "bucket" { The following arguments are supported: -* `bucket` - (Required) The name of the bucket. +* `bucket` - (Optional, Forces new resource) The name of the bucket. If omitted, Terraform will assign a random, unique name. +* `bucket_prefix` - (Optional, Forces new resource) Creates a unique bucket name beginning with the specified prefix. Conflicts with `name`. * `acl` - (Optional) The [canned ACL](https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl) to apply. Defaults to "private". * `policy` - (Optional) A valid [bucket policy](https://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html) JSON document. Note that if the policy document is not specific enough (but still valid), Terraform may view the policy as constantly changing in a `terraform plan`. In this case, please make sure you use the verbose/specific version of the policy.