2017-07-19 04:07:24 -05:00
|
|
|
package gcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-09-08 02:25:20 -05:00
|
|
|
"path"
|
2017-09-07 06:10:56 -05:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
2017-07-19 04:07:24 -05:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"google.golang.org/api/iterator"
|
|
|
|
)
|
|
|
|
|
2017-09-08 02:35:09 -05:00
|
|
|
const (
|
|
|
|
stateFileSuffix = ".tfstate"
|
|
|
|
lockFileSuffix = ".tflock"
|
|
|
|
)
|
|
|
|
|
2017-09-08 02:25:20 -05:00
|
|
|
// States returns a list of names for the states found on GCS. The default
|
|
|
|
// state is always returned as the first element in the slice.
|
2017-07-19 04:07:24 -05:00
|
|
|
func (b *Backend) States() ([]string, error) {
|
2017-09-08 02:25:20 -05:00
|
|
|
states := []string{backend.DefaultStateName}
|
2017-07-19 04:07:24 -05:00
|
|
|
|
|
|
|
bucket := b.storageClient.Bucket(b.bucketName)
|
2017-09-08 02:25:20 -05:00
|
|
|
objs := bucket.Objects(b.storageContext, &storage.Query{
|
|
|
|
Delimiter: "/",
|
|
|
|
Prefix: b.stateDir,
|
|
|
|
})
|
2017-07-19 04:07:24 -05:00
|
|
|
for {
|
2017-09-08 02:25:20 -05:00
|
|
|
attrs, err := objs.Next()
|
2017-07-19 04:07:24 -05:00
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-09-08 02:25:20 -05:00
|
|
|
return nil, fmt.Errorf("querying Cloud Storage failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := path.Base(attrs.Name)
|
2017-09-08 02:35:09 -05:00
|
|
|
if !strings.HasSuffix(name, stateFileSuffix) {
|
2017-09-08 02:25:20 -05:00
|
|
|
continue
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
2017-09-08 02:35:09 -05:00
|
|
|
st := strings.TrimSuffix(name, stateFileSuffix)
|
2017-07-19 04:07:24 -05:00
|
|
|
|
2017-09-08 02:25:20 -05:00
|
|
|
if st != backend.DefaultStateName {
|
|
|
|
states = append(states, st)
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:25:20 -05:00
|
|
|
sort.Strings(states[1:])
|
|
|
|
return states, nil
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:30:48 -05:00
|
|
|
// DeleteState deletes the named state. The "default" state cannot be deleted.
|
2017-07-19 04:07:24 -05:00
|
|
|
func (b *Backend) DeleteState(name string) error {
|
2017-09-08 02:30:48 -05:00
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return fmt.Errorf("cowardly refusing to delete the %q state", name)
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := b.remoteClient(name)
|
|
|
|
if err != nil {
|
2017-09-08 02:30:48 -05:00
|
|
|
return err
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:30:48 -05:00
|
|
|
return client.Delete()
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:35:09 -05:00
|
|
|
// remoteClient returns a RemoteClient for the named state.
|
2017-07-19 04:07:24 -05:00
|
|
|
func (b *Backend) remoteClient(name string) (*RemoteClient, error) {
|
|
|
|
if name == "" {
|
2017-09-08 02:35:09 -05:00
|
|
|
return nil, fmt.Errorf("%q is not a valid state name", name)
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:35:09 -05:00
|
|
|
return &RemoteClient{
|
2017-07-19 04:07:24 -05:00
|
|
|
storageContext: b.storageContext,
|
|
|
|
storageClient: b.storageClient,
|
|
|
|
bucketName: b.bucketName,
|
2017-09-08 02:35:09 -05:00
|
|
|
stateFilePath: path.Join(b.stateDir, name+stateFileSuffix),
|
|
|
|
lockFilePath: path.Join(b.stateDir, name+lockFileSuffix),
|
|
|
|
}, nil
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) State(name string) (state.State, error) {
|
|
|
|
client, err := b.remoteClient(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to create Google Storage client: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stateMgr := &remote.State{Client: client}
|
|
|
|
lockInfo := state.NewLockInfo()
|
|
|
|
lockInfo.Operation = "init"
|
|
|
|
lockId, err := stateMgr.Lock(lockInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to lock state in Google Cloud Storage: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local helper function so we can call it multiple places
|
|
|
|
lockUnlock := func(parent error) error {
|
|
|
|
if err := stateMgr.Unlock(lockId); err != nil {
|
2017-09-07 05:56:17 -05:00
|
|
|
return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, client.lockFileURL(), err)
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return parent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the value
|
|
|
|
if err := stateMgr.RefreshState(); err != nil {
|
|
|
|
err = lockUnlock(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have no state, we have to create an empty state
|
|
|
|
if v := stateMgr.State(); v == nil {
|
|
|
|
if err := stateMgr.WriteState(terraform.NewState()); err != nil {
|
|
|
|
err = lockUnlock(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := stateMgr.PersistState(); err != nil {
|
|
|
|
err = lockUnlock(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock, the state should now be initialized
|
|
|
|
if err := lockUnlock(nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateMgr, 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.
|
|
|
|
`
|