backend/remote-state/gcloud: Make remoteClient private.

This class is only used via the "state/remote".State interface, so there
is no need to export this type beyond the gcloud package.
This commit is contained in:
Florian Forster 2017-09-08 13:36:40 +02:00 committed by James Bardin
parent 42e8441a2b
commit 9ec39573ee
3 changed files with 24 additions and 21 deletions

View File

@ -60,7 +60,7 @@ func (b *Backend) configure(ctx context.Context) error {
} }
// ctx is a background context with the backend config added. // ctx is a background context with the backend config added.
// Since no context is passed to RemoteClient.Get(), .Lock(), etc. but // Since no context is passed to remoteClient.Get(), .Lock(), etc. but
// one is required for calling the GCP API, we're holding on to this // one is required for calling the GCP API, we're holding on to this
// context here and re-use it later. // context here and re-use it later.
b.storageContext = ctx b.storageContext = ctx

View File

@ -59,21 +59,21 @@ func (b *Backend) DeleteState(name string) error {
return fmt.Errorf("cowardly refusing to delete the %q state", name) return fmt.Errorf("cowardly refusing to delete the %q state", name)
} }
client, err := b.remoteClient(name) c, err := b.client(name)
if err != nil { if err != nil {
return err return err
} }
return client.Delete() return c.Delete()
} }
// remoteClient returns a RemoteClient for the named state. // client returns a remoteClient for the named state.
func (b *Backend) remoteClient(name string) (*RemoteClient, error) { func (b *Backend) client(name string) (*remoteClient, error) {
if name == "" { if name == "" {
return nil, fmt.Errorf("%q is not a valid state name", name) return nil, fmt.Errorf("%q is not a valid state name", name)
} }
return &RemoteClient{ return &remoteClient{
storageContext: b.storageContext, storageContext: b.storageContext,
storageClient: b.storageClient, storageClient: b.storageClient,
bucketName: b.bucketName, bucketName: b.bucketName,
@ -85,12 +85,12 @@ func (b *Backend) remoteClient(name string) (*RemoteClient, error) {
// State reads and returns the named state from GCS. If the named state does // State reads and returns the named state from GCS. If the named state does
// not yet exist, a new state file is created. // 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) c, err := b.client(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
st := &remote.State{Client: client} st := &remote.State{Client: c}
lockInfo := state.NewLockInfo() lockInfo := state.NewLockInfo()
lockInfo.Operation = "init" lockInfo.Operation = "init"
lockID, err := st.Lock(lockInfo) lockID, err := st.Lock(lockInfo)
@ -111,7 +111,7 @@ Additionally, unlocking the state file on Google Cloud Storage failed:
You may have to force-unlock this state in order to use it again. 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 GCloud backend acquires a lock during initialization to ensure
the initial state file is created.` the initial state file is created.`
return fmt.Errorf(unlockErrMsg, baseErr, err.Error(), lockID, client.lockFileURL()) return fmt.Errorf(unlockErrMsg, baseErr, err.Error(), lockID, c.lockFileURL())
} }
return baseErr return baseErr

View File

@ -13,7 +13,10 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
) )
type RemoteClient struct { // remoteClient is used by "state/remote".State to read and write
// blobs representing state.
// Implements "state/remote".ClientLocker
type remoteClient struct {
storageContext context.Context storageContext context.Context
storageClient *storage.Client storageClient *storage.Client
bucketName string bucketName string
@ -21,7 +24,7 @@ type RemoteClient struct {
lockFilePath string lockFilePath string
} }
func (c *RemoteClient) Get() (payload *remote.Payload, err error) { func (c *remoteClient) Get() (payload *remote.Payload, err error) {
stateFileReader, err := c.stateFile().NewReader(c.storageContext) stateFileReader, err := c.stateFile().NewReader(c.storageContext)
if err != nil { if err != nil {
if err == storage.ErrObjectNotExist { if err == storage.ErrObjectNotExist {
@ -50,7 +53,7 @@ func (c *RemoteClient) Get() (payload *remote.Payload, err error) {
return result, nil return result, nil
} }
func (c *RemoteClient) Put(data []byte) error { func (c *remoteClient) Put(data []byte) error {
err := func() error { err := func() error {
stateFileWriter := c.stateFile().NewWriter(c.storageContext) stateFileWriter := c.stateFile().NewWriter(c.storageContext)
if _, err := stateFileWriter.Write(data); err != nil { if _, err := stateFileWriter.Write(data); err != nil {
@ -65,7 +68,7 @@ func (c *RemoteClient) Put(data []byte) error {
return nil return nil
} }
func (c *RemoteClient) Delete() error { func (c *remoteClient) Delete() error {
if err := c.stateFile().Delete(c.storageContext); err != nil { if err := c.stateFile().Delete(c.storageContext); err != nil {
return fmt.Errorf("Failed to delete state file %v: %v", c.stateFileURL(), err) return fmt.Errorf("Failed to delete state file %v: %v", c.stateFileURL(), err)
} }
@ -75,7 +78,7 @@ func (c *RemoteClient) Delete() error {
// Lock writes to a lock file, ensuring file creation. Returns the generation // Lock writes to a lock file, ensuring file creation. Returns the generation
// number, which must be passed to Unlock(). // number, which must be passed to Unlock().
func (c *RemoteClient) Lock(info *state.LockInfo) (string, error) { func (c *remoteClient) Lock(info *state.LockInfo) (string, error) {
infoJson, err := json.Marshal(info) infoJson, err := json.Marshal(info)
if err != nil { if err != nil {
return "", err return "", err
@ -99,7 +102,7 @@ func (c *RemoteClient) Lock(info *state.LockInfo) (string, error) {
return info.ID, nil return info.ID, nil
} }
func (c *RemoteClient) Unlock(id string) error { func (c *remoteClient) Unlock(id string) error {
gen, err := strconv.ParseInt(id, 10, 64) gen, err := strconv.ParseInt(id, 10, 64)
if err != nil { if err != nil {
return err return err
@ -112,7 +115,7 @@ func (c *RemoteClient) Unlock(id string) error {
return nil return nil
} }
func (c *RemoteClient) lockError(err error) *state.LockError { func (c *remoteClient) lockError(err error) *state.LockError {
lockErr := &state.LockError{ lockErr := &state.LockError{
Err: err, Err: err,
} }
@ -128,7 +131,7 @@ func (c *RemoteClient) lockError(err error) *state.LockError {
// lockInfo reads the lock file, parses its contents and returns the parsed // lockInfo reads the lock file, parses its contents and returns the parsed
// LockInfo struct. // LockInfo struct.
func (c *RemoteClient) lockInfo() (*state.LockInfo, error) { func (c *remoteClient) lockInfo() (*state.LockInfo, error) {
r, err := c.lockFile().NewReader(c.storageContext) r, err := c.lockFile().NewReader(c.storageContext)
if err != nil { if err != nil {
return nil, err return nil, err
@ -148,18 +151,18 @@ func (c *RemoteClient) lockInfo() (*state.LockInfo, error) {
return info, nil return info, nil
} }
func (c *RemoteClient) stateFile() *storage.ObjectHandle { func (c *remoteClient) stateFile() *storage.ObjectHandle {
return c.storageClient.Bucket(c.bucketName).Object(c.stateFilePath) return c.storageClient.Bucket(c.bucketName).Object(c.stateFilePath)
} }
func (c *RemoteClient) stateFileURL() string { func (c *remoteClient) stateFileURL() string {
return fmt.Sprintf("gs://%v/%v", c.bucketName, c.stateFilePath) return fmt.Sprintf("gs://%v/%v", c.bucketName, c.stateFilePath)
} }
func (c *RemoteClient) lockFile() *storage.ObjectHandle { func (c *remoteClient) lockFile() *storage.ObjectHandle {
return c.storageClient.Bucket(c.bucketName).Object(c.lockFilePath) return c.storageClient.Bucket(c.bucketName).Object(c.lockFilePath)
} }
func (c *RemoteClient) lockFileURL() string { func (c *remoteClient) lockFileURL() string {
return fmt.Sprintf("gs://%v/%v", c.bucketName, c.lockFilePath) return fmt.Sprintf("gs://%v/%v", c.bucketName, c.lockFilePath)
} }