Skip to content

Commit 13c5f1a

Browse files
5.0.0 - remove computed keys from JSON in monitoring dashboard (#9065) (#16014)
Signed-off-by: Modular Magician <[email protected]>
1 parent 24afffd commit 13c5f1a

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

.changelog/9065.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:breaking-change
2+
monitoring: fixed perma-diffs in `google_monitoring_dashboard.dashboard_json` by suppressing values returned by the API that are not in configuration
3+
```

google/services/monitoring/resource_monitoring_dashboard.go

+29-8
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,45 @@ import (
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1717
)
1818

19-
func monitoringDashboardDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
20-
computedFields := []string{"etag", "name"}
19+
// This recursive function takes an old map and a new map and is intended to remove the computed keys
20+
// from the old json string (stored in state) so that it doesn't show a diff if it's not defined in the
21+
// new map's json string (defined in config)
22+
func removeComputedKeys(old map[string]interface{}, new map[string]interface{}) map[string]interface{} {
23+
for k, v := range old {
24+
if _, ok := old[k]; ok && new[k] == nil {
25+
delete(old, k)
26+
continue
27+
}
28+
29+
if reflect.ValueOf(v).Kind() == reflect.Map {
30+
old[k] = removeComputedKeys(v.(map[string]interface{}), new[k].(map[string]interface{}))
31+
continue
32+
}
33+
34+
if reflect.ValueOf(v).Kind() == reflect.Slice {
35+
for i, j := range v.([]interface{}) {
36+
if reflect.ValueOf(j).Kind() == reflect.Map {
37+
old[k].([]interface{})[i] = removeComputedKeys(j.(map[string]interface{}), new[k].([]interface{})[i].(map[string]interface{}))
38+
}
39+
}
40+
continue
41+
}
42+
}
43+
44+
return old
45+
}
2146

47+
func monitoringDashboardDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
2248
oldMap, err := structure.ExpandJsonFromString(old)
2349
if err != nil {
2450
return false
2551
}
26-
2752
newMap, err := structure.ExpandJsonFromString(new)
2853
if err != nil {
2954
return false
3055
}
3156

32-
for _, f := range computedFields {
33-
delete(oldMap, f)
34-
delete(newMap, f)
35-
}
36-
57+
oldMap = removeComputedKeys(oldMap, newMap)
3758
return reflect.DeepEqual(oldMap, newMap)
3859
}
3960

google/services/monitoring/resource_monitoring_dashboard_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ func TestAccMonitoringDashboard_rowLayout(t *testing.T) {
8585
}
8686

8787
func TestAccMonitoringDashboard_update(t *testing.T) {
88-
// TODO: Fix requires a breaking change https://github.com/hashicorp/terraform-provider-google/issues/9976
89-
t.Skip()
9088
t.Parallel()
9189

9290
acctest.VcrTest(t, resource.TestCase{

website/docs/r/monitoring_dashboard.html.markdown

+6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ The following arguments are supported:
114114
The JSON representation of a dashboard, following the format at https://cloud.google.com/monitoring/api/ref_v3/rest/v1/projects.dashboards.
115115
The representation of an existing dashboard can be found by using the [API Explorer](https://cloud.google.com/monitoring/api/ref_v3/rest/v1/projects.dashboards/get)
116116

117+
~> **Warning:** Because this is represented as a JSON string, Terraform doesn't have underlying information to know
118+
which fields in the string have defaults. To prevent permanent diffs from default values, Terraform will attempt to
119+
suppress diffs where the value is returned in the JSON string but doesn't exist in the configuration. Consequently,
120+
legitmate remove-only diffs will also be suppressed. For Terraform to detect the diff, key removals must also be
121+
accompanied by a non-removal change (trivial or not).
122+
117123
- - -
118124

119125

0 commit comments

Comments
 (0)