opentofu/internal/terraform/transform_removed_modules.go
Martin Atkins 36d0a50427 Move terraform/ to internal/terraform/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

45 lines
1.0 KiB
Go

package terraform
import (
"log"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/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
}