hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
41.74k stars 9.43k forks source link

More intelligent JSON diffing capabailities #33471

Open tmccombs opened 1 year ago

tmccombs commented 1 year ago

Terraform Version

Terraform v1.5.0
on linux_amd64
+ provider registry.terraform.io/akamai/akamai v4.1.0
+ provider registry.terraform.io/hashicorp/tfe v0.45.0

Your version of Terraform is out of date! The latest version
is 1.5.2. You can update by downloading from https://www.terraform.io/downloads.html

Use Cases

Some providers take complicate json policy documents as inputs. Terraform will try to detect if strings are JSON, and if so, use a use a JSON diffing algorithm. Often, this works great, however sometimes relatively small changes in the JSON can result in massive diffs. I haven't (yet) been able to determine exactly what scenarios cause this, but I recently ran into a case where a dozen lines of changes, all at the leaf level of deeply nested object, out of about a thousand lines of json resulted in basically everything in the document being marked as replaced, and added.

This makes reviewing the diffs for such changes very difficult.

Attempted Solutions

It is possible to work around this by getting the json output of a plan (for example using terraform show -json or the API for terraform cloud or enterprise), extracting the before and after values (using jq or similar), and then using another json diffing tool (for example https://www.npmjs.com/package/json-diff) to obtain a more useful diff.

Proposal

Improve the json diffing formatter to show more useful diffs for changes to complicated JSON documents.

Possibly make use of an existing go library for json diffs.

References

No response

kmoe commented 1 year ago

Thanks, sounds sensible. Hopefully we'll find an example of this sooner or later so we can implement an appropriate fix.