Skip to content

Commit 1e73f0f

Browse files
Deprecate bucket policy field in google_storage_bucket (#3916) (#2442)
Signed-off-by: Modular Magician <[email protected]>
1 parent eef4542 commit 1e73f0f

File tree

4 files changed

+91
-12
lines changed

4 files changed

+91
-12
lines changed

.changelog/3916.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:deprecation
2+
storage: deprecated `bucket_policy_only` field in `google_storage_bucket` in favour of `uniform_bucket_level_access`
3+
```

google-beta/resource_storage_bucket.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,19 @@ func resourceStorageBucket() *schema.Resource {
325325
Description: `The bucket's Access & Storage Logs configuration.`,
326326
},
327327
"bucket_policy_only": {
328-
Type: schema.TypeBool,
329-
Optional: true,
330-
Computed: true,
331-
Description: `Enables Bucket Policy Only access to a bucket.`,
328+
Type: schema.TypeBool,
329+
Optional: true,
330+
Computed: true,
331+
Description: `Enables Bucket Policy Only access to a bucket.`,
332+
Deprecated: `Please use the uniform_bucket_level_access as this field has been renamed by Google.`,
333+
ConflictsWith: []string{"uniform_bucket_level_access"},
334+
},
335+
"uniform_bucket_level_access": {
336+
Type: schema.TypeBool,
337+
Optional: true,
338+
Computed: true,
339+
Description: `Enables uniform bucket-level access on a bucket.`,
340+
ConflictsWith: []string{"bucket_policy_only"},
332341
},
333342
},
334343
}
@@ -550,6 +559,9 @@ func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error
550559
if d.HasChange("bucket_policy_only") {
551560
sb.IamConfiguration = expandIamConfiguration(d)
552561
}
562+
if d.HasChange("uniform_bucket_level_access") {
563+
sb.IamConfiguration = expandIamConfiguration(d)
564+
}
553565

554566
res, err := config.clientStorage.Buckets.Patch(d.Get("name").(string), sb).Do()
555567

