mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-26 17:01:04 -06:00
terraform: orphan dependencies should be inverted
This commit is contained in:
parent
74386655a5
commit
7031cb145c
@ -154,6 +154,49 @@ func TestBuiltinGraphBuilder_multiLevelModule(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuiltinGraphBuilder_orphanDeps(t *testing.T) {
|
||||
state := &State{
|
||||
Modules: []*ModuleState{
|
||||
&ModuleState{
|
||||
Path: rootModulePath,
|
||||
Resources: map[string]*ResourceState{
|
||||
"aws_instance.foo": &ResourceState{
|
||||
Type: "aws_instance",
|
||||
Primary: &InstanceState{
|
||||
ID: "foo",
|
||||
},
|
||||
},
|
||||
|
||||
"aws_instance.bar": &ResourceState{
|
||||
Type: "aws_instance",
|
||||
Dependencies: []string{"aws_instance.foo"},
|
||||
Primary: &InstanceState{
|
||||
ID: "bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
b := &BuiltinGraphBuilder{
|
||||
Root: testModule(t, "graph-builder-orphan-deps"),
|
||||
State: state,
|
||||
Validate: true,
|
||||
}
|
||||
|
||||
g, err := b.Build(RootModulePath)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(g.String())
|
||||
expected := strings.TrimSpace(testBuiltinGraphBuilderOrphanDepsStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad: %s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
TODO: This exposes a really bad bug we need to fix after we merge
|
||||
the f-ast-branch. This bug still exists in master.
|
||||
@ -233,6 +276,16 @@ root
|
||||
module.foo.plan-destroy
|
||||
`
|
||||
|
||||
const testBuiltinGraphBuilderOrphanDepsStr = `
|
||||
aws_instance.bar (orphan)
|
||||
provider.aws
|
||||
aws_instance.foo (orphan)
|
||||
aws_instance.bar (orphan)
|
||||
provider.aws
|
||||
provider.aws (close)
|
||||
aws_instance.foo (orphan)
|
||||
`
|
||||
|
||||
/*
|
||||
TODO: Commented out this const as it's likely this needs to
|
||||
be updated when the TestBuiltinGraphBuilder_modules test is
|
||||
|
@ -0,0 +1 @@
|
||||
provider "aws" {}
|
@ -1,6 +1,8 @@
|
||||
package terraform
|
||||
|
||||
import "github.com/hashicorp/terraform/dag"
|
||||
import (
|
||||
"github.com/hashicorp/terraform/dag"
|
||||
)
|
||||
|
||||
type GraphNodeDestroyMode byte
|
||||
|
||||
@ -103,6 +105,12 @@ func (t *DestroyTransformer) transform(
|
||||
nodeToCn[n] = cn
|
||||
nodeToDn[cn] = n
|
||||
|
||||
// If the creation node is equal to the destroy node, then
|
||||
// don't do any of the edge jump rope below.
|
||||
if n.(interface{}) == cn.(interface{}) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Add it to the graph
|
||||
g.Add(n)
|
||||
|
||||
|
@ -298,6 +298,24 @@ func (n *graphNodeOrphanResource) dependableName() string {
|
||||
return n.ResourceName
|
||||
}
|
||||
|
||||
// GraphNodeDestroyable impl.
|
||||
func (n *graphNodeOrphanResource) DestroyNode(mode GraphNodeDestroyMode) GraphNodeDestroy {
|
||||
if mode != DestroyPrimary {
|
||||
return nil
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
// GraphNodeDestroy impl.
|
||||
func (n *graphNodeOrphanResource) CreateBeforeDestroy() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (n *graphNodeOrphanResource) CreateNode() dag.Vertex {
|
||||
return n
|
||||
}
|
||||
|
||||
// Same as graphNodeOrphanResource, but for flattening
|
||||
type graphNodeOrphanResourceFlat struct {
|
||||
*graphNodeOrphanResource
|
||||
@ -313,3 +331,21 @@ func (n *graphNodeOrphanResourceFlat) Name() string {
|
||||
func (n *graphNodeOrphanResourceFlat) Path() []string {
|
||||
return n.PathValue
|
||||
}
|
||||
|
||||
// GraphNodeDestroyable impl.
|
||||
func (n *graphNodeOrphanResourceFlat) DestroyNode(mode GraphNodeDestroyMode) GraphNodeDestroy {
|
||||
if mode != DestroyPrimary {
|
||||
return nil
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
// GraphNodeDestroy impl.
|
||||
func (n *graphNodeOrphanResourceFlat) CreateBeforeDestroy() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (n *graphNodeOrphanResourceFlat) CreateNode() dag.Vertex {
|
||||
return n
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user