2017-09-08 09:11:41 -05:00
|
|
|
package gcs
|
2017-07-19 04:07:24 -05:00
|
|
|
|
|
|
|
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-09-08 06:50:07 -05:00
|
|
|
func (b *gcsBackend) 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: "/",
|
2017-09-11 01:25:42 -05:00
|
|
|
Prefix: b.prefix,
|
2017-09-08 02:25:20 -05:00
|
|
|
})
|
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-09-08 06:50:07 -05:00
|
|
|
func (b *gcsBackend) 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
|
|
|
}
|
|
|
|
|
2017-09-08 06:36:40 -05:00
|
|
|
c, err := b.client(name)
|
2017-07-19 04:07:24 -05:00
|
|
|
if err != nil {
|
2017-09-08 02:30:48 -05:00
|
|
|
return err
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-09-08 06:36:40 -05:00
|
|
|
return c.Delete()
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-09-08 06:36:40 -05:00
|
|
|
// client returns a remoteClient for the named state.
|
2017-09-08 06:50:07 -05:00
|
|
|
func (b *gcsBackend) client(name string) (*remoteClient, error) {
|
2017-07-19 04:07:24 -05:00
|
|
|
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 06:36:40 -05:00
|
|
|
return &remoteClient{
|
2017-07-19 04:07:24 -05:00
|
|
|
storageContext: b.storageContext,
|
|
|
|
storageClient: b.storageClient,
|
|
|
|
bucketName: b.bucketName,
|
2017-09-08 07:32:00 -05:00
|
|
|
stateFilePath: b.stateFile(name),
|
|
|
|
lockFilePath: b.lockFile(name),
|
2017-12-17 18:59:10 -06:00
|
|
|
encryptionKey: b.encryptionKey,
|
2017-09-08 02:35:09 -05:00
|
|
|
}, nil
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-09-08 03:36:28 -05:00
|
|
|
// State reads and returns the named state from GCS. If the named state does
|
|
|
|
// not yet exist, a new state file is created.
|
2017-09-08 06:50:07 -05:00
|
|
|
func (b *gcsBackend) State(name string) (state.State, error) {
|
2017-09-08 06:36:40 -05:00
|
|
|
c, err := b.client(name)
|
2017-07-19 04:07:24 -05:00
|
|
|
if err != nil {
|
2017-09-08 03:36:28 -05:00
|
|
|
return nil, err
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-09-08 06:36:40 -05:00
|
|
|
st := &remote.State{Client: c}
|
2017-12-06 11:36:16 -06:00
|
|
|
|
|
|
|
// Grab the value
|
|
|
|
if err := st.RefreshState(); err != nil {
|
2017-09-08 03:36:28 -05:00
|
|
|
return nil, err
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-12-06 11:36:16 -06:00
|
|
|
// If we have no state, we have to create an empty state
|
|
|
|
if v := st.State(); v == nil {
|
2017-09-08 03:36:28 -05:00
|
|
|
|
2017-12-06 11:36:16 -06:00
|
|
|
lockInfo := state.NewLockInfo()
|
|
|
|
lockInfo.Operation = "init"
|
|
|
|
lockID, err := st.Lock(lockInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-12-06 11:36:16 -06:00
|
|
|
// Local helper function so we can call it multiple places
|
|
|
|
unlock := func(baseErr error) error {
|
|
|
|
if err := st.Unlock(lockID); err != nil {
|
|
|
|
const unlockErrMsg = `%v
|
|
|
|
Additionally, unlocking the state file on Google Cloud Storage failed:
|
2017-07-19 04:07:24 -05:00
|
|
|
|
2017-12-06 11:36:16 -06:00
|
|
|
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, c.lockFileURL())
|
|
|
|
}
|
|
|
|
|
|
|
|
return baseErr
|
|
|
|
}
|
2017-07-19 04:07:24 -05:00
|
|
|
|
2017-09-08 03:36:28 -05:00
|
|
|
if err := st.WriteState(terraform.NewState()); err != nil {
|
|
|
|
return nil, unlock(err)
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
2017-09-08 03:36:28 -05:00
|
|
|
if err := st.PersistState(); err != nil {
|
|
|
|
return nil, unlock(err)
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-12-06 11:36:16 -06:00
|
|
|
// Unlock, the state should now be initialized
|
|
|
|
if err := unlock(nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
|
|
|
|
2017-09-08 03:36:28 -05:00
|
|
|
return st, nil
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
2017-09-08 07:32:00 -05:00
|
|
|
|
|
|
|
func (b *gcsBackend) stateFile(name string) string {
|
|
|
|
if name == backend.DefaultStateName && b.defaultStateFile != "" {
|
|
|
|
return b.defaultStateFile
|
|
|
|
}
|
2017-09-11 01:25:42 -05:00
|
|
|
return path.Join(b.prefix, name+stateFileSuffix)
|
2017-09-08 07:32:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *gcsBackend) lockFile(name string) string {
|
|
|
|
if name == backend.DefaultStateName && b.defaultStateFile != "" {
|
|
|
|
return strings.TrimSuffix(b.defaultStateFile, stateFileSuffix) + lockFileSuffix
|
|
|
|
}
|
2017-09-11 01:25:42 -05:00
|
|
|
return path.Join(b.prefix, name+lockFileSuffix)
|
2017-09-08 07:32:00 -05:00
|
|
|
}
|