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 {
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
}

View File

@ -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)
}
}

View File

@ -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 {