Skip to content

Commit

Permalink
Merge pull request #22299 from hashicorp/jbardin/state-mv
Browse files Browse the repository at this point in the history
allow moving resource to new modules not in state
  • Loading branch information
jbardin authored Aug 2, 2019
2 parents 88197bf + 32f7f58 commit 3cda2bd
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 10 deletions.
14 changes: 4 additions & 10 deletions command/state_mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,8 @@ func (c *StateMvCommand) Run(args []string) int {
}
diags = diags.Append(c.validateResourceMove(addrFrom, addrTo))
if stateTo.Module(addrTo.Module) == nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
msgInvalidTarget,
fmt.Sprintf("Cannot move to %s: %s does not exist in the current state.", addrTo, addrTo.Module),
))
// moving something to a mew module, so we need to ensure it exists
stateTo.EnsureModule(addrTo.Module)
}
if stateTo.Resource(addrTo) != nil {
diags = diags.Append(tfdiags.Sourceless(
Expand Down Expand Up @@ -251,11 +248,8 @@ func (c *StateMvCommand) Run(args []string) int {
diags = diags.Append(c.validateResourceMove(addrFrom.ContainingResource(), addrTo.ContainingResource()))

if stateTo.Module(addrTo.Module) == nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
msgInvalidTarget,
fmt.Sprintf("Cannot move to %s: %s does not exist in the current state.", addrTo, addrTo.Module),
))
// moving something to a mew module, so we need to ensure it exists
stateTo.EnsureModule(addrTo.Module)
}
if stateTo.ResourceInstance(addrTo) != nil {
diags = diags.Append(tfdiags.Sourceless(
Expand Down
92 changes: 92 additions & 0 deletions command/state_mv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,70 @@ func TestStateMv_stateOutNew_nestedModule(t *testing.T) {
testStateOutput(t, backups[0], testStateMvNestedModule_stateOutOriginal)
}

func TestStateMv_toNewModule(t *testing.T) {
state := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "bar",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"id":"bar","foo":"value","bar":"value"}`),
Status: states.ObjectReady,
},
addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance),
)
})

statePath := testStateFile(t, state)
stateOutPath1 := statePath + ".out1"
stateOutPath2 := statePath + ".out2"

p := testProvider()
ui := new(cli.MockUi)
c := &StateMvCommand{
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}

args := []string{
"-state", statePath,
"-state-out", stateOutPath1,
"test_instance.bar",
"module.bar.test_instance.bar",
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}

// Test it is correct
testStateOutput(t, stateOutPath1, testStateMvNewModule_stateOut)
testStateOutput(t, statePath, testStateMvNestedModule_stateOutSrc)

// Test we have backups
backups := testStateBackups(t, filepath.Dir(statePath))
if len(backups) != 1 {
t.Fatalf("bad: %#v", backups)
}
testStateOutput(t, backups[0], testStateMvNewModule_stateOutOriginal)

// now verify we can move the module itself
args = []string{
"-state", stateOutPath1,
"-state-out", stateOutPath2,
"module.bar",
"module.foo",
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
testStateOutput(t, stateOutPath2, testStateMvModuleNewModule_stateOut)
}
func TestStateMv_withinBackend(t *testing.T) {
td := tempDir(t)
copy.CopyDir(testFixturePath("backend-unchanged"), td)
Expand Down Expand Up @@ -1129,6 +1193,34 @@ module.bar.child2:
foo = value
`

const testStateMvNewModule_stateOut = `
<no state>
module.bar:
test_instance.bar:
ID = bar
provider = provider.test
bar = value
foo = value
`

const testStateMvModuleNewModule_stateOut = `
<no state>
module.foo:
test_instance.bar:
ID = bar
provider = provider.test
bar = value
foo = value
`

const testStateMvNewModule_stateOutOriginal = `
test_instance.bar:
ID = bar
provider = provider.test
bar = value
foo = value
`

const testStateMvNestedModule_stateOutSrc = `
<no state>
`
Expand Down

0 comments on commit 3cda2bd

Please sign in to comment.