From edc0ce6333d06699024048385c74316c307f23c2 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 11 Sep 2018 16:57:27 -0700 Subject: [PATCH] states: Prune empty modules after possibly removing resources Also includes a new log message for the situation where we _do_ prune, since this seems helpful during debugging. --- states/sync.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/states/sync.go b/states/sync.go index 617be07ca..9f36c42c3 100644 --- a/states/sync.go +++ b/states/sync.go @@ -1,6 +1,7 @@ package states import ( + "log" "sync" "github.com/zclconf/go-cty/cty" @@ -267,6 +268,7 @@ func (s *SyncState) SetResourceInstanceCurrent(addr addrs.AbsResourceInstance, o ms := s.state.EnsureModule(addr.Module) ms.SetResourceInstanceCurrent(addr.Resource, obj.DeepCopy(), provider) + s.maybePruneModule(addr.Module) } // SetResourceInstanceDeposed saves the given instance object as a deposed @@ -298,6 +300,7 @@ func (s *SyncState) SetResourceInstanceDeposed(addr addrs.AbsResourceInstance, k ms := s.state.EnsureModule(addr.Module) ms.SetResourceInstanceDeposed(addr.Resource, key, obj.DeepCopy(), provider) + s.maybePruneModule(addr.Module) } // DeposeResourceInstanceObject moves the current instance object for the @@ -334,6 +337,7 @@ func (s *SyncState) ForgetResourceInstanceDeposed(addr addrs.AbsResourceInstance return } ms.ForgetResourceInstanceDeposed(addr.Resource, key) + s.maybePruneModule(addr.Module) } // RemovePlannedResourceInstanceObjects removes from the state any resource @@ -428,6 +432,7 @@ func (s *SyncState) maybePruneModule(addr addrs.ModuleInstance) { } if ms.empty() { + log.Printf("[TRACE] states.SyncState: pruning %s because it is empty", addr) s.state.RemoveModule(addr) } }