From 9f56fc8ddcd1b3191d6be3329891f778c553de61 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 7 Jul 2014 15:43:32 -0700 Subject: [PATCH] terraform: copy the proper dependencies over on destroy plan --- terraform/graph.go | 17 ++++++++++++++++- terraform/graph_test.go | 5 +++++ .../test-fixtures/graph-diff-destroy/main.tf | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/terraform/graph.go b/terraform/graph.go index 61fb94cea..c952bb4ee 100644 --- a/terraform/graph.go +++ b/terraform/graph.go @@ -283,11 +283,26 @@ func graphAddDiff(g *depgraph.Graph, d *Diff) error { // Make the diff _just_ the destroy. newNode.Resource.Diff = &ResourceDiff{Destroy: true} - // Append it to the list so we handle it later + // Create the new node newN := &depgraph.Noun{ Name: fmt.Sprintf("%s (destroy)", newNode.Resource.Id), Meta: newNode, } + newN.Deps = make([]*depgraph.Dependency, 0, len(n.Deps)) + for _, d := range n.Deps { + // We don't want to copy any resource dependencies + if _, ok := d.Target.Meta.(*GraphNodeResource); ok { + continue + } + + newN.Deps = append(newN.Deps, &depgraph.Dependency{ + Name: d.Name, + Source: newN, + Target: d.Target, + }) + } + + // Append it to the list so we handle it later nlist = append(nlist, newN) // Mark the old diff to not destroy since we handle that in diff --git a/terraform/graph_test.go b/terraform/graph_test.go index 1b38be511..b634b9cce 100644 --- a/terraform/graph_test.go +++ b/terraform/graph_test.go @@ -277,11 +277,16 @@ root: root aws_instance.bar aws_instance.bar -> aws_instance.bar (destroy) aws_instance.bar -> aws_instance.foo + aws_instance.bar -> provider.aws aws_instance.bar (destroy) + aws_instance.bar (destroy) -> provider.aws aws_instance.foo aws_instance.foo -> aws_instance.foo (destroy) + aws_instance.foo -> provider.aws aws_instance.foo (destroy) aws_instance.foo (destroy) -> aws_instance.bar (destroy) + aws_instance.foo (destroy) -> provider.aws +provider.aws root root -> aws_instance.bar root -> aws_instance.foo diff --git a/terraform/test-fixtures/graph-diff-destroy/main.tf b/terraform/test-fixtures/graph-diff-destroy/main.tf index da9db4e0c..acbc6dd33 100644 --- a/terraform/test-fixtures/graph-diff-destroy/main.tf +++ b/terraform/test-fixtures/graph-diff-destroy/main.tf @@ -1,3 +1,5 @@ +provider "aws" {} + resource "aws_instance" "foo" { }