@@ -632,10 +644,13 @@ func resourceStorageBucketRead(d *schema.ResourceData, meta interface{}) error {
632644
d.Set("website", flattenBucketWebsite(res.Website))
633645
d.Set("retention_policy", flattenBucketRetentionPolicy(res.RetentionPolicy))
634646

635-
if res.IamConfiguration != nil && res.IamConfiguration.BucketPolicyOnly != nil {
636-
d.Set("bucket_policy_only", res.IamConfiguration.BucketPolicyOnly.Enabled)
647+
// Delete the bucket_policy_only field in the next major version of the provider.
648+
if res.IamConfiguration != nil && res.IamConfiguration.UniformBucketLevelAccess != nil {
649+
d.Set("uniform_bucket_level_access", res.IamConfiguration.UniformBucketLevelAccess.Enabled)
650+
d.Set("bucket_policy_only", res.IamConfiguration.UniformBucketLevelAccess.Enabled)
637651
} else {
638652
d.Set("bucket_policy_only", false)
653+
d.Set("uniform_bucket_level_access", false)
639654
}
640655

641656
if res.Billing == nil {
@@ -995,20 +1010,41 @@ func expandBucketWebsite(v interface{}) *storage.BucketWebsite {
9951010
if v := website["main_page_suffix"]; v != "" {
9961011
w.MainPageSuffix = v.(string)
9971012
}
998-
9991013
return w
10001014
}
10011015

1016+
// remove this on next major release of the provider.
10021017
func expandIamConfiguration(d *schema.ResourceData) *storage.BucketIamConfiguration {
1018+
// We are checking for a change because the last else block is only executed on Create.
1019+
enabled := false
1020+
if d.HasChange("bucket_policy_only") {
1021+
enabled = d.Get("bucket_policy_only").(bool)
1022+
} else if d.HasChange("uniform_bucket_level_access") {
1023+
enabled = d.Get("uniform_bucket_level_access").(bool)
1024+
} else {
1025+
enabled = d.Get("bucket_policy_only").(bool) || d.Get("uniform_bucket_level_access").(bool)
1026+
}
1027+
10031028
return &storage.BucketIamConfiguration{
1004-
ForceSendFields: []string{"BucketPolicyOnly"},
1005-
BucketPolicyOnly: &storage.BucketIamConfigurationBucketPolicyOnly{
1006-
Enabled: d.Get("bucket_policy_only").(bool),
1029+
ForceSendFields: []string{"UniformBucketLevelAccess"},
1030+
UniformBucketLevelAccess: &storage.BucketIamConfigurationUniformBucketLevelAccess{
1031+
Enabled: enabled,
10071032
ForceSendFields: []string{"Enabled"},
10081033
},
10091034
}
10101035
}
10111036

1037+
// Uncomment once the previous function is removed.
1038+
// func expandIamConfiguration(d *schema.ResourceData) *storage.BucketIamConfiguration {
1039+
// return &storage.BucketIamConfiguration{
1040+
// ForceSendFields: []string{"UniformBucketLevelAccess"},
1041+
// UniformBucketLevelAccess: &storage.BucketIamConfigurationUniformBucketLevelAccess{
1042+
// Enabled: d.Get("uniform_bucket_level_access").(bool),
1043+
// ForceSendFields: []string{"Enabled"},
1044+
// },
1045+
// }
1046+
// }
1047+
10121048
func expandStorageBucketLifecycle(v interface{}) (*storage.BucketLifecycle, error) {
10131049
if v == nil {
10141050
return &storage.BucketLifecycle{

google-beta/resource_storage_bucket_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,35 @@ func TestAccStorageBucket_bucketPolicyOnly(t *testing.T) {
762762
})
763763
}
764764

765+
func TestAccStorageBucket_uniformBucketAccessOnly(t *testing.T) {
766+
t.Parallel()
767+
768+
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", randInt(t))
769+
770+
vcrTest(t, resource.TestCase{
771+
PreCheck: func() { testAccPreCheck(t) },
772+
Providers: testAccProviders,
773+
Steps: []resource.TestStep{
774+
{
775+
Config: testAccStorageBucket_uniformBucketAccessOnly(bucketName, true),
776+
},
777+
{
778+
ResourceName: "google_storage_bucket.bucket",
779+
ImportState: true,
780+
ImportStateVerify: true,
781+
},
782+
{
783+
Config: testAccStorageBucket_uniformBucketAccessOnly(bucketName, false),
784+
},
785+
{
786+
ResourceName: "google_storage_bucket.bucket",
787+
ImportState: true,
788+
ImportStateVerify: true,
789+
},
790+
},
791+
})
792+
}
793+
765794
func TestAccStorageBucket_labels(t *testing.T) {
766795
t.Parallel()
767796

@@ -1375,6 +1404,15 @@ resource "google_storage_bucket" "bucket" {
13751404
`, bucketName, enabled)
13761405
}
13771406

1407+
func testAccStorageBucket_uniformBucketAccessOnly(bucketName string, enabled bool) string {
1408+
return fmt.Sprintf(`
1409+
resource "google_storage_bucket" "bucket" {
1410+
name = "%s"
1411+
uniform_bucket_level_access = %t
1412+
}
1413+
`, bucketName, enabled)
1414+
}
1415+
13781416
func testAccStorageBucket_encryption(context map[string]interface{}) string {
13791417
return Nprintf(`
13801418
resource "google_project" "acceptance" {

website/docs/r/storage_bucket.html.markdown

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ resource "google_storage_bucket" "static-site" {
3131
location = "EU"
3232
force_destroy = true
3333
34-
bucket_policy_only = true
34+
uniform_bucket_level_access = true
3535
3636
website {
3737
main_page_suffix = "index.html"
@@ -101,7 +101,9 @@ The following arguments are supported:
101101

102102
* `requester_pays` - (Optional, Default: false) Enables [Requester Pays](https://cloud.google.com/storage/docs/requester-pays) on a storage bucket.
103103

104-
* `bucket_policy_only` - (Optional, Default: false) Enables [Bucket Policy Only](https://cloud.google.com/storage/docs/bucket-policy-only) access to a bucket.
104+
* `bucket_policy_only` - (Deprecated, Default: false) Enables [Bucket Policy Only](https://cloud.google.com/storage/docs/bucket-policy-only) access to a bucket. This field will be removed in the next major release of the provider.
105+
106+
* `uniform_bucket_level_access` - (Optional, Default: false) Enables [Uniform bucket-level access](https://cloud.google.com/storage/docs/uniform-bucket-level-access) access to a bucket.
105107

106108
The `lifecycle_rule` block supports:
107109

0 commit comments

Comments
 (0)