terraform: make DeepCopy public

This commit is contained in:
Mitchell Hashimoto 2015-02-23 21:32:27 -08:00
parent ed6128aa6e
commit f3af221866
2 changed files with 23 additions and 21 deletions

View File

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

View File

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