command: Fix various issues in the "terraform state ..." subcommands #20719
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.
In earlier refactoring we updated these commands to support the new address and state types, but attempted to partially retain the old-style
StateFilter
abstraction that originally lived in the Terraform package, even though that was no longer being used for any other functionality.Unfortunately the adaptation of the existing filtering to the new types wasn't exact and so these commands ended up having a few bugs that were not covered by the existing tests.
Since the old
StateFilter
behavior was the source of various misbehavior anyway, here it's removed altogether and replaced with some simpler functions in thestate_meta.go
file that are tailored to the use-cases of these sub-commands.As well as just generally behaving more consistently with the other parts of Terraform that use the new resource address types, this commit fixes the following bugs:
A resource address of
aws_instance.foo
would previously match an resource of that type and name in any module, which disagreed with the expected interpretation elsewhere of meaning a single resource in the root module. (This was actually a pre-existing bug in 0.11 too; fixesterraform state rm foo.bar
also removesmodule.baz.foo.bar
#19692)The
terraform state mv
command was not supporting moves from a single resource address to an indexed address and vice-versa, because the old logic didn't need to make that distinction while they are two separate address types in the new logic. Now we allow resources that do not havecount
/for_each
to be treated as if they are instances for the purposes of this command, which is a better match for likely user intent and for the old behavior.terraform state mv
was not fully protecting against moving between resource types or resource modes. (Fixes Moving a resource drops the "data." prefix from data source #18978, and probably several others.)Finally, we also clean up a little some of the usage output from these commands, which hasn't been updated for some time and so had both some stale information and some inaccurate terminology.