diff --git a/terraform/state.go b/terraform/state.go index dea8fe961..ccb4b5737 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -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) diff --git a/terraform/state_test.go b/terraform/state_test.go index 7a5554651..f7889dc6d 100644 --- a/terraform/state_test.go +++ b/terraform/state_test.go @@ -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", diff --git a/terraform/test-fixtures/state-module-orphans/bar/main.tf b/terraform/test-fixtures/state-module-orphans/bar/main.tf new file mode 100644 index 000000000..c01ade299 --- /dev/null +++ b/terraform/test-fixtures/state-module-orphans/bar/main.tf @@ -0,0 +1 @@ +# Nothing diff --git a/terraform/test-fixtures/state-module-orphans/main.tf b/terraform/test-fixtures/state-module-orphans/main.tf new file mode 100644 index 000000000..f009f1924 --- /dev/null +++ b/terraform/test-fixtures/state-module-orphans/main.tf @@ -0,0 +1,3 @@ +module "bar" { + source = "./bar" +}