Add `bucket_prefix` to `aws_s3_bucket` (#13274)

Adds a `bucket_prefix` parameter to the `aws_s3_bucket` resource.
This commit is contained in:
Joshua Spence 2017-04-21 06:28:40 +10:00 committed by Paul Stack
parent 280b9cf74d
commit f3b5a883b7
3 changed files with 65 additions and 3 deletions

View File

@ -32,7 +32,14 @@ func resourceAwsS3Bucket() *schema.Resource {
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"bucket": { "bucket": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"bucket_prefix"},
},
"bucket_prefix": {
Type: schema.TypeString,
Optional: true,
ForceNew: true, ForceNew: true,
}, },
@ -389,7 +396,15 @@ func resourceAwsS3BucketCreate(d *schema.ResourceData, meta interface{}) error {
s3conn := meta.(*AWSClient).s3conn s3conn := meta.(*AWSClient).s3conn
// Get the bucket and acl // 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) acl := d.Get("acl").(string)
log.Printf("[DEBUG] S3 bucket create: %s, ACL: %s", bucket, acl) log.Printf("[DEBUG] S3 bucket create: %s, ACL: %s", bucket, acl)

View File

@ -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) { func TestAccAWSS3Bucket_region(t *testing.T) {
rInt := acctest.RandInt() rInt := acctest.RandInt()
@ -1601,3 +1635,15 @@ resource "aws_s3_bucket" "destination" {
} }
`, randInt, randInt, randInt) `, 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-"
}
`

View File

@ -286,7 +286,8 @@ resource "aws_s3_bucket" "bucket" {
The following arguments are supported: 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". * `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. * `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.