terraform: fix some test failures on state add with multiple modules

This commit is contained in:
Mitchell Hashimoto 2016-08-18 17:39:07 -04:00
parent 3892cc4e91
commit 3b3f92cd9b
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 30 additions and 3 deletions

View File

@ -79,13 +79,16 @@ func (c *StateMvCommand) Run(args []string) int {
return 1 return 1
} }
// Get the item to add to the state
add := c.addableResult(results)
// Do the actual move // Do the actual move
if err := stateFromReal.Remove(args[0]); err != nil { if err := stateFromReal.Remove(args[0]); err != nil {
c.Ui.Error(fmt.Sprintf(errStateMv, err)) c.Ui.Error(fmt.Sprintf(errStateMv, err))
return 1 return 1
} }
if err := stateToReal.Add(args[0], args[1], results[0].Value); err != nil { if err := stateToReal.Add(args[0], args[1], add); err != nil {
c.Ui.Error(fmt.Sprintf(errStateMv, err)) c.Ui.Error(fmt.Sprintf(errStateMv, err))
return 1 return 1
} }
@ -119,6 +122,29 @@ func (c *StateMvCommand) Run(args []string) int {
return 0 return 0
} }
// addableResult takes the result from a filter operation and returns what to
// call State.Add with. The reason we do this is beacuse in the module case
// we must add the list of all modules returned versus just the root module.
func (c *StateMvCommand) addableResult(results []*terraform.StateFilterResult) interface{} {
switch v := results[0].Value.(type) {
case *terraform.ModuleState:
// If a module state then we should add the full list of modules
result := []*terraform.ModuleState{v}
if len(results) > 1 {
for _, r := range results[1:] {
if ms, ok := r.Value.(*terraform.ModuleState); ok {
result = append(result, ms)
}
}
}
return result
default:
// By default just add the first result
return v
}
}
func (c *StateMvCommand) Help() string { func (c *StateMvCommand) Help() string {
helpText := ` helpText := `
Usage: terraform state mv [options] ADDRESS ADDRESS Usage: terraform state mv [options] ADDRESS ADDRESS

View File

@ -327,6 +327,7 @@ test_instance.baz:
` `
const testStateMvNestedModule_stateOut = ` const testStateMvNestedModule_stateOut = `
<no state>
module.bar: module.bar:
<no state> <no state>
module.bar.child1: module.bar.child1:

View File

@ -129,7 +129,7 @@ func stateAddFunc_Module_Module(s *State, fromAddr, addr *ResourceAddress, raw i
} }
// It is! Strip the leading prefix and attach that to our address // It is! Strip the leading prefix and attach that to our address
extra := item.Path[len(src.Path)+1:] extra := item.Path[len(src.Path):]
addrCopy := addr.Copy() addrCopy := addr.Copy()
addrCopy.Path = append(addrCopy.Path, extra...) addrCopy.Path = append(addrCopy.Path, extra...)

View File

@ -201,7 +201,7 @@ func TestStateAdd(t *testing.T) {
[]*ModuleState{ []*ModuleState{
&ModuleState{ &ModuleState{
Path: rootModulePath, Path: []string{"root", "foo"},
Resources: map[string]*ResourceState{}, Resources: map[string]*ResourceState{},
}, },