Skip to content

Commit

Permalink
Updated labels for cloud run domain mapping diff suppress (#4720) (#8971
Browse files Browse the repository at this point in the history
)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Apr 21, 2021
1 parent 77262cb commit 11935e3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/4720.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cloudrun: fixed permadiff on `google_cloud_run_domain_mapping.metadata.labels`
```
13 changes: 9 additions & 4 deletions google/resource_cloud_run_domain_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions google/resource_cloud_run_domain_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 11935e3

Please sign in to comment.