mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-28 17:34:24 -06:00
700e20de5d
NodeModuleRemoved is redundant now with the concept of nodeCloseModule, so we can replace it within the graph. This does mean that nodeCloseModule needs to know if it's evaluating an orphaned module that can't be expanded, but the overhead to checking this isn't too bad. Now that nodeModuleClose is referenceable, and we can ensure it's always in the graph at the correct time, we can eliminate the need to connect each resource to every single node within a module it references, and instead connect only to the nodeModuleClose, which acts as the module root. Since module expansion can cause exponential growth in the number of edges in graphs, this will help with performance problems when transforming and reducing these graphs by eliminating the bulk of redundant edges. This will also help with general debugging, making the graphs easier to read.
46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package terraform
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/hashicorp/terraform/configs"
|
|
"github.com/hashicorp/terraform/states"
|
|
)
|
|
|
|
// RemovedModuleTransformer implements GraphTransformer to add nodes indicating
|
|
// when a module was removed from the configuration.
|
|
type RemovedModuleTransformer struct {
|
|
Config *configs.Config // root node in the config tree
|
|
State *states.State
|
|
}
|
|
|
|
func (t *RemovedModuleTransformer) Transform(g *Graph) error {
|
|
// nothing to remove if there's no state!
|
|
if t.State == nil {
|
|
return nil
|
|
}
|
|
|
|
removed := map[string]addrs.Module{}
|
|
|
|
for _, m := range t.State.Modules {
|
|
cc := t.Config.DescendentForInstance(m.Addr)
|
|
if cc != nil {
|
|
continue
|
|
}
|
|
removed[m.Addr.Module().String()] = m.Addr.Module()
|
|
log.Printf("[DEBUG] %s is no longer in configuration\n", m.Addr)
|
|
}
|
|
|
|
// add closers to collect any module instances we're removing
|
|
for _, modAddr := range removed {
|
|
closer := &nodeCloseModule{
|
|
Addr: modAddr,
|
|
orphaned: true,
|
|
}
|
|
g.Add(closer)
|
|
}
|
|
|
|
return nil
|
|
}
|