From 381a05c085612423a27c7cb191b120c55f094e31 Mon Sep 17 00:00:00 2001 From: Martin Nowak Date: Fri, 10 Jan 2020 17:19:46 +0100 Subject: [PATCH] add defaultEventBasedHold to storage buckets - fixes https://github.com/terraform-providers/terraform-provider-google/issues/4747 - description from https://cloud.google.com/storage/docs/json_api/v1/buckets/update#defaultEventBasedHold --- products/storage/api.yaml | 5 +++ .../resources/resource_storage_bucket.go | 17 ++++++++++ .../tests/resource_storage_bucket_test.go | 31 +++++++++++++++++++ third_party/validator/storage_bucket.go | 4 +++ 4 files changed, 57 insertions(+) diff --git a/products/storage/api.yaml b/products/storage/api.yaml index 7801219feb0e..2f390d5c6567 100644 --- a/products/storage/api.yaml +++ b/products/storage/api.yaml @@ -152,6 +152,11 @@ objects: The list of HTTP headers other than the simple response headers to give permission for the user-agent to share across domains. item_type: Api::Type::String + - !ruby/object:Api::Type::Boolean + name: 'defaultEventBasedHold' + description: | + Whether or not to automatically apply an eventBasedHold to new objects + added to the bucket. - !ruby/object:Api::Type::Array name: 'defaultObjectAcl' description: | diff --git a/third_party/terraform/resources/resource_storage_bucket.go b/third_party/terraform/resources/resource_storage_bucket.go index f2331af374fb..2e177870973f 100644 --- a/third_party/terraform/resources/resource_storage_bucket.go +++ b/third_party/terraform/resources/resource_storage_bucket.go @@ -263,6 +263,12 @@ func resourceStorageBucket() *schema.Resource { }, }, }, + + "default_event_based_hold": { + Type: schema.TypeBool, + Optional: true, + }, + "logging": { Type: schema.TypeList, Optional: true, @@ -354,6 +360,10 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error } } + if v, ok := d.GetOk("default_event_based_hold"); ok { + sb.DefaultEventBasedHold = v.(bool) + } + if v, ok := d.GetOk("cors"); ok { sb.Cors = expandCors(v.([]interface{})) } @@ -452,6 +462,12 @@ func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error sb.Cors = expandCors(v.([]interface{})) } + if d.HasChange("default_event_based_hold") { + v := d.Get("default_event_based_hold") + sb.DefaultEventBasedHold = v.(bool) + sb.ForceSendFields = append(sb.ForceSendFields, "DefaultEventBasedHold") + } + if d.HasChange("logging") { if v, ok := d.GetOk("logging"); ok { sb.Logging = expandBucketLogging(v.([]interface{})) @@ -567,6 +583,7 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error { d.Set("encryption", flattenBucketEncryption(res.Encryption)) d.Set("location", res.Location) d.Set("cors", flattenCors(res.Cors)) + d.Set("default_event_based_hold", res.DefaultEventBasedHold) d.Set("logging", flattenBucketLogging(res.Logging)) d.Set("versioning", flattenBucketVersioning(res.Versioning)) d.Set("lifecycle_rule", flattenBucketLifecycle(res.Lifecycle)) diff --git a/third_party/terraform/tests/resource_storage_bucket_test.go b/third_party/terraform/tests/resource_storage_bucket_test.go index e6d8eb1e11dc..b3140f2042c1 100644 --- a/third_party/terraform/tests/resource_storage_bucket_test.go +++ b/third_party/terraform/tests/resource_storage_bucket_test.go @@ -701,6 +701,28 @@ func TestAccStorageBucket_cors(t *testing.T) { }) } +func TestAccStorageBucket_defaultEventBasedHold(t *testing.T) { + t.Parallel() + + bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccStorageBucketDestroy, + Steps: []resource.TestStep{ + { + Config: testAccStorageBucket_defaultEventBasedHold(bucketName), + }, + { + ResourceName: "google_storage_bucket.bucket", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccStorageBucket_encryption(t *testing.T) { t.Parallel() @@ -1171,6 +1193,15 @@ resource "google_storage_bucket" "bucket" { `, bucketName) } +func testAccStorageBucket_defaultEventBasedHold(bucketName string) string { + return fmt.Sprintf(` +resource "google_storage_bucket" "bucket" { + name = "%s" + default_event_based_hold = true +} +`, bucketName) +} + func testAccStorageBucket_forceDestroyWithVersioning(bucketName string) string { return fmt.Sprintf(` resource "google_storage_bucket" "bucket" { diff --git a/third_party/validator/storage_bucket.go b/third_party/validator/storage_bucket.go index b23fe082f37c..08d2103f0f56 100644 --- a/third_party/validator/storage_bucket.go +++ b/third_party/validator/storage_bucket.go @@ -87,6 +87,10 @@ func GetStorageBucketApiObject(d TerraformResourceData, config *Config) (map[str sb.Cors = expandCors(v.([]interface{})) } + if v, ok := d.GetOk("default_event_based_hold"); ok { + sb.DefaultEventBasedHold = v.(bool) + } + if v, ok := d.GetOk("logging"); ok { sb.Logging = expandBucketLogging(v.([]interface{})) }