terraform: handle state pruning

This commit is contained in:
Armon Dadgar 2014-09-15 17:06:32 -07:00
parent bb26c79421
commit ab7ae0516c
1 changed files with 31 additions and 0 deletions

View File

@ -35,6 +35,13 @@ func (s *State) deepcopy() *State {
return n
}
// prune is used to remove any resources that are no longer required
func (s *State) prune() {
for _, mod := range m.Modules {
mod.prune()
}
}
// ModuleState is used to track all the state relevant to a single
// module. Previous to Terraform 0.3, all state belonged to the "root"
// module.
@ -71,6 +78,16 @@ func (m *ModuleState) deepcopy() *ModuleState {
return n
}
// prune is used to remove any resources that are no longer required
func (m *ModuleState) prune() {
for k, v := range m.Resources {
v.prune()
if len(v.instances) == 0 {
delete(m.Resources, k)
}
}
}
// ResourceState holds the state of a resource that is used so that
// a provider can find and manage an existing resource as well as for
// storing attributes that are used to populate variables of child
@ -126,6 +143,20 @@ func (r *ResourceState) deepcopy() *ResourceState {
return n
}
// prune is used to remove any instances that are no longer required
func (r *ResourceState) prune() {
n := len(r.Instances)
for i := 0; i < n; i++ {
inst := r.Instances[i]
if inst.ID == "" {
copy(r.Instances[i:], r.Instances[i+1:])
r.Instances[n-1] = nil
n--
}
}
r.Instances = r.Instances[:n]
}
// InstanceState is used to track the unique state information belonging
// to a given instance.
type InstanceState struct {