Skip to content

Commit

Permalink
Add rpo attribute to 'google_storage_bucket' resource (#9574)
Browse files Browse the repository at this point in the history
* Add rpo attribute for 'google_storage_bucket' resource

* Modifies test-case, markdown and Fixes indent issues

* Modifies rpo markdown and dualLocation_rpo testcase.
[upstream:680968fde8a58272df843d8c2dc6867ac8a176b7]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician committed Dec 8, 2023
1 parent eae87a8 commit 63805f7
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/9574.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
storage: added 'rpo' field to 'google_storage_bucket' resource.
```
26 changes: 25 additions & 1 deletion google-beta/services/storage/resource_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,12 @@ func ResourceStorageBucket() *schema.Resource {
},
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.`,
},
"rpo": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the RPO setting of bucket. If set 'ASYNC_TURBO', The Turbo Replication will be enabled for the dual-region bucket. Value 'DEFAULT' will set RPO setting to default. Turbo Replication is only for buckets in dual-regions.See the docs for more details.`,
},
"public_access_prevention": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -603,6 +609,10 @@ func resourceStorageBucketCreate(d *schema.ResourceData, meta interface{}) error
sb.CustomPlacementConfig = expandBucketCustomPlacementConfig(v.([]interface{}))
}

if v, ok := d.GetOk("rpo"); ok {
sb.Rpo = v.(string)
}

var res *storage.Bucket

err = transport_tpg.Retry(transport_tpg.RetryOptions{
Expand Down Expand Up @@ -766,6 +776,14 @@ func resourceStorageBucketUpdate(d *schema.ResourceData, meta interface{}) error
sb.IamConfiguration = expandIamConfiguration(d)
}

if d.HasChange("rpo") {
if v, ok := d.GetOk("rpo"); ok {
sb.Rpo = v.(string)
} else {
sb.NullFields = append(sb.NullFields, "Rpo")
}
}

res, err := config.NewStorageClient(userAgent).Buckets.Patch(d.Get("name").(string), sb).Do()
if err != nil {
return err
Expand Down Expand Up @@ -1692,7 +1710,13 @@ func setStorageBucket(d *schema.ResourceData, config *transport_tpg.Config, res
if err := d.Set("custom_placement_config", flattenBucketCustomPlacementConfig(res.CustomPlacementConfig)); err != nil {
return fmt.Errorf("Error setting custom_placement_config: %s", err)
}

// Needs to hide rpo field for single-region buckets.
// Check the Rpo field from API response to determine whether bucket is in single region config or not.
if res.Rpo != "" {
if err := d.Set("rpo", res.Rpo); err != nil {
return fmt.Errorf("Error setting RPO setting : %s", err)
}
}
if res.IamConfiguration != nil && res.IamConfiguration.UniformBucketLevelAccess != nil {
if err := d.Set("uniform_bucket_level_access", res.IamConfiguration.UniformBucketLevelAccess.Enabled); err != nil {
return fmt.Errorf("Error setting uniform_bucket_level_access: %s", err)
Expand Down
117 changes: 117 additions & 0 deletions google-beta/services/storage/resource_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,98 @@ func TestAccStorageBucket_dualLocation(t *testing.T) {
})
}

func TestAccStorageBucket_dualLocation_rpo(t *testing.T) {
t.Parallel()
bucketName := acctest.TestBucketName(t)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccStorageBucketDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccStorageBucket_dualLocation(bucketName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "rpo", "DEFAULT"),
),
},
{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
{
Config: testAccStorageBucket_dualLocation_rpo(bucketName, "ASYNC_TURBO"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "rpo", "ASYNC_TURBO"),
),
},
{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
{
Config: testAccStorageBucket_dualLocation_rpo(bucketName, "DEFAULT"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "rpo", "DEFAULT"),
),
},
{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}

func TestAccStorageBucket_multiLocation_rpo(t *testing.T) {
t.Parallel()

bucketName := acctest.TestBucketName(t)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccStorageBucketDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccStorageBucket_basic(bucketName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "rpo", "DEFAULT"),
),
},
{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
{
Config: testAccStorageBucket_multiLocation_rpo(bucketName, "DEFAULT"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_storage_bucket.bucket", "rpo", "DEFAULT"),
),
},
{
ResourceName: "google_storage_bucket.bucket",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}

func TestAccStorageBucket_customAttributes(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1492,6 +1584,31 @@ resource "google_storage_bucket" "bucket" {
`, bucketName)
}

func testAccStorageBucket_dualLocation_rpo(bucketName string, rpo string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
location = "ASIA"
force_destroy = true
custom_placement_config {
data_locations = ["ASIA-EAST1", "ASIA-SOUTHEAST1"]
}
rpo = "%s"
}
`, bucketName, rpo)
}

func testAccStorageBucket_multiLocation_rpo(bucketName string, rpo string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
location = "ASIA"
force_destroy = true
rpo = "%s"
}
`, bucketName, rpo)
}

func testAccStorageBucket_customAttributes(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/storage_bucket.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ The following arguments are supported:

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

* `rpo` - (Optional) The recovery point objective for cross-region replication of the bucket. Applicable only for dual and multi-region buckets. `"DEFAULT"` sets default replication. `"ASYNC_TURBO"` value enables turbo replication, valid for dual-region buckets only. See [Turbo Replication](https://cloud.google.com/storage/docs/managing-turbo-replication) for more information. If rpo is not specified at bucket creation, it defaults to `"DEFAULT"` for dual and multi-region buckets. **NOTE** If used with single-region bucket, It will throw an error.

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

* `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".
Expand Down

0 comments on commit 63805f7

Please sign in to comment.