terraform: State.ModuleOrphans

This commit is contained in:
Mitchell Hashimoto 2015-01-27 10:13:50 -08:00
parent cb4e364aca
commit e08dc05f54
4 changed files with 53 additions and 0 deletions

View File

@ -100,6 +100,29 @@ func (s *State) ModuleByPath(path []string) *ModuleState {
return nil
}
// ModuleOrphans returns all the module orphans in this state by
// returning their full paths. These paths can be used with ModuleByPath
// to return the actual state.
func (s *State) ModuleOrphans(path []string, c *config.Config) [][]string {
childrenKeys := make(map[string]struct{})
for _, m := range c.Modules {
childrenKeys[m.Name] = struct{}{}
}
// Go over the direct children and find any that aren't in our
// keys.
var orphans [][]string
for _, m := range s.Children(path) {
if _, ok := childrenKeys[m.Path[len(m.Path)-1]]; ok {
continue
}
orphans = append(orphans, m.Path)
}
return orphans
}
// RootModule returns the ModuleState for the root module
func (s *State) RootModule() *ModuleState {
root := s.ModuleByPath(rootModulePath)

View File

@ -59,6 +59,32 @@ func TestStateAddModule(t *testing.T) {
}
}
func TestStateModuleOrphans(t *testing.T) {
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: RootModulePath,
},
&ModuleState{
Path: []string{RootModuleName, "foo"},
},
&ModuleState{
Path: []string{RootModuleName, "bar"},
},
},
}
config := testModule(t, "state-module-orphans").Config()
actual := state.ModuleOrphans(RootModulePath, config)
expected := [][]string{
[]string{RootModuleName, "foo"},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestInstanceState_MergeDiff(t *testing.T) {
is := InstanceState{
ID: "foo",

View File

@ -0,0 +1 @@
# Nothing

View File

@ -0,0 +1,3 @@
module "bar" {
source = "./bar"
}