From 0c83832751f6f4be7b435d9764ab7cc9ac9d5e76 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Wed, 21 Apr 2021 18:29:04 +0000 Subject: [PATCH] Updated labels for cloud run domain mapping diff suppress (#4720) Signed-off-by: Modular Magician --- .changelog/4720.txt | 3 + google/resource_cloud_run_domain_mapping.go | 13 +++-- .../resource_cloud_run_domain_mapping_test.go | 55 +++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 .changelog/4720.txt diff --git a/.changelog/4720.txt b/.changelog/4720.txt new file mode 100644 index 00000000000..aa6c9bf1ee0 --- /dev/null +++ b/.changelog/4720.txt @@ -0,0 +1,3 @@ +```release-note:bug +cloudrun: fixed permadiff on `google_cloud_run_domain_mapping.metadata.labels` +``` diff --git a/google/resource_cloud_run_domain_mapping.go b/google/resource_cloud_run_domain_mapping.go index 3515f261805..7b7ab40e847 100644 --- a/google/resource_cloud_run_domain_mapping.go +++ b/google/resource_cloud_run_domain_mapping.go @@ -27,12 +27,17 @@ import ( "google.golang.org/api/googleapi" ) -const domainMappingGoogleProvidedLabel = "cloud.googleapis.com/location" +var domainMappingGoogleProvidedLabels = []string{ + "cloud.googleapis.com/location", + "run.googleapis.com/overrideAt", +} func domainMappingLabelDiffSuppress(k, old, new string, d *schema.ResourceData) bool { - // Suppress diffs for the label provided by Google - if strings.Contains(k, domainMappingGoogleProvidedLabel) && new == "" { - return true + // Suppress diffs for the labels provided by Google + for _, label := range domainMappingGoogleProvidedLabels { + if strings.Contains(k, label) && new == "" { + return true + } } // Let diff be determined by labels (above) diff --git a/google/resource_cloud_run_domain_mapping_test.go b/google/resource_cloud_run_domain_mapping_test.go index 851f83d6786..cb46e4d7905 100644 --- a/google/resource_cloud_run_domain_mapping_test.go +++ b/google/resource_cloud_run_domain_mapping_test.go @@ -6,6 +6,61 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) +func TestDomainMappingLabelDiffSuppress(t *testing.T) { + cases := map[string]struct { + K, Old, New string + ExpectDiffSuppress bool + }{ + "missing run.googleapis.com/overrideAt": { + K: "metadata.0.labels.run.googleapis.com/overrideAt", + Old: "2021-04-20T22:38:23.584Z", + New: "", + ExpectDiffSuppress: true, + }, + "explicit run.googleapis.com/overrideAt": { + K: "metadata.0.labels.run.googleapis.com/overrideAt", + Old: "2021-04-20T22:38:23.584Z", + New: "2022-04-20T22:38:23.584Z", + ExpectDiffSuppress: false, + }, + "missing cloud.googleapis.com/location": { + K: "metadata.0.labels.cloud.googleapis.com/location", + Old: "us-central1", + New: "", + ExpectDiffSuppress: true, + }, + "explicit cloud.googleapis.com/location": { + K: "metadata.0.labels.cloud.googleapis.com/location", + Old: "us-central1", + New: "us-central2", + ExpectDiffSuppress: false, + }, + "labels.%": { + K: "metadata.0.labels.%", + Old: "3", + New: "1", + ExpectDiffSuppress: true, + }, + "deleted custom key": { + K: "metadata.0.labels.my-label", + Old: "my-value", + New: "", + ExpectDiffSuppress: false, + }, + "added custom key": { + K: "metadata.0.labels.my-label", + Old: "", + New: "my-value", + ExpectDiffSuppress: false, + }, + } + for tn, tc := range cases { + if domainMappingLabelDiffSuppress(tc.K, tc.Old, tc.New, nil) != tc.ExpectDiffSuppress { + t.Errorf("bad: %s, %q: %q => %q expect DiffSuppress to return %t", tn, tc.K, tc.Old, tc.New, tc.ExpectDiffSuppress) + } + } +} + // Destroy and recreate the mapping, testing that Terraform doesn't return a 409 func TestAccCloudRunDomainMapping_foregroundDeletion(t *testing.T) { t.Parallel()