Print addresses in state mv errors

Using the addrTo after it has failed its check means <invalid>/no
address will be printed. Change this throughout, but particularly
add a test for the origin issue for this.
This commit is contained in:
Pam Selle 2021-01-12 12:59:28 -05:00
parent ae52190c01
commit 52c77ba33e
2 changed files with 70 additions and 3 deletions

View File

@ -139,7 +139,7 @@ func (c *StateMvCommand) Run(args []string) int {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
msgInvalidTarget,
fmt.Sprintf("Cannot move %s to %s: the target must also be a module.", addrFrom, addrTo),
fmt.Sprintf("Cannot move %s to %s: the target must also be a module.", addrFrom, destAddr),
))
c.showDiagnostics(diags)
return 1
@ -184,7 +184,7 @@ func (c *StateMvCommand) Run(args []string) int {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
msgInvalidTarget,
fmt.Sprintf("Cannot move %s to %s: the target must also be a whole resource.", addrFrom, addrTo),
fmt.Sprintf("Cannot move %s to %s: the target must also be a whole resource.", addrFrom, destAddr),
))
c.showDiagnostics(diags)
return 1
@ -231,7 +231,7 @@ func (c *StateMvCommand) Run(args []string) int {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
msgInvalidTarget,
fmt.Sprintf("Cannot move %s to %s: the target must also be a resource instance.", addrFrom, addrTo),
fmt.Sprintf("Cannot move %s to %s: the target must also be a resource instance.", addrFrom, destAddr),
))
c.showDiagnostics(diags)
return 1

View File

@ -7,6 +7,7 @@ import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mitchellh/cli"
"github.com/hashicorp/terraform/addrs"
@ -148,6 +149,7 @@ func TestStateMv(t *testing.T) {
}
func TestStateMv_resourceToInstance(t *testing.T) {
// A single resource (no count defined)
state := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
@ -236,6 +238,71 @@ test_instance.baz:
testStateOutput(t, backups[0], testStateMvOutputOriginal)
}
func TestStateMv_resourceToInstanceErr(t *testing.T) {
state := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.IntKey(0)).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
Status: states.ObjectReady,
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
s.SetResourceProvider(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "bar",
}.Absolute(addrs.RootModuleInstance),
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
statePath := testStateFile(t, state)
p := testProvider()
ui := new(cli.MockUi)
c := &StateMvCommand{
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
args := []string{
"-state", statePath,
"test_instance.foo",
"test_instance.bar[0]",
}
if code := c.Run(args); code == 0 {
t.Fatalf("expected error output, got:\n%s", ui.OutputWriter.String())
}
expectedErr := `
Error: Invalid target address
Cannot move test_instance.foo to test_instance.bar[0]: the target must also be
a whole resource.
`
errOutput := ui.ErrorWriter.String()
if errOutput != expectedErr {
t.Errorf("Unexpected diff.\ngot:\n%s\nwant:\n%s\n", errOutput, expectedErr)
t.Errorf("%s", cmp.Diff(errOutput, expectedErr))
}
}
func TestStateMv_instanceToResource(t *testing.T) {
state := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(