diff --git a/terraform/state.go b/terraform/state.go index 6f9373de7..a98bc8c39 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -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 {