terraform: cbd works!

This commit is contained in:
Mitchell Hashimoto 2016-09-22 11:03:03 -07:00
parent 23665790f3
commit c1664d2eaa
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
7 changed files with 65 additions and 16 deletions

View File

@ -2297,6 +2297,7 @@ func TestContext2Apply_multiDepose_createBeforeDestroy(t *testing.T) {
checkStateString(t, state, `
aws_instance.web: (1 deposed)
ID = bar
provider = aws
Deposed ID 1 = foo
`)
@ -2321,6 +2322,7 @@ aws_instance.web: (1 deposed)
checkStateString(t, state, `
aws_instance.web: (2 deposed)
ID = baz
provider = aws
Deposed ID 1 = foo
Deposed ID 2 = bar
`)
@ -2348,6 +2350,7 @@ aws_instance.web: (2 deposed)
checkStateString(t, state, `
aws_instance.web: (1 deposed)
ID = qux
provider = aws
Deposed ID 1 = bar
`)
@ -2369,6 +2372,7 @@ aws_instance.web: (1 deposed)
checkStateString(t, state, `
aws_instance.web:
ID = quux
provider = aws
`)
}
@ -4746,8 +4750,10 @@ func TestContext2Apply_createBefore_depends(t *testing.T) {
State: state,
})
if _, err := ctx.Plan(); err != nil {
if p, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
} else {
t.Logf("plan: %s", p)
}
h.Active = true
@ -4764,7 +4770,7 @@ func TestContext2Apply_createBefore_depends(t *testing.T) {
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testTerraformApplyDependsCreateBeforeStr)
if actual != expected {
t.Fatalf("bad: \n%s\n%s", actual, expected)
t.Fatalf("bad: \n%s\n\n%s", actual, expected)
}
// Test that things were managed _in the right order_

View File

@ -263,10 +263,11 @@ func (g *Graph) walk(walker GraphWalker) error {
rerr = err
return
}
// Walk the subgraph
if rerr = g.walk(walker); rerr != nil {
return
if g != nil {
// Walk the subgraph
if rerr = g.walk(walker); rerr != nil {
return
}
}
}

View File

@ -27,6 +27,9 @@ type ApplyGraphBuilder struct {
// Provisioners is the list of provisioners supported.
Provisioners []string
// DisableReduce, if true, will not reduce the graph. Great for testing.
DisableReduce bool
}
// See GraphBuilder
@ -103,10 +106,12 @@ func (b *ApplyGraphBuilder) Steps() []GraphTransformer {
// Single root
&RootTransformer{},
}
if !b.DisableReduce {
// Perform the transitive reduction to make our graph a bit
// more sane if possible (it usually is possible).
&TransitiveReductionTransformer{},
steps = append(steps, &TransitiveReductionTransformer{})
}
return steps

View File

@ -65,10 +65,11 @@ func TestApplyGraphBuilder(t *testing.T) {
}
b := &ApplyGraphBuilder{
Module: testModule(t, "graph-builder-apply-basic"),
Diff: diff,
Providers: []string{"aws"},
Provisioners: []string{"exec"},
Module: testModule(t, "graph-builder-apply-basic"),
Diff: diff,
Providers: []string{"aws"},
Provisioners: []string{"exec"},
DisableReduce: true,
}
g, err := b.Build(RootModulePath)
@ -93,6 +94,14 @@ aws_instance.create
aws_instance.other
aws_instance.create
provider.aws
meta.count-boundary (count boundary fixup)
aws_instance.create
aws_instance.other
module.child.aws_instance.create
module.child.aws_instance.other
module.child.provider.aws
provider.aws
provisioner.exec
module.child.aws_instance.create
module.child.provider.aws
provisioner.exec
@ -103,7 +112,4 @@ module.child.provider.aws
provider.aws
provider.aws
provisioner.exec
root
aws_instance.other
module.child.aws_instance.other
`

View File

@ -6,7 +6,7 @@ import (
// NodeDestroyResource represents a resource that is to be destroyed.
type NodeDestroyResource struct {
*NodeAbstractResource
NodeAbstractResource
}
func (n *NodeDestroyResource) Name() string {
@ -38,6 +38,34 @@ func (n *NodeDestroyResource) References() []string {
return nil
}
// GraphNodeDynamicExpandable
func (n *NodeDestroyResource) DynamicExpand(ctx EvalContext) (*Graph, error) {
// If we have no config we do nothing
if n.Config == nil {
return nil, nil
}
state, lock := ctx.State()
lock.RLock()
defer lock.RUnlock()
// Start creating the steps
steps := make([]GraphTransformer, 0, 5)
// We want deposed resources in the state to be destroyed
steps = append(steps, &DeposedTransformer{
State: state,
View: n.Config.Id(),
})
// Always end with the root being added
steps = append(steps, &RootTransformer{})
// Build the graph
b := &BasicGraphBuilder{Steps: steps}
return b.Build(ctx.Path())
}
// GraphNodeEvalable
func (n *NodeDestroyResource) EvalTree() EvalNode {
// stateId is the ID to put into the state

View File

@ -298,6 +298,7 @@ aws_instance.lb:
aws_instance.web
aws_instance.web:
ID = foo
provider = aws
require_new = ami-new
type = aws_instance
`
@ -305,6 +306,7 @@ aws_instance.web:
const testTerraformApplyCreateBeforeStr = `
aws_instance.bar:
ID = foo
provider = aws
require_new = xyz
type = aws_instance
`
@ -591,6 +593,7 @@ aws_instance.bar:
const testTerraformApplyErrorDestroyCreateBeforeDestroyStr = `
aws_instance.bar: (1 deposed)
ID = foo
provider = aws
Deposed ID 1 = bar
`

View File

@ -59,7 +59,7 @@ func (t *DiffTransformer) Transform(g *Graph) error {
// If we're destroying, add the destroy node
if inst.Destroy {
abstract := &NodeAbstractResource{Addr: addr}
abstract := NodeAbstractResource{Addr: addr}
g.Add(&NodeDestroyResource{NodeAbstractResource: abstract})
}