From dc70177293665885d8957a55a2c3acafb7314045 Mon Sep 17 00:00:00 2001 From: The Magician Date: Wed, 19 Jun 2019 15:58:53 -0700 Subject: [PATCH] add nil checks to bucket policy and allow updating to false (#3886) Signed-off-by: Modular Magician --- google/resource_storage_bucket.go | 18 +++++++++++------- google/resource_storage_bucket_test.go | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/google/resource_storage_bucket.go b/google/resource_storage_bucket.go index 8488efaf24e..2d632c3e55f 100644 --- a/google/resource_storage_bucket.go +++ b/google/resource_storage_bucket.go @@ -524,7 +524,11 @@ 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) - d.Set("bucket_policy_only", res.IamConfiguration.BucketPolicyOnly.Enabled) + if res.IamConfiguration != nil && res.IamConfiguration.BucketPolicyOnly != nil { + d.Set("bucket_policy_only", res.IamConfiguration.BucketPolicyOnly.Enabled) + } else { + d.Set("bucket_policy_only", false) + } if res.Billing == nil { d.Set("requester_pays", nil) @@ -795,13 +799,13 @@ func flattenBucketLifecycleRuleCondition(condition *storage.BucketLifecycleRuleC } func expandIamConfiguration(d *schema.ResourceData) *storage.BucketIamConfiguration { - iamConfig := &storage.BucketIamConfiguration{} - if v, ok := d.GetOk("bucket_policy_only"); ok { - iamConfig.BucketPolicyOnly = &storage.BucketIamConfigurationBucketPolicyOnly{ - Enabled: v.(bool), - } + return &storage.BucketIamConfiguration{ + ForceSendFields: []string{"BucketPolicyOnly"}, + BucketPolicyOnly: &storage.BucketIamConfigurationBucketPolicyOnly{ + Enabled: d.Get("bucket_policy_only").(bool), + ForceSendFields: []string{"Enabled"}, + }, } - return iamConfig } func expandStorageBucketLifecycle(v interface{}) (*storage.BucketLifecycle, error) { diff --git a/google/resource_storage_bucket_test.go b/google/resource_storage_bucket_test.go index 68bd9fcd2c4..65ba6267439 100644 --- a/google/resource_storage_bucket_test.go +++ b/google/resource_storage_bucket_test.go @@ -772,7 +772,7 @@ func TestAccStorageBucket_bucketPolicyOnly(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccStorageBucket_bucketPolicyOnly(bucketName, false), + Config: testAccStorageBucket_bucketPolicyOnly(bucketName, true), }, { ResourceName: "google_storage_bucket.bucket", @@ -780,7 +780,7 @@ func TestAccStorageBucket_bucketPolicyOnly(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccStorageBucket_bucketPolicyOnly(bucketName, true), + Config: testAccStorageBucket_bucketPolicyOnly(bucketName, false), }, { ResourceName: "google_storage_bucket.bucket",