mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 00:41:27 -06:00
terraform: module orphans
This commit is contained in:
parent
e08dc05f54
commit
922bdc92db
1
terraform/test-fixtures/transform-orphan-modules/main.tf
Normal file
1
terraform/test-fixtures/transform-orphan-modules/main.tf
Normal file
@ -0,0 +1 @@
|
||||
resource "aws_instance" "foo" {}
|
@ -24,25 +24,31 @@ func (t *OrphanTransformer) Transform(g *Graph) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get the orphans from our configuration. This will only get resources.
|
||||
orphans := state.Orphans(t.Config)
|
||||
if len(orphans) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Go over each orphan and add it to the graph.
|
||||
for _, k := range orphans {
|
||||
// Go over each resource orphan and add it to the graph.
|
||||
for _, k := range state.Orphans(t.Config) {
|
||||
g.ConnectTo(
|
||||
g.Add(&graphNodeOrphanResource{ResourceName: k}),
|
||||
state.Resources[k].Dependencies)
|
||||
}
|
||||
|
||||
// TODO: modules
|
||||
// Go over each module orphan and add it to the graph
|
||||
for _, path := range t.State.ModuleOrphans(g.Path, t.Config) {
|
||||
g.Add(&graphNodeOrphanModule{Path: path})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// graphNodeOrphan is the graph vertex representing an orphan resource..
|
||||
// graphNodeOrphanModule is the graph vertex representing an orphan resource..
|
||||
type graphNodeOrphanModule struct {
|
||||
Path []string
|
||||
}
|
||||
|
||||
func (n *graphNodeOrphanModule) Name() string {
|
||||
return fmt.Sprintf("module.%s (orphan)", n.Path[len(n.Path)-1])
|
||||
}
|
||||
|
||||
// graphNodeOrphanResource is the graph vertex representing an orphan resource..
|
||||
type graphNodeOrphanResource struct {
|
||||
ResourceName string
|
||||
}
|
||||
|
@ -51,6 +51,57 @@ func TestOrphanTransformer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrphanTransformer_modules(t *testing.T) {
|
||||
mod := testModule(t, "transform-orphan-modules")
|
||||
state := &State{
|
||||
Modules: []*ModuleState{
|
||||
&ModuleState{
|
||||
Path: RootModulePath,
|
||||
Resources: map[string]*ResourceState{
|
||||
"aws_instance.foo": &ResourceState{
|
||||
Type: "aws_instance",
|
||||
Primary: &InstanceState{
|
||||
ID: "foo",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
// Orphan module
|
||||
&ModuleState{
|
||||
Path: []string{RootModuleName, "foo"},
|
||||
Resources: map[string]*ResourceState{
|
||||
"aws_instance.web": &ResourceState{
|
||||
Type: "aws_instance",
|
||||
Primary: &InstanceState{
|
||||
ID: "foo",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
g := Graph{Path: RootModulePath}
|
||||
{
|
||||
tf := &ConfigTransformer{Module: mod}
|
||||
if err := tf.Transform(&g); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
transform := &OrphanTransformer{State: state, Config: mod.Config()}
|
||||
if err := transform.Transform(&g); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(g.String())
|
||||
expected := strings.TrimSpace(testTransformOrphanModulesStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad:\n\n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrphanTransformer_resourceDepends(t *testing.T) {
|
||||
mod := testModule(t, "transform-orphan-basic")
|
||||
state := &State{
|
||||
@ -105,6 +156,11 @@ aws_instance.db (orphan)
|
||||
aws_instance.web
|
||||
`
|
||||
|
||||
const testTransformOrphanModulesStr = `
|
||||
aws_instance.foo
|
||||
module.foo (orphan)
|
||||
`
|
||||
|
||||
const testTransformOrphanResourceDependsStr = `
|
||||
aws_instance.db (orphan)
|
||||
aws_instance.web
|
||||
|
Loading…
Reference in New Issue
Block a user