mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 17:01:04 -06:00
c638252210
Create a single transformer to remove all unused nodes from the apply graph. This is similar to the combination of the resource pruning done in the destroy edge transformer, and the unused values transformer. In addition to resources, variables, locals, and outputs, we now need to remove unused module expansion nodes as well. Since these can all be interdependent, we need to process them as whole in a single transformation.
45 lines
1.0 KiB
Go
45 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,
|
|
}
|
|
g.Add(closer)
|
|
}
|
|
|
|
return nil
|
|
}
|