2017-09-08 16:11:41 +02:00
|
|
|
package gcs
|
2017-07-19 11:07:24 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-09-07 13:10:56 +02:00
|
|
|
"io/ioutil"
|
2017-09-07 12:10:28 +02:00
|
|
|
"strconv"
|
2017-09-07 13:10:56 +02:00
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
2017-09-07 12:10:28 +02:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states/remote"
|
|
|
|
"github.com/hashicorp/terraform/internal/states/statemgr"
|
2017-07-19 11:07:24 +02:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
// remoteClient is used by "state/remote".State to read and write
|
|
|
|
// blobs representing state.
|
|
|
|
// Implements "state/remote".ClientLocker
|
|
|
|
type remoteClient struct {
|
2017-07-19 11:07:24 +02:00
|
|
|
storageContext context.Context
|
|
|
|
storageClient *storage.Client
|
|
|
|
bucketName string
|
|
|
|
stateFilePath string
|
|
|
|
lockFilePath string
|
2017-12-18 01:59:10 +01:00
|
|
|
encryptionKey []byte
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
func (c *remoteClient) Get() (payload *remote.Payload, err error) {
|
2017-09-07 13:16:53 +02:00
|
|
|
stateFileReader, err := c.stateFile().NewReader(c.storageContext)
|
2017-07-19 11:07:24 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == storage.ErrObjectNotExist {
|
|
|
|
return nil, nil
|
|
|
|
} else {
|
2017-09-07 13:16:53 +02:00
|
|
|
return nil, fmt.Errorf("Failed to open state file at %v: %v", c.stateFileURL(), err)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
defer stateFileReader.Close()
|
|
|
|
|
|
|
|
stateFileContents, err := ioutil.ReadAll(stateFileReader)
|
|
|
|
if err != nil {
|
2017-09-07 13:16:53 +02:00
|
|
|
return nil, fmt.Errorf("Failed to read state file from %v: %v", c.stateFileURL(), err)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-07 13:16:53 +02:00
|
|
|
stateFileAttrs, err := c.stateFile().Attrs(c.storageContext)
|
2017-07-19 11:07:24 +02:00
|
|
|
if err != nil {
|
2017-09-07 13:16:53 +02:00
|
|
|
return nil, fmt.Errorf("Failed to read state file attrs from %v: %v", c.stateFileURL(), err)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result := &remote.Payload{
|
|
|
|
Data: stateFileContents,
|
|
|
|
MD5: stateFileAttrs.MD5,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
func (c *remoteClient) Put(data []byte) error {
|
2017-09-07 13:52:51 +02:00
|
|
|
err := func() error {
|
|
|
|
stateFileWriter := c.stateFile().NewWriter(c.storageContext)
|
|
|
|
if _, err := stateFileWriter.Write(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return stateFileWriter.Close()
|
|
|
|
}()
|
2017-07-19 11:07:24 +02:00
|
|
|
if err != nil {
|
2017-09-07 12:56:17 +02:00
|
|
|
return fmt.Errorf("Failed to upload state to %v: %v", c.stateFileURL(), err)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
func (c *remoteClient) Delete() error {
|
2017-09-07 13:54:12 +02:00
|
|
|
if err := c.stateFile().Delete(c.storageContext); err != nil {
|
2017-09-07 12:56:17 +02:00
|
|
|
return fmt.Errorf("Failed to delete state file %v: %v", c.stateFileURL(), err)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-07 12:10:28 +02:00
|
|
|
// Lock writes to a lock file, ensuring file creation. Returns the generation
|
|
|
|
// number, which must be passed to Unlock().
|
2020-08-11 17:43:01 +02:00
|
|
|
func (c *remoteClient) Lock(info *statemgr.LockInfo) (string, error) {
|
2018-02-21 02:54:01 +01:00
|
|
|
// update the path we're using
|
|
|
|
// we can't set the ID until the info is written
|
|
|
|
info.Path = c.lockFileURL()
|
|
|
|
|
2017-07-19 11:07:24 +02:00
|
|
|
infoJson, err := json.Marshal(info)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-09-07 12:10:28 +02:00
|
|
|
lockFile := c.lockFile()
|
|
|
|
w := lockFile.If(storage.Conditions{DoesNotExist: true}).NewWriter(c.storageContext)
|
|
|
|
err = func() error {
|
|
|
|
if _, err := w.Write(infoJson); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return w.Close()
|
|
|
|
}()
|
2018-02-21 02:54:01 +01:00
|
|
|
|
2017-09-07 12:10:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", c.lockError(fmt.Errorf("writing %q failed: %v", c.lockFileURL(), err))
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-07 12:10:28 +02:00
|
|
|
info.ID = strconv.FormatInt(w.Attrs().Generation, 10)
|
|
|
|
|
2017-07-19 11:07:24 +02:00
|
|
|
return info.ID, nil
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
func (c *remoteClient) Unlock(id string) error {
|
2017-09-07 12:10:28 +02:00
|
|
|
gen, err := strconv.ParseInt(id, 10, 64)
|
2017-07-19 11:07:24 +02:00
|
|
|
if err != nil {
|
2019-05-28 05:07:14 +02:00
|
|
|
return fmt.Errorf("Lock ID should be numerical value, got '%s'", id)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-07 12:10:28 +02:00
|
|
|
if err := c.lockFile().If(storage.Conditions{GenerationMatch: gen}).Delete(c.storageContext); err != nil {
|
|
|
|
return c.lockError(err)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-07 12:10:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-07-19 11:07:24 +02:00
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
func (c *remoteClient) lockError(err error) *statemgr.LockError {
|
|
|
|
lockErr := &statemgr.LockError{
|
2017-09-07 12:10:28 +02:00
|
|
|
Err: err,
|
|
|
|
}
|
2017-07-19 11:07:24 +02:00
|
|
|
|
2017-09-07 12:10:28 +02:00
|
|
|
info, infoErr := c.lockInfo()
|
|
|
|
if infoErr != nil {
|
|
|
|
lockErr.Err = multierror.Append(lockErr.Err, infoErr)
|
|
|
|
} else {
|
|
|
|
lockErr.Info = info
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
2017-09-07 12:10:28 +02:00
|
|
|
return lockErr
|
|
|
|
}
|
2017-07-19 11:07:24 +02:00
|
|
|
|
2017-09-07 12:10:28 +02:00
|
|
|
// lockInfo reads the lock file, parses its contents and returns the parsed
|
|
|
|
// LockInfo struct.
|
2020-08-11 17:43:01 +02:00
|
|
|
func (c *remoteClient) lockInfo() (*statemgr.LockInfo, error) {
|
2017-09-07 12:10:28 +02:00
|
|
|
r, err := c.lockFile().NewReader(c.storageContext)
|
2017-07-19 11:07:24 +02:00
|
|
|
if err != nil {
|
2017-09-07 12:10:28 +02:00
|
|
|
return nil, err
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
2017-09-07 12:10:28 +02:00
|
|
|
defer r.Close()
|
2017-07-19 11:07:24 +02:00
|
|
|
|
2017-09-07 12:10:28 +02:00
|
|
|
rawData, err := ioutil.ReadAll(r)
|
2017-07-19 11:07:24 +02:00
|
|
|
if err != nil {
|
2017-09-07 12:10:28 +02:00
|
|
|
return nil, err
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
info := &statemgr.LockInfo{}
|
2017-09-07 12:10:28 +02:00
|
|
|
if err := json.Unmarshal(rawData, info); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-02-21 02:54:01 +01:00
|
|
|
// We use the Generation as the ID, so overwrite the ID in the json.
|
|
|
|
// This can't be written into the Info, since the generation isn't known
|
|
|
|
// until it's written.
|
|
|
|
attrs, err := c.lockFile().Attrs(c.storageContext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info.ID = strconv.FormatInt(attrs.Generation, 10)
|
|
|
|
|
2017-09-07 12:10:28 +02:00
|
|
|
return info, nil
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
func (c *remoteClient) stateFile() *storage.ObjectHandle {
|
2017-12-18 01:59:10 +01:00
|
|
|
h := c.storageClient.Bucket(c.bucketName).Object(c.stateFilePath)
|
|
|
|
if len(c.encryptionKey) > 0 {
|
|
|
|
return h.Key(c.encryptionKey)
|
|
|
|
}
|
|
|
|
return h
|
2017-09-07 13:16:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
func (c *remoteClient) stateFileURL() string {
|
2017-07-19 11:07:24 +02:00
|
|
|
return fmt.Sprintf("gs://%v/%v", c.bucketName, c.stateFilePath)
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
func (c *remoteClient) lockFile() *storage.ObjectHandle {
|
2017-09-07 13:16:53 +02:00
|
|
|
return c.storageClient.Bucket(c.bucketName).Object(c.lockFilePath)
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
func (c *remoteClient) lockFileURL() string {
|
2017-07-19 11:07:24 +02:00
|
|
|
return fmt.Sprintf("gs://%v/%v", c.bucketName, c.lockFilePath)
|
|
|
|
}
|