Add attribute mv and replace commands #111
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This pull request adds
attribute mv
andattribute replace
commands.Why
When working on dependency upgrades, I often come across warnings such as "A is deprecated. Use B instead". The current functionality only allows removing the old attribute and appending a new one to the end, which changes the order of the attributes and loses valuable comments. It would be great if we could rename an attribute.
For example, Terraform v1.10 introduced DynamoDB-free S3-native state locking in the s3 backend, and the upcoming Terraform v1.11 will start deprecating the old
dynamodb_table
attribute and recommend using the newuse_lockfile
. hashicorp/terraform#36257It's easy to rewrite a few files by hand, but it could be tedious if you have hundreds of backend configurations across many repositories.
What
This pull request adds
attribute mv
andattribute replace
commands to help such an upgrade work.Note: As for implementation, hclwrite's attribute name is currently a private variable with no API for renaming it. Therefore, a forked version of hclwrite is used to implement this feature. So, we need to wait for the upstream patch to be merged before we merge this implementation.The upstream patch can be found at hashicorp/hcl#506. (Edit: The upstream patch has been merged 🎉 )Regarding the design, we have aligned the
attribute mv
command withblock mv
. It acceptsfrom
andto
addresses so that it can be extended in the future, but we have not implemented moving an attribute across blocks yet.In addition to the
attribute mv
command, this PR also adds a newattribute replace
command. This command not only renames the key of an attribute but also sets the value of the attribute. This could be done by combiningattribute mv
andattribute set
, but doing it with a single command would be more convenient. This is because in dependency upgrade scenarios, where attribute A is deprecated and use B instead, it is often the case that not only the name of the attribute changes but also its value.Examples
An example of rewriting s3 backend
dynamodb_table
touse_lockfile
in Terraform v1.11 upgrade:You can combine it with find, xargs, etc., and rewrite multiple files at once:
While my initial motivation was the above, I'm sure there are many more real-world use cases, not just this one.