From e5ff4d5bd4a97113b53fb69a9a06327ee266bd51 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 31 Aug 2016 16:37:45 -0400 Subject: [PATCH] create some unlocked methods for State State.Init() needs to be called externally, so make sure it only calls unlocked internal methods once a lock is acquired. --- terraform/state.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/terraform/state.go b/terraform/state.go index 884ac9436..2ab56ba6c 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -128,6 +128,11 @@ func (s *State) children(path []string) []*ModuleState { func (s *State) AddModule(path []string) *ModuleState { s.Lock() defer s.Unlock() + + return s.addModule(path) +} + +func (s *State) addModule(path []string) *ModuleState { // check if the module exists first m := s.moduleByPath(path) if m != nil { @@ -616,10 +621,10 @@ func (s *State) init() { if s.Version == 0 { s.Version = StateVersion } - if s.ModuleByPath(rootModulePath) == nil { - s.AddModule(rootModulePath) + if s.moduleByPath(rootModulePath) == nil { + s.addModule(rootModulePath) } - s.EnsureHasLineage() + s.ensureHasLineage() for _, mod := range s.Modules { mod.init() @@ -634,6 +639,10 @@ func (s *State) EnsureHasLineage() { s.Lock() defer s.Unlock() + s.ensureHasLineage() +} + +func (s *State) ensureHasLineage() { if s.Lineage == "" { s.Lineage = uuid.NewV4().String() log.Printf("[DEBUG] New state was assigned lineage %q\n", s.Lineage) @@ -648,6 +657,10 @@ func (s *State) AddModuleState(mod *ModuleState) { s.Lock() defer s.Unlock() + s.addModuleState(mod) +} + +func (s *State) addModuleState(mod *ModuleState) { for i, m := range s.Modules { if reflect.DeepEqual(m.Path, mod.Path) { s.Modules[i] = mod