backend/remote-state/gcloud: Refactor Backend.State().
Fixes: * https://github.com/golang/go/wiki/CodeReviewComments#doc-comments * https://github.com/golang/go/wiki/CodeReviewComments#error-strings * https://github.com/golang/go/wiki/CodeReviewComments#initialisms
This commit is contained in:
parent
5cb574035a
commit
42e8441a2b
|
@ -82,63 +82,60 @@ func (b *Backend) remoteClient(name string) (*RemoteClient, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// State reads and returns the named state from GCS. If the named state does
|
||||||
|
// not yet exist, a new state file is created.
|
||||||
func (b *Backend) State(name string) (state.State, error) {
|
func (b *Backend) State(name string) (state.State, error) {
|
||||||
client, err := b.remoteClient(name)
|
client, err := b.remoteClient(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to create Google Storage client: %v", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
stateMgr := &remote.State{Client: client}
|
st := &remote.State{Client: client}
|
||||||
lockInfo := state.NewLockInfo()
|
lockInfo := state.NewLockInfo()
|
||||||
lockInfo.Operation = "init"
|
lockInfo.Operation = "init"
|
||||||
lockId, err := stateMgr.Lock(lockInfo)
|
lockID, err := st.Lock(lockInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to lock state in Google Cloud Storage: %v", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Local helper function so we can call it multiple places
|
// Local helper function so we can call it multiple places
|
||||||
lockUnlock := func(parent error) error {
|
unlock := func(baseErr error) error {
|
||||||
if err := stateMgr.Unlock(lockId); err != nil {
|
if err := st.Unlock(lockID); err != nil {
|
||||||
return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, client.lockFileURL(), err)
|
const unlockErrMsg = `%v
|
||||||
|
Additionally, unlocking the state file on Google Cloud Storage failed:
|
||||||
|
|
||||||
|
Error message: %q
|
||||||
|
Lock ID (gen): %v
|
||||||
|
Lock file URL: %v
|
||||||
|
|
||||||
|
You may have to force-unlock this state in order to use it again.
|
||||||
|
The GCloud backend acquires a lock during initialization to ensure
|
||||||
|
the initial state file is created.`
|
||||||
|
return fmt.Errorf(unlockErrMsg, baseErr, err.Error(), lockID, client.lockFileURL())
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent
|
return baseErr
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grab the value
|
// Grab the value
|
||||||
if err := stateMgr.RefreshState(); err != nil {
|
if err := st.RefreshState(); err != nil {
|
||||||
err = lockUnlock(err)
|
return nil, unlock(err)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we have no state, we have to create an empty state
|
// If we have no state, we have to create an empty state
|
||||||
if v := stateMgr.State(); v == nil {
|
if v := st.State(); v == nil {
|
||||||
if err := stateMgr.WriteState(terraform.NewState()); err != nil {
|
if err := st.WriteState(terraform.NewState()); err != nil {
|
||||||
err = lockUnlock(err)
|
return nil, unlock(err)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
if err := stateMgr.PersistState(); err != nil {
|
if err := st.PersistState(); err != nil {
|
||||||
err = lockUnlock(err)
|
return nil, unlock(err)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlock, the state should now be initialized
|
// Unlock, the state should now be initialized
|
||||||
if err := lockUnlock(nil); err != nil {
|
if err := unlock(nil); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return stateMgr, nil
|
return st, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const errStateUnlock = `
|
|
||||||
Error unlocking Google Cloud Storage state.
|
|
||||||
|
|
||||||
Lock ID: %v
|
|
||||||
Lock file URL: %v
|
|
||||||
Error: %v
|
|
||||||
|
|
||||||
You may have to force-unlock this state in order to use it again.
|
|
||||||
The GCloud backend acquires a lock during initialization to ensure
|
|
||||||
the initial state file is created.
|
|
||||||
`
|
|
||||||
|
|
Loading…
Reference in New Issue