mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
terraform: the orphan transform uses the graph path
This commit is contained in:
parent
3820aea513
commit
4f8152c28a
@ -9,6 +9,9 @@ import (
|
|||||||
// RootModuleName is the name given to the root module implicitly.
|
// RootModuleName is the name given to the root module implicitly.
|
||||||
const RootModuleName = "root"
|
const RootModuleName = "root"
|
||||||
|
|
||||||
|
// RootModulePath is the path for the root module.
|
||||||
|
var RootModulePath = []string{RootModuleName}
|
||||||
|
|
||||||
// Graph represents the graph that Terraform uses to represent resources
|
// Graph represents the graph that Terraform uses to represent resources
|
||||||
// and their dependencies. Each graph represents only one module, but it
|
// and their dependencies. Each graph represents only one module, but it
|
||||||
// can contain further modules, which themselves have their own graph.
|
// can contain further modules, which themselves have their own graph.
|
||||||
|
@ -9,13 +9,23 @@ import (
|
|||||||
// OrphanTransformer is a GraphTransformer that adds orphans to the
|
// OrphanTransformer is a GraphTransformer that adds orphans to the
|
||||||
// graph. This transformer adds both resource and module orphans.
|
// graph. This transformer adds both resource and module orphans.
|
||||||
type OrphanTransformer struct {
|
type OrphanTransformer struct {
|
||||||
State *ModuleState
|
// State is the global state. We require the global state to
|
||||||
|
// properly find module orphans at our path.
|
||||||
|
State *State
|
||||||
|
|
||||||
|
// Config is just the configuration of our current module.
|
||||||
Config *config.Config
|
Config *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *OrphanTransformer) Transform(g *Graph) error {
|
func (t *OrphanTransformer) Transform(g *Graph) error {
|
||||||
|
state := t.State.ModuleByPath(g.Path)
|
||||||
|
if state == nil {
|
||||||
|
// If there is no state for our module, there can't be any orphans
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Get the orphans from our configuration. This will only get resources.
|
// Get the orphans from our configuration. This will only get resources.
|
||||||
orphans := t.State.Orphans(t.Config)
|
orphans := state.Orphans(t.Config)
|
||||||
if len(orphans) == 0 {
|
if len(orphans) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -24,7 +34,7 @@ func (t *OrphanTransformer) Transform(g *Graph) error {
|
|||||||
for _, k := range orphans {
|
for _, k := range orphans {
|
||||||
g.ConnectTo(
|
g.ConnectTo(
|
||||||
g.Add(&graphNodeOrphanResource{ResourceName: k}),
|
g.Add(&graphNodeOrphanResource{ResourceName: k}),
|
||||||
t.State.Resources[k].Dependencies)
|
state.Resources[k].Dependencies)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: modules
|
// TODO: modules
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package terraform
|
package terraform
|
||||||
|
|
||||||
/*
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -8,33 +7,40 @@ import (
|
|||||||
|
|
||||||
func TestOrphanTransformer(t *testing.T) {
|
func TestOrphanTransformer(t *testing.T) {
|
||||||
mod := testModule(t, "transform-orphan-basic")
|
mod := testModule(t, "transform-orphan-basic")
|
||||||
state := &ModuleState{
|
state := &State{
|
||||||
Path: rootModulePath,
|
Modules: []*ModuleState{
|
||||||
Resources: map[string]*ResourceState{
|
&ModuleState{
|
||||||
"aws_instance.web": &ResourceState{
|
Path: RootModulePath,
|
||||||
Type: "aws_instance",
|
Resources: map[string]*ResourceState{
|
||||||
Primary: &InstanceState{
|
"aws_instance.web": &ResourceState{
|
||||||
ID: "foo",
|
Type: "aws_instance",
|
||||||
},
|
Primary: &InstanceState{
|
||||||
},
|
ID: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
// The orphan
|
// The orphan
|
||||||
"aws_instance.db": &ResourceState{
|
"aws_instance.db": &ResourceState{
|
||||||
Type: "aws_instance",
|
Type: "aws_instance",
|
||||||
Primary: &InstanceState{
|
Primary: &InstanceState{
|
||||||
ID: "foo",
|
ID: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
g, err := Graph2(mod)
|
g := Graph{Path: RootModulePath}
|
||||||
if err != nil {
|
{
|
||||||
t.Fatalf("err: %s", err)
|
tf := &ConfigTransformer{Module: mod}
|
||||||
|
if err := tf.Transform(&g); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
transform := &OrphanTransformer{State: state, Config: mod.Config()}
|
transform := &OrphanTransformer{State: state, Config: mod.Config()}
|
||||||
if err := transform.Transform(g); err != nil {
|
if err := transform.Transform(&g); err != nil {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,36 +53,43 @@ func TestOrphanTransformer(t *testing.T) {
|
|||||||
|
|
||||||
func TestOrphanTransformer_resourceDepends(t *testing.T) {
|
func TestOrphanTransformer_resourceDepends(t *testing.T) {
|
||||||
mod := testModule(t, "transform-orphan-basic")
|
mod := testModule(t, "transform-orphan-basic")
|
||||||
state := &ModuleState{
|
state := &State{
|
||||||
Path: rootModulePath,
|
Modules: []*ModuleState{
|
||||||
Resources: map[string]*ResourceState{
|
&ModuleState{
|
||||||
"aws_instance.web": &ResourceState{
|
Path: RootModulePath,
|
||||||
Type: "aws_instance",
|
Resources: map[string]*ResourceState{
|
||||||
Primary: &InstanceState{
|
"aws_instance.web": &ResourceState{
|
||||||
ID: "foo",
|
Type: "aws_instance",
|
||||||
},
|
Primary: &InstanceState{
|
||||||
},
|
ID: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
// The orphan
|
// The orphan
|
||||||
"aws_instance.db": &ResourceState{
|
"aws_instance.db": &ResourceState{
|
||||||
Type: "aws_instance",
|
Type: "aws_instance",
|
||||||
Primary: &InstanceState{
|
Primary: &InstanceState{
|
||||||
ID: "foo",
|
ID: "foo",
|
||||||
},
|
},
|
||||||
Dependencies: []string{
|
Dependencies: []string{
|
||||||
"aws_instance.web",
|
"aws_instance.web",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
g, err := Graph2(mod)
|
g := Graph{Path: RootModulePath}
|
||||||
if err != nil {
|
{
|
||||||
t.Fatalf("err: %s", err)
|
tf := &ConfigTransformer{Module: mod}
|
||||||
|
if err := tf.Transform(&g); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
transform := &OrphanTransformer{State: state, Config: mod.Config()}
|
transform := &OrphanTransformer{State: state, Config: mod.Config()}
|
||||||
if err := transform.Transform(g); err != nil {
|
if err := transform.Transform(&g); err != nil {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,4 +110,3 @@ aws_instance.db (orphan)
|
|||||||
aws_instance.web
|
aws_instance.web
|
||||||
aws_instance.web
|
aws_instance.web
|
||||||
`
|
`
|
||||||
*/
|
|
||||||
|
Loading…
Reference in New Issue
Block a user