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

remove computed keys from json #5595

Closed
wants to merge 2 commits into from
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 @@ -10,9 +10,35 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func monitoringDashboardDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
computedFields := []string{"etag", "name"}
// This recursive function takes an old map and a new map and is intended to remove the computed keys
// from the old json string (stored in state) so that it doesn't show a diff if it's not defined in the
// new map's json string (defined in config)
func removeComputedKeys(old map[string]interface{}, new map[string]interface{}) map[string]interface{} {
for k, v := range old {
if _, ok := old[k]; ok && new[k] == nil {
delete(old, k)
continue
}

if reflect.ValueOf(v).Kind() == reflect.Map {
old[k] = removeComputedKeys(v.(map[string]interface{}), new[k].(map[string]interface{}))
continue
}

if reflect.ValueOf(v).Kind() == reflect.Slice {
for i, j := range v.([]interface{}) {
if reflect.ValueOf(j).Kind() == reflect.Map {
old[k].([]interface{})[i] = removeComputedKeys(j.(map[string]interface{}), new[k].([]interface{})[i].(map[string]interface{}))
}
}
continue
}
}

return old
}

func monitoringDashboardDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
oldMap, err := structure.ExpandJsonFromString(old)
if err != nil {
return false
Expand All @@ -23,11 +49,7 @@ func monitoringDashboardDiffSuppress(k, old, new string, d *schema.ResourceData)
return false
}

for _, f := range computedFields {
delete(oldMap, f)
delete(newMap, f)
}

oldMap = removeComputedKeys(oldMap, newMap)
return reflect.DeepEqual(oldMap, newMap)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ The following arguments are supported:
The JSON representation of a dashboard, following the format at https://cloud.google.com/monitoring/api/ref_v3/rest/v1/projects.dashboards.
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)

~> **Warning:** Because this is represented as a JSON string, Terraform doesn't have underlying information to know which fields in the string have defaults. Thus, if the string returned from the API contains a default value that is not defined in the configuration, Terraform will produce a diff. This is not ideal and would cause a permanent diff every time the plan is run. Therefore, we have decided to suppress any diffs where the value is returned in the JSON string, but doesn't exist in the configuration. Consequently, you will not see an expected diff if a field is removed from the JSON string, but was already defined in state.

- - -


Expand Down