mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 17:01:04 -06:00
terraform: State.ModuleOrphans
This commit is contained in:
parent
cb4e364aca
commit
e08dc05f54
@ -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)
|
||||
|
@ -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",
|
||||
|
1
terraform/test-fixtures/state-module-orphans/bar/main.tf
Normal file
1
terraform/test-fixtures/state-module-orphans/bar/main.tf
Normal file
@ -0,0 +1 @@
|
||||
# Nothing
|
3
terraform/test-fixtures/state-module-orphans/main.tf
Normal file
3
terraform/test-fixtures/state-module-orphans/main.tf
Normal file
@ -0,0 +1,3 @@
|
||||
module "bar" {
|
||||
source = "./bar"
|
||||
}
|
Loading…
Reference in New Issue
Block a user