terraform: some deepcopying going on

This commit is contained in:
Mitchell Hashimoto 2014-09-17 17:18:03 -07:00
parent c14a17f39b
commit 1aff5e98e1
3 changed files with 19 additions and 13 deletions

View File

@ -752,8 +752,10 @@ func (c *Context) planWalkFn(result *Plan) depgraph.WalkFunc {
cb := func(r *Resource) error { cb := func(r *Resource) error {
var diff *InstanceDiff var diff *InstanceDiff
is := r.State.Primary
for _, h := range c.hooks { for _, h := range c.hooks {
handleHook(h.PreDiff(r.Id, r.State.Primary)) handleHook(h.PreDiff(r.Id, is))
} }
if r.Config == nil { if r.Config == nil {
@ -794,15 +796,15 @@ func (c *Context) planWalkFn(result *Plan) depgraph.WalkFunc {
diff.Destroy = true 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 // This will also require a destroy
diff.Destroy = true diff.Destroy = true
} }
if diff.RequiresNew() || r.State.Primary == nil || r.State.Primary.ID == "" { if diff.RequiresNew() || is == nil || is.ID == "" {
var oldID string var oldID string
if r.State.Primary != nil { if is != nil {
oldID = r.State.Primary.Attributes["id"] oldID = is.Attributes["id"]
} }
// Add diff to compute new 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 // Determine the new state and update variables
if !diff.Empty() { 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 // Update our internal state so that variable computation works
c.sl.Lock() c.sl.Lock()
defer c.sl.Unlock() defer c.sl.Unlock()
// TODO: Handle other modules // TODO: Handle other modules
mod := c.state.RootModule() mod := c.state.RootModule()
mod.Resources[r.Id] = r.State mod.Resources[r.Id] = state
return nil return nil
} }

View File

@ -1780,7 +1780,7 @@ func TestContextPlan_diffVar(t *testing.T) {
actual := strings.TrimSpace(plan.String()) actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanDiffVarStr) expected := strings.TrimSpace(testTerraformPlanDiffVarStr)
if actual != expected { if actual != expected {
t.Fatalf("bad:\n%s", actual) t.Fatalf("actual:\n%s\n\nexpected:\n%s", actual, expected)
} }
} }

View File

@ -413,11 +413,11 @@ func (i *InstanceState) deepcopy() *InstanceState {
// won't be available until apply, the value is replaced with the // won't be available until apply, the value is replaced with the
// computeID. // computeID.
func (s *InstanceState) MergeDiff(d *InstanceDiff) *InstanceState { func (s *InstanceState) MergeDiff(d *InstanceDiff) *InstanceState {
var result InstanceState result := s.deepcopy()
if s != nil { if result == nil {
result = *s result = new(InstanceState)
}
result.init() result.init()
}
if s != nil { if s != nil {
for k, v := range s.Attributes { 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 { func (i *InstanceState) GoString() string {