backend/gcs: fix locking issue when used with terraform_remote_state

Previously there was a problem with double-locking when using the GCS backend with the terraform_remote_state data source.

Here we adjust the locking methodology to avoid that problem.
This commit is contained in:
Kaveh Mousavi Zamani 2017-12-06 18:36:16 +01:00 committed by Martin Atkins
parent 12b7dac124
commit 7507e3cd21
1 changed files with 33 additions and 30 deletions

View File

@ -91,6 +91,15 @@ func (b *gcsBackend) State(name string) (state.State, error) {
}
st := &remote.State{Client: c}
// Grab the value
if err := st.RefreshState(); err != nil {
return nil, err
}
// If we have no state, we have to create an empty state
if v := st.State(); v == nil {
lockInfo := state.NewLockInfo()
lockInfo.Operation = "init"
lockID, err := st.Lock(lockInfo)
@ -117,26 +126,20 @@ the initial state file is created.`
return baseErr
}
// Grab the value
if err := st.RefreshState(); err != nil {
return nil, unlock(err)
}
// If we have no state, we have to create an empty state
if v := st.State(); v == nil {
if err := st.WriteState(terraform.NewState()); err != nil {
return nil, unlock(err)
}
if err := st.PersistState(); err != nil {
return nil, unlock(err)
}
}
// Unlock, the state should now be initialized
if err := unlock(nil); err != nil {
return nil, err
}
}
return st, nil
}