terraform: Module copy copies outputs and dependencies

This commit is contained in:
Mitchell Hashimoto 2016-04-11 18:05:55 -07:00
parent 30cf550fc5
commit af3c3e4c60
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 65 additions and 6 deletions

View File

@ -673,11 +673,13 @@ func (m *ModuleState) deepcopy() *ModuleState {
return nil return nil
} }
n := &ModuleState{ n := &ModuleState{
Path: make([]string, len(m.Path)), Path: make([]string, len(m.Path)),
Outputs: make(map[string]interface{}, len(m.Outputs)), Outputs: make(map[string]interface{}, len(m.Outputs)),
Resources: make(map[string]*ResourceState, len(m.Resources)), Resources: make(map[string]*ResourceState, len(m.Resources)),
Dependencies: make([]string, len(m.Dependencies)),
} }
copy(n.Path, m.Path) copy(n.Path, m.Path)
copy(n.Dependencies, m.Dependencies)
for k, v := range m.Outputs { for k, v := range m.Outputs {
n.Outputs[k] = v n.Outputs[k] = v
} }

View File

@ -59,7 +59,7 @@ func (s *State) Add(addrRaw string, raw interface{}) error {
} }
func stateAddFunc_Module_Module(s *State, addr *ResourceAddress, raw interface{}) error { func stateAddFunc_Module_Module(s *State, addr *ResourceAddress, raw interface{}) error {
src := raw.(*ModuleState) src := raw.(*ModuleState).deepcopy()
// If the target module exists, it is an error // If the target module exists, it is an error
path := append([]string{"root"}, addr.Path...) path := append([]string{"root"}, addr.Path...)
@ -67,8 +67,10 @@ func stateAddFunc_Module_Module(s *State, addr *ResourceAddress, raw interface{}
return fmt.Errorf("module target is not empty: %s", addr) return fmt.Errorf("module target is not empty: %s", addr)
} }
// TODO: outputs // Create it and copy our outputs and dependencies
// TODO: dependencies mod := s.AddModule(path)
mod.Outputs = src.Outputs
mod.Dependencies = src.Dependencies
// Go through the resources perform an add for each of those // Go through the resources perform an add for each of those
for k, v := range src.Resources { for k, v := range src.Resources {

View File

@ -478,6 +478,61 @@ func TestStateAdd(t *testing.T) {
}, },
}, },
"ModuleState w/ outputs and deps => Module Addr (new)": {
false,
"module.foo",
&ModuleState{
Path: rootModulePath,
Outputs: map[string]interface{}{
"foo": "bar",
},
Dependencies: []string{"foo"},
Resources: map[string]*ResourceState{
"test_instance.foo": &ResourceState{
Type: "test_instance",
Primary: &InstanceState{
ID: "foo",
},
},
"test_instance.bar": &ResourceState{
Type: "test_instance",
Primary: &InstanceState{
ID: "foo",
},
},
},
},
&State{},
&State{
Modules: []*ModuleState{
&ModuleState{
Path: []string{"root", "foo"},
Outputs: map[string]interface{}{
"foo": "bar",
},
Dependencies: []string{"foo"},
Resources: map[string]*ResourceState{
"test_instance.foo": &ResourceState{
Type: "test_instance",
Primary: &InstanceState{
ID: "foo",
},
},
"test_instance.bar": &ResourceState{
Type: "test_instance",
Primary: &InstanceState{
ID: "foo",
},
},
},
},
},
},
},
"ModuleState => Module Addr (existing)": { "ModuleState => Module Addr (existing)": {
true, true,
"module.foo", "module.foo",