Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: respect roots in CBD transform #1953

Merged
merged 1 commit into from
May 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions terraform/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,43 @@ func TestContext2Plan(t *testing.T) {
}
}

func TestContext2Plan_createBefore_maintainRoot(t *testing.T) {
m := testModule(t, "plan-cbd-maintain-root")
p := testProvider("aws")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
Variables: map[string]string{
"in": "a,b,c",
},
})

plan, err := ctx.Plan()
if err != nil {
t.Fatalf("err: %s", err)
}

actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(`
DIFF:

CREATE: aws_instance.bar.0
CREATE: aws_instance.bar.1
CREATE: aws_instance.foo.0
CREATE: aws_instance.foo.1

STATE:

<no state>
`)
if actual != expected {
t.Fatalf("expected:\n%s, got:\n%s", expected, actual)
}
}

func TestContext2Plan_emptyDiff(t *testing.T) {
m := testModule(t, "plan-empty")
p := testProvider("aws")
Expand Down
13 changes: 13 additions & 0 deletions terraform/test-fixtures/plan-cbd-maintain-root/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "aws_instance" "foo" {
count = "2"
lifecycle { create_before_destroy = true }
}

resource "aws_instance" "bar" {
count = "2"
lifecycle { create_before_destroy = true }
}

output "out" {
value = "${aws_instance.foo.0.id}"
}
12 changes: 9 additions & 3 deletions terraform/transform_destroy.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package terraform

import (
"github.com/hashicorp/terraform/dag"
)
import "github.com/hashicorp/terraform/dag"

type GraphNodeDestroyMode byte

Expand Down Expand Up @@ -193,6 +191,14 @@ func (t *CreateBeforeDestroyTransformer) Transform(g *Graph) error {
// This ensures that.
for _, sourceRaw := range g.UpEdges(cn).List() {
source := sourceRaw.(dag.Vertex)

// If the graph has a "root" node (one added by a RootTransformer and not
// just a resource that happens to have no ancestors), we don't want to
// add any edges to it, because then it ceases to be a root.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this makes a lot of sense. We now do a root transformer potentially before the destroy transform which never happened before. I see how this happened.

if _, ok := source.(graphNodeRoot); ok {
continue
}

connect = append(connect, dag.BasicEdge(dn, source))
}

Expand Down