diff --git a/google/resource_storage_bucket.go b/google/resource_storage_bucket.go index 8620d1be..6cf86073 100644 --- a/google/resource_storage_bucket.go +++ b/google/resource_storage_bucket.go @@ -48,6 +48,11 @@ func resourceStorageBucket() *schema.Resource { }, }, + "requester_pays": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + }, + "force_destroy": &schema.Schema{ Type: schema.TypeBool, Optional: true, @@ -313,6 +318,12 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error sb.Encryption = expandBucketEncryption(v.([]interface{})) } + if v, ok := d.GetOk("requester_pays"); ok { + sb.Billing = &storage.BucketBilling{ + RequesterPays: v.(bool), + } + } + var res *storage.Bucket err = retry(func() error { @@ -342,6 +353,14 @@ func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error } } + if d.HasChange("requester_pays") { + v := d.Get("requester_pays") + sb.Billing = &storage.BucketBilling{ + RequesterPays: v.(bool), + ForceSendFields: []string{"RequesterPays"}, + } + } + if d.HasChange("versioning") { if v, ok := d.GetOk("versioning"); ok { sb.Versioning = expandBucketVersioning(v) @@ -471,6 +490,13 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error { d.Set("versioning", flattenBucketVersioning(res.Versioning)) d.Set("lifecycle_rule", flattenBucketLifecycle(res.Lifecycle)) d.Set("labels", res.Labels) + + if res.Billing == nil { + d.Set("requester_pays", nil) + } else { + d.Set("requester_pays", res.Billing.RequesterPays) + } + d.SetId(res.Id) return nil } diff --git a/google/resource_storage_bucket_test.go b/google/resource_storage_bucket_test.go index 5d78ebde..0786f9e5 100644 --- a/google/resource_storage_bucket_test.go +++ b/google/resource_storage_bucket_test.go @@ -47,6 +47,35 @@ func TestAccStorageBucket_basic(t *testing.T) { }) } +func TestAccStorageBucket_requesterPays(t *testing.T) { + t.Parallel() + + var bucket storage.Bucket + bucketName := fmt.Sprintf("tf-test-requester-bucket-%d", acctest.RandInt()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccStorageBucketDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccStorageBucket_requesterPays(bucketName, true), + Check: resource.ComposeTestCheckFunc( + testAccCheckStorageBucketExists( + "google_storage_bucket.bucket", bucketName, &bucket), + resource.TestCheckResourceAttr( + "google_storage_bucket.bucket", "requester_pays", "true"), + ), + }, + resource.TestStep{ + ResourceName: "google_storage_bucket.bucket", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccStorageBucket_lowercaseLocation(t *testing.T) { t.Parallel() @@ -206,6 +235,38 @@ func TestAccStorageBucket_storageClass(t *testing.T) { }, }) } +func TestAccStorageBucket_update_requesterPays(t *testing.T) { + t.Parallel() + + var bucket storage.Bucket + bucketName := fmt.Sprintf("tf-test-requester-bucket-%d", acctest.RandInt()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccStorageBucketDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccStorageBucket_requesterPays(bucketName, true), + Check: resource.ComposeTestCheckFunc( + testAccCheckStorageBucketExists( + "google_storage_bucket.bucket", bucketName, &bucket), + resource.TestCheckResourceAttr( + "google_storage_bucket.bucket", "requester_pays", "true"), + ), + }, + resource.TestStep{ + Config: testAccStorageBucket_requesterPays(bucketName, false), + Check: resource.ComposeTestCheckFunc( + testAccCheckStorageBucketExists( + "google_storage_bucket.bucket", bucketName, &bucket), + resource.TestCheckResourceAttr( + "google_storage_bucket.bucket", "requester_pays", "false"), + ), + }, + }, + }) +} func TestAccStorageBucket_update(t *testing.T) { t.Parallel() @@ -746,6 +807,15 @@ resource "google_storage_bucket" "bucket" { `, bucketName) } +func testAccStorageBucket_requesterPays(bucketName string, pays bool) string { + return fmt.Sprintf(` +resource "google_storage_bucket" "bucket" { + name = "%s" + requester_pays = %t +} +`, bucketName, pays) +} + func testAccStorageBucket_lowercaseLocation(bucketName string) string { return fmt.Sprintf(` resource "google_storage_bucket" "bucket" { diff --git a/website/docs/r/storage_bucket.html.markdown b/website/docs/r/storage_bucket.html.markdown index 053f3629..c82ec0aa 100644 --- a/website/docs/r/storage_bucket.html.markdown +++ b/website/docs/r/storage_bucket.html.markdown @@ -68,6 +68,8 @@ The following arguments are supported: * `encryption` - (Optional) The bucket's encryption configuration. +* `requester_pays` - (Optional, Default: false) Enables [Requester Pays](https://cloud.google.com/storage/docs/requester-pays) on a storage bucket. + The `lifecycle_rule` block supports: * `action` - (Required) The Lifecycle Rule's action configuration. A single block of this type is supported. Structure is documented below.