Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show diff for computed nested labels fields #10393

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,73 @@ func TestAccCloudRunServiceMigration_withLabels(t *testing.T) {
})
}

func TestAccCloudRunService_withComputedLabels(t *testing.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 mmv1/third_party/terraform/tpgresource/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 introducted by SetNew on "metadata" field with the object including "effective_annotations".
// "effective_annotations" cannot be set directrly 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 mmv1/third_party/terraform/tpgresource/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,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 introducted by SetNew on "metadata" field with the object including terraform_labels and effective_labels.
// "terraform_labels" and "effective_labels" cannot be set directrly 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
Loading