Merge pull request #27482 from hashicorp/pselle/state_mv

Make errors in state mv more accurate
This commit is contained in:
Pam Selle 2021-01-13 16:43:19 -05:00 committed by GitHub
commit e6d02aec16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 3 deletions

View File

@ -139,7 +139,7 @@ func (c *StateMvCommand) Run(args []string) int {
diags = diags.Append(tfdiags.Sourceless( diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error, tfdiags.Error,
msgInvalidTarget, 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) c.showDiagnostics(diags)
return 1 return 1
@ -184,7 +184,7 @@ func (c *StateMvCommand) Run(args []string) int {
diags = diags.Append(tfdiags.Sourceless( diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error, tfdiags.Error,
msgInvalidTarget, 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 source is a whole resource (not a resource instance) so the target must also be a whole resource.", addrFrom, destAddr),
)) ))
c.showDiagnostics(diags) c.showDiagnostics(diags)
return 1 return 1
@ -231,7 +231,7 @@ func (c *StateMvCommand) Run(args []string) int {
diags = diags.Append(tfdiags.Sourceless( diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error, tfdiags.Error,
msgInvalidTarget, 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) c.showDiagnostics(diags)
return 1 return 1

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/google/go-cmp/cmp"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/addrs"
@ -148,6 +149,7 @@ func TestStateMv(t *testing.T) {
} }
func TestStateMv_resourceToInstance(t *testing.T) { func TestStateMv_resourceToInstance(t *testing.T) {
// A single resource (no count defined)
state := states.BuildState(func(s *states.SyncState) { state := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent( s.SetResourceInstanceCurrent(
addrs.Resource{ addrs.Resource{
@ -236,6 +238,72 @@ test_instance.baz:
testStateOutput(t, backups[0], testStateMvOutputOriginal) 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 source is a whole
resource (not a resource instance) so 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) { func TestStateMv_instanceToResource(t *testing.T) {
state := states.BuildState(func(s *states.SyncState) { state := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent( s.SetResourceInstanceCurrent(