Skip to content

Commit d1907ae

Browse files
Promote public_access_prevention field on google_storage_bucket resource to GA, add to documentation (#6683) (#12766)
* Promote `public_access_prevention` field of `google_storage_bucket` resource to GA, add to documentation * Update description of new field, add new example Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent b56aa65 commit d1907ae

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

.changelog/6683.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
storage: Promoted `public_access_prevention` field on `google_storage_bucket` resource to GA
3+
```

google/resource_storage_bucket.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ func resourceStorageBucket() *schema.Resource {
386386
},
387387
Description: `The bucket's custom location configuration, which specifies the individual regions that comprise a dual-region bucket. If the bucket is designated a single or multi-region, the parameters are empty.`,
388388
},
389+
"public_access_prevention": {
390+
Type: schema.TypeString,
391+
Optional: true,
392+
Computed: true,
393+
Description: `Prevents public access to a bucket.`,
394+
},
389395
},
390396
UseJSONNumber: true,
391397
}
@@ -650,7 +656,7 @@ func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error
650656
}
651657
}
652658

653-
if d.HasChange("uniform_bucket_level_access") {
659+
if d.HasChange("uniform_bucket_level_access") || d.HasChange("public_access_prevention") {
654660
sb.IamConfiguration = expandIamConfiguration(d)
655661
}
656662

@@ -1142,6 +1148,10 @@ func expandIamConfiguration(d *schema.ResourceData) *storage.BucketIamConfigurat
11421148
},
11431149
}
11441150

1151+
if v, ok := d.GetOk("public_access_prevention"); ok {
1152+
cfg.PublicAccessPrevention = v.(string)
1153+
}
1154+
11451155
return cfg
11461156
}
11471157

@@ -1506,6 +1516,12 @@ func setStorageBucket(d *schema.ResourceData, config *Config, res *storage.Bucke
15061516
}
15071517
}
15081518

1519+
if res.IamConfiguration != nil && res.IamConfiguration.PublicAccessPrevention != "" {
1520+
if err := d.Set("public_access_prevention", res.IamConfiguration.PublicAccessPrevention); err != nil {
1521+
return fmt.Errorf("Error setting public_access_prevention: %s", err)
1522+
}
1523+
}
1524+
15091525
if res.Billing == nil {
15101526
if err := d.Set("requester_pays", nil); err != nil {
15111527
return fmt.Errorf("Error setting requester_pays: %s", err)

google/resource_storage_bucket_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,28 @@ func TestAccStorageBucket_encryption(t *testing.T) {
866866
})
867867
}
868868

869+
func TestAccStorageBucket_publicAccessPrevention(t *testing.T) {
870+
t.Parallel()
871+
872+
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", randInt(t))
873+
874+
vcrTest(t, resource.TestCase{
875+
PreCheck: func() { testAccPreCheck(t) },
876+
Providers: testAccProviders,
877+
Steps: []resource.TestStep{
878+
{
879+
Config: testAccStorageBucket_publicAccessPrevention(bucketName, "enforced"),
880+
},
881+
{
882+
ResourceName: "google_storage_bucket.bucket",
883+
ImportState: true,
884+
ImportStateVerify: true,
885+
ImportStateVerifyIgnore: []string{"force_destroy"},
886+
},
887+
},
888+
})
889+
}
890+
869891
func TestAccStorageBucket_uniformBucketAccessOnly(t *testing.T) {
870892
t.Parallel()
871893

@@ -1785,6 +1807,17 @@ resource "google_storage_bucket" "bucket" {
17851807
`, bucketName, enabled)
17861808
}
17871809

1810+
func testAccStorageBucket_publicAccessPrevention(bucketName string, prevention string) string {
1811+
return fmt.Sprintf(`
1812+
resource "google_storage_bucket" "bucket" {
1813+
name = "%s"
1814+
location = "US"
1815+
public_access_prevention = "%s"
1816+
force_destroy = true
1817+
}
1818+
`, bucketName, prevention)
1819+
}
1820+
17881821
func testAccStorageBucket_encryption(context map[string]interface{}) string {
17891822
return Nprintf(`
17901823
resource "google_project" "acceptance" {

website/docs/r/storage_bucket.html.markdown

+15
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ resource "google_storage_bucket" "auto-expire" {
6060
}
6161
}
6262
```
63+
64+
## Example Usage - Enabling public access prevention
65+
66+
```hcl
67+
resource "google_storage_bucket" "auto-expire" {
68+
name = "no-public-access-bucket"
69+
location = "US"
70+
force_destroy = true
71+
72+
public_access_prevention = "enforced"
73+
}
74+
```
75+
6376
## Argument Reference
6477

6578
The following arguments are supported:
@@ -101,6 +114,8 @@ The following arguments are supported:
101114

102115
* `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.
103116

117+
* `public_access_prevention` - (Optional) Prevents public access to a bucket. Acceptable values are "inherited" or "enforced". If "inherited", the bucket uses [public access prevention](https://cloud.google.com/storage/docs/public-access-prevention). only if the bucket is subject to the public access prevention organization policy constraint. Defaults to "inherited".
118+
104119
* `custom_placement_config` - (Optional) The bucket's custom location configuration, which specifies the individual regions that comprise a dual-region bucket. If the bucket is designated a single or multi-region, the parameters are empty. Structure is [documented below](#nested_custom_placement_config).
105120

106121
<a name="nested_lifecycle_rule"></a>The `lifecycle_rule` block supports:

0 commit comments

Comments
 (0)