terraform: orphan dependencies should be inverted

This commit is contained in:
Mitchell Hashimoto 2015-06-23 20:41:02 -07:00
parent 74386655a5
commit 7031cb145c
4 changed files with 99 additions and 1 deletions

View File

@ -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 TODO: This exposes a really bad bug we need to fix after we merge
the f-ast-branch. This bug still exists in master. the f-ast-branch. This bug still exists in master.
@ -233,6 +276,16 @@ root
module.foo.plan-destroy 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 TODO: Commented out this const as it's likely this needs to
be updated when the TestBuiltinGraphBuilder_modules test is be updated when the TestBuiltinGraphBuilder_modules test is

View File

@ -0,0 +1 @@
provider "aws" {}

View File

@ -1,6 +1,8 @@
package terraform package terraform
import "github.com/hashicorp/terraform/dag" import (
"github.com/hashicorp/terraform/dag"
)
type GraphNodeDestroyMode byte type GraphNodeDestroyMode byte
@ -103,6 +105,12 @@ func (t *DestroyTransformer) transform(
nodeToCn[n] = cn nodeToCn[n] = cn
nodeToDn[cn] = n 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 // Add it to the graph
g.Add(n) g.Add(n)

View File

@ -298,6 +298,24 @@ func (n *graphNodeOrphanResource) dependableName() string {
return n.ResourceName 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 // Same as graphNodeOrphanResource, but for flattening
type graphNodeOrphanResourceFlat struct { type graphNodeOrphanResourceFlat struct {
*graphNodeOrphanResource *graphNodeOrphanResource
@ -313,3 +331,21 @@ func (n *graphNodeOrphanResourceFlat) Name() string {
func (n *graphNodeOrphanResourceFlat) Path() []string { func (n *graphNodeOrphanResourceFlat) Path() []string {
return n.PathValue 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
}