From 1aff5e98e1a186f9a966b06b4f801c1300b79c23 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 17 Sep 2014 17:18:03 -0700 Subject: [PATCH] terraform: some deepcopying going on --- terraform/context.go | 20 +++++++++++++------- terraform/context_test.go | 2 +- terraform/state.go | 10 +++++----- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/terraform/context.go b/terraform/context.go index ab719c17b..ee965b3bf 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -752,8 +752,10 @@ func (c *Context) planWalkFn(result *Plan) depgraph.WalkFunc { cb := func(r *Resource) error { var diff *InstanceDiff + is := r.State.Primary + for _, h := range c.hooks { - handleHook(h.PreDiff(r.Id, r.State.Primary)) + handleHook(h.PreDiff(r.Id, is)) } if r.Config == nil { @@ -794,15 +796,15 @@ func (c *Context) planWalkFn(result *Plan) depgraph.WalkFunc { diff.Destroy = true } - if diff.RequiresNew() && r.State.Primary != nil && r.State.Primary.ID != "" { + if diff.RequiresNew() && is != nil && is.ID != "" { // This will also require a destroy diff.Destroy = true } - if diff.RequiresNew() || r.State.Primary == nil || r.State.Primary.ID == "" { + if diff.RequiresNew() || is == nil || is.ID == "" { var oldID string - if r.State.Primary != nil { - oldID = r.State.Primary.Attributes["id"] + if is != nil { + oldID = is.Attributes["id"] } // Add diff to compute new ID @@ -827,16 +829,20 @@ func (c *Context) planWalkFn(result *Plan) depgraph.WalkFunc { // Determine the new state and update variables if !diff.Empty() { - r.State.Primary = r.State.Primary.MergeDiff(diff) + is = is.MergeDiff(diff) } + // TODO(mitchellh): do we really need this? + state := r.State.deepcopy() + state.Primary = is + // Update our internal state so that variable computation works c.sl.Lock() defer c.sl.Unlock() // TODO: Handle other modules mod := c.state.RootModule() - mod.Resources[r.Id] = r.State + mod.Resources[r.Id] = state return nil } diff --git a/terraform/context_test.go b/terraform/context_test.go index 7eda3899c..a31b0ec15 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -1780,7 +1780,7 @@ func TestContextPlan_diffVar(t *testing.T) { actual := strings.TrimSpace(plan.String()) expected := strings.TrimSpace(testTerraformPlanDiffVarStr) if actual != expected { - t.Fatalf("bad:\n%s", actual) + t.Fatalf("actual:\n%s\n\nexpected:\n%s", actual, expected) } } diff --git a/terraform/state.go b/terraform/state.go index d58d4cf5b..f0cec4c9e 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -413,11 +413,11 @@ func (i *InstanceState) deepcopy() *InstanceState { // won't be available until apply, the value is replaced with the // computeID. func (s *InstanceState) MergeDiff(d *InstanceDiff) *InstanceState { - var result InstanceState - if s != nil { - result = *s + result := s.deepcopy() + if result == nil { + result = new(InstanceState) + result.init() } - result.init() if s != nil { for k, v := range s.Attributes { @@ -439,7 +439,7 @@ func (s *InstanceState) MergeDiff(d *InstanceDiff) *InstanceState { } } - return &result + return result } func (i *InstanceState) GoString() string {