From f3af221866785450257edc7c117f3069adf4eb20 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 23 Feb 2015 21:32:27 -0800 Subject: [PATCH] terraform: make DeepCopy public --- terraform/context.go | 6 +++--- terraform/state.go | 38 ++++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/terraform/context.go b/terraform/context.go index 97abe573c..e2db6e6f9 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -224,7 +224,7 @@ func (c *Context) Apply() (*State, error) { defer c.releaseRun(v) // Copy our own state - c.state = c.state.deepcopy() + c.state = c.state.DeepCopy() // Do the walk _, err := c.walk(walkApply) @@ -264,7 +264,7 @@ func (c *Context) Plan(opts *PlanOpts) (*Plan, error) { c.state = &State{} c.state.init() } else { - c.state = old.deepcopy() + c.state = old.DeepCopy() } defer func() { c.state = old @@ -299,7 +299,7 @@ func (c *Context) Refresh() (*State, error) { defer c.releaseRun(v) // Copy our own state - c.state = c.state.deepcopy() + c.state = c.state.DeepCopy() // Do the walk if _, err := c.walk(walkRefresh); err != nil { diff --git a/terraform/state.go b/terraform/state.go index 68b5c11c6..f94d9bedc 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -188,6 +188,26 @@ func (s *State) Equal(other *State) bool { return true } +// DeepCopy performs a deep copy of the state structure and returns +// a new structure. +func (s *State) DeepCopy() *State { + if s == nil { + return nil + } + n := &State{ + Version: s.Version, + Serial: s.Serial, + Modules: make([]*ModuleState, 0, len(s.Modules)), + } + for _, mod := range s.Modules { + n.Modules = append(n.Modules, mod.deepcopy()) + } + if s.Remote != nil { + n.Remote = s.Remote.deepcopy() + } + return n +} + // IncrementSerialMaybe increments the serial number of this state // if it different from the other state. func (s *State) IncrementSerialMaybe(other *State) { @@ -209,24 +229,6 @@ func (s *State) init() { } } -func (s *State) deepcopy() *State { - if s == nil { - return nil - } - n := &State{ - Version: s.Version, - Serial: s.Serial, - Modules: make([]*ModuleState, 0, len(s.Modules)), - } - for _, mod := range s.Modules { - n.Modules = append(n.Modules, mod.deepcopy()) - } - if s.Remote != nil { - n.Remote = s.Remote.deepcopy() - } - return n -} - // prune is used to remove any resources that are no longer required func (s *State) prune() { if s == nil {