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
42.35k stars 9.49k forks source link

Config generation should use `jsonencode()` when generating list elements and fields of complex types, not only for root-level arguments #35482

Open cysp opened 2 months ago

cysp commented 2 months ago

Terraform Version

1.9.2

Use Cases

I'm trying to generate config for an imported resource where some of its arguments are complex types that themselves contain JSON-encoded fields.

e.g. a resource has an argument represented as a list of JSON-encoded strings, which gets generated as a list of strings where each element is represented as an escaped JSON string (argument = ["{\"foo\":\"bar\"}])

Attempted Solutions

Since this is a provider I am developing myself, I did try changing the argument modelling to be a single JSON value but it feels like a worse representation and results in impedance mismatch with the API's SDK.

Proposal

The current config generation implementation opportunistically uses jsonencode() for root-level arguments if their values turn out to be valid JSON, perhaps this heuristic could be applied to all string values? In this case the argument fields make use of jsontypes, perhaps that could be used as a hint when generating configuration?

References

No response

crw commented 2 months ago

Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional use case descriptions. Thanks again!