From 691db584780a735a64e09309b1ade9ffdeb4a3a0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 12 Feb 2015 20:03:44 -0800 Subject: [PATCH] terraform: apply for orphans --- terraform/context_test.go | 6 +++--- terraform/eval_diff.go | 14 ++++++++++++++ terraform/transform_orphan.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/terraform/context_test.go b/terraform/context_test.go index c097f1d5d5..6c3917f2ab 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -2948,8 +2948,7 @@ func TestContext2Apply_compute(t *testing.T) { } } -/* -func TestContextApply_countDecrease(t *testing.T) { +func TestContext2Apply_countDecrease(t *testing.T) { m := testModule(t, "apply-count-dec") p := testProvider("aws") p.DiffFn = testDiffFn @@ -2992,7 +2991,7 @@ func TestContextApply_countDecrease(t *testing.T) { }, }, } - ctx := testContext(t, &ContextOpts{ + ctx := testContext2(t, &ContextOpts{ Module: m, Providers: map[string]ResourceProviderFactory{ "aws": testProviderFuncFixed(p), @@ -3016,6 +3015,7 @@ func TestContextApply_countDecrease(t *testing.T) { } } +/* func TestContextApply_countDecreaseToOne(t *testing.T) { m := testModule(t, "apply-count-dec-one") p := testProvider("aws") diff --git a/terraform/eval_diff.go b/terraform/eval_diff.go index 3ec842567d..4fab4c2d8d 100644 --- a/terraform/eval_diff.go +++ b/terraform/eval_diff.go @@ -2,6 +2,7 @@ package terraform import ( "fmt" + "log" ) // EvalCompareDiff is an EvalNode implementation that compares two diffs @@ -20,7 +21,20 @@ func (n *EvalCompareDiff) Eval( ctx EvalContext, args []interface{}) (interface{}, error) { one, two := *n.One, *n.Two + // If either are nil, let them be empty + if one == nil { + one = new(InstanceDiff) + one.init() + } + if two == nil { + two = new(InstanceDiff) + two.init() + } + if !one.Same(two) { + log.Printf("[ERROR] %s: diff's didn't match", n.Info.Id) + log.Printf("[ERROR] %s: diff one: %#v", n.Info.Id, one) + log.Printf("[ERROR] %s: diff two: %#v", n.Info.Id, two) return nil, fmt.Errorf( "%s: diffs didn't match during apply. This is a bug with "+ "Terraform and should be reported.", n.Info.Id) diff --git a/terraform/transform_orphan.go b/terraform/transform_orphan.go index 6d446f41b6..e1c1f8a1e9 100644 --- a/terraform/transform_orphan.go +++ b/terraform/transform_orphan.go @@ -223,6 +223,41 @@ func (n *graphNodeOrphanResource) EvalTree() EvalNode { }, }) + // Apply + var provider ResourceProvider + seq.Nodes = append(seq.Nodes, &EvalOpFilter{ + Ops: []walkOperation{walkApply}, + Node: &EvalSequence{ + Nodes: []EvalNode{ + &EvalReadDiff{ + Name: n.ResourceName, + Diff: &diff, + }, + &EvalGetProvider{ + Name: n.ProvidedBy()[0], + Output: &provider, + }, + &EvalReadState{ + Name: n.ResourceName, + Output: &state, + }, + &EvalApply{ + Info: info, + State: &state, + Diff: &diff, + Provider: &provider, + Output: &state, + }, + &EvalWriteState{ + Name: n.ResourceName, + ResourceType: n.ResourceType, + Dependencies: n.DependentOn(), + State: &state, + }, + }, + }, + }) + return seq }