Move the state module cleanup from init to prune

It makes for sense for this to happen in State.prune(). Also move a
redundant pruning from ResourceState.init, and make sure
ResourceState.prune is called from the parent's prune method.
This commit is contained in:
James Bardin 2016-11-20 11:31:24 -05:00
parent def55e52ca
commit a5cb530571
1 changed files with 20 additions and 26 deletions

View File

@ -644,16 +644,9 @@ func (s *State) init() {
} }
s.ensureHasLineage() s.ensureHasLineage()
// Filter out empty modules. // We can't trust that state read from a file doesn't have nil/empty
// A module is always assumed to have a path, and it's length isn't always // modules
// bounds checked later on. Modules may be "emptied" during destroy, but we s.prune()
// never want to store those in the state.
for i := 0; i < len(s.Modules); i++ {
if s.Modules[i] == nil || len(s.Modules[i].Path) == 0 {
s.Modules = append(s.Modules[:i], s.Modules[i+1:]...)
i--
}
}
for _, mod := range s.Modules { for _, mod := range s.Modules {
mod.init() mod.init()
@ -662,6 +655,7 @@ func (s *State) init() {
if s.Remote != nil { if s.Remote != nil {
s.Remote.init() s.Remote.init()
} }
} }
func (s *State) EnsureHasLineage() { func (s *State) EnsureHasLineage() {
@ -706,6 +700,18 @@ func (s *State) prune() {
if s == nil { if s == nil {
return return
} }
// Filter out empty modules.
// A module is always assumed to have a path, and it's length isn't always
// bounds checked later on. Modules may be "emptied" during destroy, but we
// never want to store those in the state.
for i := 0; i < len(s.Modules); i++ {
if s.Modules[i] == nil || len(s.Modules[i].Path) == 0 {
s.Modules = append(s.Modules[:i], s.Modules[i+1:]...)
i--
}
}
for _, mod := range s.Modules { for _, mod := range s.Modules {
mod.prune() mod.prune()
} }
@ -1085,11 +1091,12 @@ func (m *ModuleState) prune() {
defer m.Unlock() defer m.Unlock()
for k, v := range m.Resources { for k, v := range m.Resources {
v.prune() if v == nil || (v.Primary == nil || v.Primary.ID == "") && len(v.Deposed) == 0 {
if (v.Primary == nil || v.Primary.ID == "") && len(v.Deposed) == 0 {
delete(m.Resources, k) delete(m.Resources, k)
continue
} }
v.prune()
} }
for k, v := range m.Outputs { for k, v := range m.Outputs {
@ -1431,19 +1438,6 @@ func (s *ResourceState) init() {
if s.Deposed == nil { if s.Deposed == nil {
s.Deposed = make([]*InstanceState, 0) s.Deposed = make([]*InstanceState, 0)
} }
// clean out any possible nil values read in from the state file
end := len(s.Deposed) - 1
for i := 0; i <= end; i++ {
if s.Deposed[i] == nil {
s.Deposed[i], s.Deposed[end] = s.Deposed[end], s.Deposed[i]
end--
i--
} else {
s.Deposed[i].init()
}
}
s.Deposed = s.Deposed[:end+1]
} }
func (s *ResourceState) deepcopy() *ResourceState { func (s *ResourceState) deepcopy() *ResourceState {