Skip to content

Commit b74c44e

Browse files
functions2 align storage object generation to other storage source attributes (#11287) (#19031)
[upstream:fdf0c088f9ad24207e61421c5810ebbbb1032b2a] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent a3cdb04 commit b74c44e

File tree

3 files changed

+91
-8
lines changed

3 files changed

+91
-8
lines changed

.changelog/11287.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
cloudfunctions2: fixed an issue with `google_cloudfunctions2_function` where `storage_source.generation` would always show a diff
3+
```

google/services/cloudfunctions2/resource_cloudfunctions2_function.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,16 @@ func flattenCloudfunctions2functionBuildConfigSourceStorageSourceObject(v interf
11401140
}
11411141

11421142
func flattenCloudfunctions2functionBuildConfigSourceStorageSourceGeneration(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1143+
// This flatten function is shared between the resource and the datasource.
1144+
// TF Input will use the generation from the source object
1145+
// GET Response will use the generation from the automatically created object
1146+
// As TF Input and GET response values have different format,
1147+
// we will return TF Input value to prevent state drift.
1148+
1149+
if genVal, ok := d.GetOk("build_config.0.source.0.storage_source.0.generation"); ok {
1150+
v = genVal
1151+
}
1152+
11431153
// Handles the string fixed64 format
11441154
if strVal, ok := v.(string); ok {
11451155
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {

google/services/cloudfunctions2/resource_cloudfunctions2_function_test.go

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestAccCloudFunctions2Function_update(t *testing.T) {
3030
ResourceName: "google_cloudfunctions2_function.terraform-test2",
3131
ImportState: true,
3232
ImportStateVerify: true,
33-
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
33+
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.generation", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
3434
},
3535
{
3636
Config: testAccCloudFunctions2Function_test_update(context),
@@ -39,7 +39,7 @@ func TestAccCloudFunctions2Function_update(t *testing.T) {
3939
ResourceName: "google_cloudfunctions2_function.terraform-test2",
4040
ImportState: true,
4141
ImportStateVerify: true,
42-
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
42+
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.generation", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
4343
},
4444
{
4545
Config: testAccCloudFunctions2Function_test_redeploy(context),
@@ -48,7 +48,7 @@ func TestAccCloudFunctions2Function_update(t *testing.T) {
4848
ResourceName: "google_cloudfunctions2_function.terraform-test2",
4949
ImportState: true,
5050
ImportStateVerify: true,
51-
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
51+
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.generation", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
5252
},
5353
},
5454
})
@@ -117,7 +117,7 @@ resource "google_cloudfunctions2_function" "terraform-test2" {
117117
labels = {
118118
env = "test-update"
119119
}
120-
120+
121121
build_config {
122122
runtime = "nodejs18"
123123
entry_point = "helloHttp"
@@ -343,7 +343,7 @@ func TestAccCloudFunctions2Function_updateAbiuFull(t *testing.T) {
343343
ResourceName: "google_cloudfunctions2_function.terraform-test2",
344344
ImportState: true,
345345
ImportStateVerify: true,
346-
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
346+
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.generation", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
347347
},
348348
{
349349
Config: testAccCloudFunctions2Function_test_abiuUpdate(context),
@@ -352,7 +352,7 @@ func TestAccCloudFunctions2Function_updateAbiuFull(t *testing.T) {
352352
ResourceName: "google_cloudfunctions2_function.terraform-test2",
353353
ImportState: true,
354354
ImportStateVerify: true,
355-
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
355+
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.generation", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
356356
},
357357
{
358358
Config: testAccCloudFunctions2Function_test_abiuUpdate2(context),
@@ -361,7 +361,7 @@ func TestAccCloudFunctions2Function_updateAbiuFull(t *testing.T) {
361361
ResourceName: "google_cloudfunctions2_function.terraform-test2",
362362
ImportState: true,
363363
ImportStateVerify: true,
364-
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
364+
ImportStateVerifyIgnore: []string{"location", "build_config.0.source.0.storage_source.0.object", "build_config.0.source.0.storage_source.0.generation", "build_config.0.source.0.storage_source.0.bucket", "labels", "terraform_labels"},
365365
},
366366
},
367367
})
@@ -472,7 +472,7 @@ resource "google_cloudfunctions2_function" "terraform-test2" {
472472
labels = {
473473
env = "test-update"
474474
}
475-
475+
476476
build_config {
477477
runtime = "nodejs18"
478478
entry_point = "helloHttp"
@@ -490,3 +490,73 @@ resource "google_cloudfunctions2_function" "terraform-test2" {
490490
}
491491
`, context)
492492
}
493+
494+
func TestAccCloudFunctions2Function_generation(t *testing.T) {
495+
t.Parallel()
496+
497+
context := map[string]interface{}{
498+
"zip_path": "./test-fixtures/function-source.zip",
499+
"random_suffix": acctest.RandString(t, 10),
500+
}
501+
502+
acctest.VcrTest(t, resource.TestCase{
503+
PreCheck: func() { acctest.AccTestPreCheck(t) },
504+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
505+
CheckDestroy: testAccCheckCloudfunctions2functionDestroyProducer(t),
506+
Steps: []resource.TestStep{
507+
{
508+
Config: testAccCloudfunctions2function_generation(context),
509+
Check: resource.ComposeTestCheckFunc(
510+
resource.TestCheckOutput("object_gen_eq_storage_source_gen", "true"),
511+
),
512+
},
513+
},
514+
})
515+
}
516+
517+
func testAccCloudfunctions2function_generation(context map[string]interface{}) string {
518+
return acctest.Nprintf(`
519+
resource "google_storage_bucket" "bucket" {
520+
name = "tf-test-cloudfunctions2-function-bucket%{random_suffix}"
521+
location = "US"
522+
uniform_bucket_level_access = true
523+
}
524+
525+
resource "google_storage_bucket_object" "object" {
526+
name = "function-source.zip"
527+
bucket = google_storage_bucket.bucket.name
528+
source = "%{zip_path}"
529+
}
530+
531+
resource "google_cloudfunctions2_function" "terraform-test2" {
532+
name = "tf-test-test-function%{random_suffix}"
533+
location = "us-central1"
534+
description = "a new function"
535+
labels = {
536+
env = "test"
537+
}
538+
539+
build_config {
540+
runtime = "nodejs18"
541+
entry_point = "helloHttp"
542+
source {
543+
storage_source {
544+
bucket = google_storage_bucket.bucket.name
545+
object = google_storage_bucket_object.object.name
546+
generation = google_storage_bucket_object.object.generation
547+
}
548+
}
549+
}
550+
551+
service_config {
552+
max_instance_count = 1
553+
available_memory = "1536Mi"
554+
timeout_seconds = 30
555+
}
556+
}
557+
558+
output "object_gen_eq_storage_source_gen" {
559+
value = google_storage_bucket_object.object.generation == google_cloudfunctions2_function.terraform-test2.build_config.0.source.0.storage_source.0.generation
560+
}
561+
`, context)
562+
}

0 commit comments

Comments
 (0)