2018-10-31 10:45:03 -05:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
tfe "github.com/hashicorp/go-tfe"
|
2020-08-11 10:43:01 -05:00
|
|
|
"github.com/hashicorp/terraform/states/remote"
|
2018-10-31 10:45:03 -05:00
|
|
|
"github.com/hashicorp/terraform/states/statefile"
|
2020-08-11 10:43:01 -05:00
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2018-10-31 10:45:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type remoteClient struct {
|
2019-04-29 13:19:23 -05:00
|
|
|
client *tfe.Client
|
2020-08-11 10:43:01 -05:00
|
|
|
lockInfo *statemgr.LockInfo
|
2019-04-29 13:19:23 -05:00
|
|
|
organization string
|
|
|
|
runID string
|
|
|
|
stateUploadErr bool
|
|
|
|
workspace *tfe.Workspace
|
2020-04-15 15:54:03 -05:00
|
|
|
forcePush bool
|
2018-10-31 10:45:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the remote state.
|
|
|
|
func (r *remoteClient) Get() (*remote.Payload, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-11-30 12:31:58 -06:00
|
|
|
sv, err := r.client.StateVersions.Current(ctx, r.workspace.ID)
|
2018-10-31 10:45:03 -05:00
|
|
|
if err != nil {
|
|
|
|
if err == tfe.ErrResourceNotFound {
|
|
|
|
// If no state exists, then return nil.
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-04-29 13:19:23 -05:00
|
|
|
return nil, fmt.Errorf("Error retrieving state: %v", err)
|
2018-10-31 10:45:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
state, err := r.client.StateVersions.Download(ctx, sv.DownloadURL)
|
|
|
|
if err != nil {
|
2019-04-29 13:19:23 -05:00
|
|
|
return nil, fmt.Errorf("Error downloading state: %v", err)
|
2018-10-31 10:45:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the state is empty, then return nil.
|
|
|
|
if len(state) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the MD5 checksum of the state.
|
|
|
|
sum := md5.Sum(state)
|
|
|
|
|
|
|
|
return &remote.Payload{
|
|
|
|
Data: state,
|
|
|
|
MD5: sum[:],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put the remote state.
|
|
|
|
func (r *remoteClient) Put(state []byte) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Read the raw state into a Terraform state.
|
|
|
|
stateFile, err := statefile.Read(bytes.NewReader(state))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading state: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
options := tfe.StateVersionCreateOptions{
|
|
|
|
Lineage: tfe.String(stateFile.Lineage),
|
|
|
|
Serial: tfe.Int64(int64(stateFile.Serial)),
|
|
|
|
MD5: tfe.String(fmt.Sprintf("%x", md5.Sum(state))),
|
|
|
|
State: tfe.String(base64.StdEncoding.EncodeToString(state)),
|
2020-04-15 15:54:03 -05:00
|
|
|
Force: tfe.Bool(r.forcePush),
|
2018-10-31 10:45:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a run ID, make sure to add it to the options
|
|
|
|
// so the state will be properly associated with the run.
|
|
|
|
if r.runID != "" {
|
|
|
|
options.Run = &tfe.Run{ID: r.runID}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new state.
|
2018-11-30 12:31:58 -06:00
|
|
|
_, err = r.client.StateVersions.Create(ctx, r.workspace.ID, options)
|
2018-10-31 10:45:03 -05:00
|
|
|
if err != nil {
|
2019-04-29 13:19:23 -05:00
|
|
|
r.stateUploadErr = true
|
|
|
|
return fmt.Errorf("Error uploading state: %v", err)
|
2018-10-31 10:45:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the remote state.
|
|
|
|
func (r *remoteClient) Delete() error {
|
2018-11-30 12:31:58 -06:00
|
|
|
err := r.client.Workspaces.Delete(context.Background(), r.organization, r.workspace.Name)
|
2018-10-31 10:45:03 -05:00
|
|
|
if err != nil && err != tfe.ErrResourceNotFound {
|
2018-11-30 12:31:58 -06:00
|
|
|
return fmt.Errorf("Error deleting workspace %s: %v", r.workspace.Name, err)
|
2018-10-31 10:45:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:54:03 -05:00
|
|
|
// EnableForcePush to allow the remote client to overwrite state
|
|
|
|
// by implementing remote.ClientForcePusher
|
|
|
|
func (r *remoteClient) EnableForcePush() {
|
|
|
|
r.forcePush = true
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:45:03 -05:00
|
|
|
// Lock the remote state.
|
2020-08-11 10:43:01 -05:00
|
|
|
func (r *remoteClient) Lock(info *statemgr.LockInfo) (string, error) {
|
2018-10-31 10:45:03 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-08-11 10:43:01 -05:00
|
|
|
lockErr := &statemgr.LockError{Info: r.lockInfo}
|
2018-10-31 10:45:03 -05:00
|
|
|
|
|
|
|
// Lock the workspace.
|
2018-11-30 12:31:58 -06:00
|
|
|
_, err := r.client.Workspaces.Lock(ctx, r.workspace.ID, tfe.WorkspaceLockOptions{
|
2018-10-31 10:45:03 -05:00
|
|
|
Reason: tfe.String("Locked by Terraform"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-04-29 13:19:23 -05:00
|
|
|
if err == tfe.ErrWorkspaceLocked {
|
2021-02-19 14:47:18 -06:00
|
|
|
lockErr.Info = info
|
2019-04-29 13:19:23 -05:00
|
|
|
err = fmt.Errorf("%s (lock ID: \"%s/%s\")", err, r.organization, r.workspace.Name)
|
|
|
|
}
|
2018-10-31 10:45:03 -05:00
|
|
|
lockErr.Err = err
|
|
|
|
return "", lockErr
|
|
|
|
}
|
|
|
|
|
|
|
|
r.lockInfo = info
|
|
|
|
|
|
|
|
return r.lockInfo.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock the remote state.
|
|
|
|
func (r *remoteClient) Unlock(id string) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-04-29 13:19:23 -05:00
|
|
|
// We first check if there was an error while uploading the latest
|
|
|
|
// state. If so, we will not unlock the workspace to prevent any
|
|
|
|
// changes from being applied until the correct state is uploaded.
|
|
|
|
if r.stateUploadErr {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 10:43:01 -05:00
|
|
|
lockErr := &statemgr.LockError{Info: r.lockInfo}
|
2018-10-31 10:45:03 -05:00
|
|
|
|
2018-11-30 12:31:58 -06:00
|
|
|
// With lock info this should be treated as a normal unlock.
|
|
|
|
if r.lockInfo != nil {
|
|
|
|
// Verify the expected lock ID.
|
|
|
|
if r.lockInfo.ID != id {
|
|
|
|
lockErr.Err = fmt.Errorf("lock ID does not match existing lock")
|
|
|
|
return lockErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock the workspace.
|
|
|
|
_, err := r.client.Workspaces.Unlock(ctx, r.workspace.ID)
|
|
|
|
if err != nil {
|
|
|
|
lockErr.Err = err
|
|
|
|
return lockErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-10-31 10:45:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the optional force-unlock lock ID.
|
2018-11-30 12:31:58 -06:00
|
|
|
if r.organization+"/"+r.workspace.Name != id {
|
2019-04-29 13:19:23 -05:00
|
|
|
lockErr.Err = fmt.Errorf(
|
|
|
|
"lock ID %q does not match existing lock ID \"%s/%s\"",
|
|
|
|
id,
|
|
|
|
r.organization,
|
|
|
|
r.workspace.Name,
|
|
|
|
)
|
2018-10-31 10:45:03 -05:00
|
|
|
return lockErr
|
|
|
|
}
|
|
|
|
|
2018-11-30 12:31:58 -06:00
|
|
|
// Force unlock the workspace.
|
|
|
|
_, err := r.client.Workspaces.ForceUnlock(ctx, r.workspace.ID)
|
2018-10-31 10:45:03 -05:00
|
|
|
if err != nil {
|
|
|
|
lockErr.Err = err
|
|
|
|
return lockErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|