Skip to content

Commit 148c195

Browse files
google_storage_bucket: fix custom_placement_config values not normalized (#10936) (#7551)
[upstream:4c0b1d9ce480bb5a8f56c0643da68e1415c147cf] Signed-off-by: Modular Magician <[email protected]>
1 parent f051ec7 commit 148c195

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

.changelog/10936.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
storage: fixed `custom_placement_config` values not normalized in `google_storage_bucket`
3+
```

google-beta/services/storage/resource_storage_bucket.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ func ResourceStorageBucket() *schema.Resource {
497497
MinItems: 2,
498498
Elem: &schema.Schema{
499499
Type: schema.TypeString,
500+
StateFunc: func(s interface{}) string {
501+
return strings.ToUpper(s.(string))
502+
},
500503
},
501504
Description: `The list of individual regions that comprise a dual-region bucket. See the docs for a list of acceptable regions. Note: If any of the data_locations changes, it will recreate the bucket.`,
502505
},
@@ -1170,9 +1173,15 @@ func flattenBucketCustomPlacementConfig(cfc *storage.BucketCustomPlacementConfig
11701173
func expandBucketDataLocations(configured interface{}) []string {
11711174
l := configured.(*schema.Set).List()
11721175

1176+
// Since we only want uppercase values to prevent unnecessary diffs, we can do a comparison
1177+
// to determine whether or not to include the value as part of the request.
1178+
1179+
// This extra check comes from the limitations of both DiffStateFunc and StateFunc towards types of Sets,Lists, and Maps.
11731180
req := make([]string, 0, len(l))
11741181
for _, raw := range l {
1175-
req = append(req, raw.(string))
1182+
if raw.(string) == strings.ToUpper(raw.(string)) {
1183+
req = append(req, raw.(string))
1184+
}
11761185
}
11771186
return req
11781187
}

google-beta/services/storage/resource_storage_bucket_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,81 @@ func TestAccStorageBucket_dualLocation(t *testing.T) {
250250
})
251251
}
252252

253+
func TestAccStorageBucket_dualLocation_lowercase(t *testing.T) {
254+
t.Parallel()
255+
256+
bucketName := acctest.TestBucketName(t)
257+
258+
acctest.VcrTest(t, resource.TestCase{
259+
PreCheck: func() { acctest.AccTestPreCheck(t) },
260+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
261+
CheckDestroy: testAccStorageBucketDestroyProducer(t),
262+
Steps: []resource.TestStep{
263+
{
264+
Config: testAccStorageBucket_dualLocation_lowercase(bucketName),
265+
},
266+
{
267+
ResourceName: "google_storage_bucket.bucket",
268+
ImportState: true,
269+
ImportStateVerify: true,
270+
ImportStateVerifyIgnore: []string{"force_destroy"},
271+
},
272+
},
273+
})
274+
}
275+
276+
func TestAccStorageBucket_dualLocation_versionChange(t *testing.T) {
277+
// Test is not parallel because ENVs are set.
278+
// Need to skip VCR as this test downloads providers from the Terraform Registry
279+
acctest.SkipIfVcr(t)
280+
281+
creds := envvar.GetTestCredsFromEnv()
282+
project := envvar.GetTestProjectFromEnv()
283+
t.Setenv("GOOGLE_CREDENTIALS", creds)
284+
t.Setenv("GOOGLE_PROJECT", project)
285+
286+
bucketName := acctest.TestBucketName(t)
287+
288+
acctest.VcrTest(t, resource.TestCase{
289+
PreCheck: func() { acctest.AccTestPreCheck(t) },
290+
CheckDestroy: testAccStorageBucketDestroyProducer(t),
291+
Steps: []resource.TestStep{
292+
{
293+
Config: testAccStorageBucket_dualLocation(bucketName),
294+
ExternalProviders: map[string]resource.ExternalProvider{
295+
"google": {
296+
VersionConstraint: "5.30.0",
297+
Source: "hashicorp/google",
298+
},
299+
},
300+
},
301+
{
302+
ResourceName: "google_storage_bucket.bucket",
303+
ExternalProviders: map[string]resource.ExternalProvider{
304+
"google": {
305+
VersionConstraint: "5.30.0",
306+
Source: "hashicorp/google",
307+
},
308+
},
309+
ImportState: true,
310+
ImportStateVerify: true,
311+
ImportStateVerifyIgnore: []string{"force_destroy"},
312+
},
313+
{
314+
Config: testAccStorageBucket_dualLocation(bucketName),
315+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
316+
},
317+
{
318+
ResourceName: "google_storage_bucket.bucket",
319+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
320+
ImportState: true,
321+
ImportStateVerify: true,
322+
ImportStateVerifyIgnore: []string{"force_destroy"},
323+
},
324+
},
325+
})
326+
}
327+
253328
func TestAccStorageBucket_dualLocation_rpo(t *testing.T) {
254329
t.Parallel()
255330
bucketName := acctest.TestBucketName(t)
@@ -1715,6 +1790,19 @@ resource "google_storage_bucket" "bucket" {
17151790
`, bucketName)
17161791
}
17171792

1793+
func testAccStorageBucket_dualLocation_lowercase(bucketName string) string {
1794+
return fmt.Sprintf(`
1795+
resource "google_storage_bucket" "bucket" {
1796+
name = "%s"
1797+
location = "ASIA"
1798+
force_destroy = true
1799+
custom_placement_config {
1800+
data_locations = ["asia-east1", "asia-southeast1"]
1801+
}
1802+
}
1803+
`, bucketName)
1804+
}
1805+
17181806
func testAccStorageBucket_dualLocation_rpo(bucketName string, rpo string) string {
17191807
return fmt.Sprintf(`
17201808
resource "google_storage_bucket" "bucket" {

0 commit comments

Comments
 (0)