Skip to content

Commit

Permalink
Show diff for computed nested labels fields when creating resources (#…
Browse files Browse the repository at this point in the history
…10401) (#17815)

[upstream:c1e30ed89595203b0b2bf597b7f1cd597eed17e5]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Apr 10, 2024
1 parent 752f66f commit 450b311
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/10401.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cloudrun: fixed the bug that computed `metadata.0.labels` and `metadata.0.annotations` fields don't appear in terraform plan when creating resource `google_cloud_run_service` and `google_cloud_run_domain_mapping`
```
70 changes: 70 additions & 0 deletions google/services/cloudrun/resource_cloud_run_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,76 @@ func TestAccCloudRunServiceMigration_withLabels(t *testing.T) {
})
}

func TestAccCloudRunService_withComputedLabels(t *testing.T) {
// Skip it in VCR test because of the randomness of uuid in "labels" field
// which causes the replaying mode after recording mode failing in VCR test
acctest.SkipIfVcr(t)
t.Parallel()

name := "tftest-cloudrun-" + acctest.RandString(t, 6)
project := envvar.GetTestProjectFromEnv()

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ExternalProviders: map[string]resource.ExternalProvider{
"random": {},
},
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccCloudRunService_withComputedLabels(name, project, "10", "600"),
},
{
ResourceName: "google_cloud_run_service.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"metadata.0.resource_version", "metadata.0.annotations", "metadata.0.labels", "metadata.0.terraform_labels", "status.0.conditions"},
},
},
})
}

func testAccCloudRunService_withComputedLabels(name, project, concurrency, timeoutSeconds string) string {
return fmt.Sprintf(`
resource "random_uuid" "test" {
}
resource "google_cloud_run_service" "default" {
name = "%s"
location = "us-central1"
metadata {
namespace = "%s"
annotations = {
env = "${random_uuid.test.result}"
}
labels = {
key1 = "${random_uuid.test.result}"
}
}
template {
spec {
containers {
image = "gcr.io/cloudrun/hello"
ports {
container_port = 8080
}
}
container_concurrency = %s
timeout_seconds = %s
}
}
traffic {
percent = 100
latest_revision = true
tag = "magic-module"
}
}
`, name, project, concurrency, timeoutSeconds)
}

func testAccCloudRunService_withProviderDefaultLabels(context map[string]interface{}) string {
return acctest.Nprintf(`
provider "google" {
Expand Down
10 changes: 10 additions & 0 deletions google/tpgresource/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ func SetMetadataAnnotationsDiff(_ context.Context, d *schema.ResourceDiff, meta
return nil
}

// Fix the bug that the computed and nested "annotations" field disappears from the terraform plan.
// https://github.com/hashicorp/terraform-provider-google/issues/17756
// The bug is introduced by SetNew on "metadata" field with the object including "effective_annotations".
// "effective_annotations" cannot be set directly due to a bug that SetNew doesn't work on nested fields
// in terraform sdk.
// https://github.com/hashicorp/terraform-plugin-sdk/issues/459
if !d.GetRawPlan().GetAttr("metadata").AsValueSlice()[0].GetAttr("annotations").IsWhollyKnown() {
return nil
}

raw := d.Get("metadata.0.annotations")
if raw == nil {
return nil
Expand Down
10 changes: 10 additions & 0 deletions google/tpgresource/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ func SetMetadataLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta inter
return nil
}

// Fix the bug that the computed and nested "labels" field disappears from the terraform plan.
// https://github.com/hashicorp/terraform-provider-google/issues/17756
// The bug is introduced by SetNew on "metadata" field with the object including terraform_labels and effective_labels.
// "terraform_labels" and "effective_labels" cannot be set directly due to a bug that SetNew doesn't work on nested fields
// in terraform sdk.
// https://github.com/hashicorp/terraform-plugin-sdk/issues/459
if !d.GetRawPlan().GetAttr("metadata").AsValueSlice()[0].GetAttr("labels").IsWhollyKnown() {
return nil
}

raw := d.Get("metadata.0.labels")
if raw == nil {
return nil
Expand Down

0 comments on commit 450b311

Please sign in to comment.