2017-03-21 18:43:31 +01:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2017-04-12 19:30:49 +02:00
|
|
|
"errors"
|
2017-03-22 20:52:55 +01:00
|
|
|
"fmt"
|
2017-03-22 21:33:41 +01:00
|
|
|
"sort"
|
2017-03-22 20:52:55 +01:00
|
|
|
"strings"
|
|
|
|
|
2017-03-22 21:33:41 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2019-01-09 19:01:37 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2017-03-22 21:33:41 +01:00
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
|
2017-03-21 18:43:31 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2017-03-21 18:43:31 +01:00
|
|
|
)
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
func (b *Backend) Workspaces() ([]string, error) {
|
2017-12-19 19:14:31 +01:00
|
|
|
prefix := b.workspaceKeyPrefix + "/"
|
|
|
|
|
|
|
|
// List bucket root if there is no workspaceKeyPrefix
|
|
|
|
if b.workspaceKeyPrefix == "" {
|
|
|
|
prefix = ""
|
|
|
|
}
|
2017-03-22 21:33:41 +01:00
|
|
|
params := &s3.ListObjectsInput{
|
|
|
|
Bucket: &b.bucketName,
|
2017-12-19 19:14:31 +01:00
|
|
|
Prefix: aws.String(prefix),
|
2017-03-22 21:33:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := b.s3Client.ListObjects(params)
|
|
|
|
if err != nil {
|
2019-01-09 19:01:37 +01:00
|
|
|
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == s3.ErrCodeNoSuchBucket {
|
|
|
|
return nil, fmt.Errorf(errS3NoSuchBucket, err)
|
|
|
|
}
|
2017-03-22 21:33:41 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-16 03:04:15 +01:00
|
|
|
wss := []string{backend.DefaultStateName}
|
2017-03-22 21:33:41 +01:00
|
|
|
for _, obj := range resp.Contents {
|
2017-12-19 19:14:31 +01:00
|
|
|
ws := b.keyEnv(*obj.Key)
|
2017-12-16 03:04:15 +01:00
|
|
|
if ws != "" {
|
|
|
|
wss = append(wss, ws)
|
2017-03-22 21:33:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 03:04:15 +01:00
|
|
|
sort.Strings(wss[1:])
|
|
|
|
return wss, nil
|
2017-03-22 21:33:41 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 19:14:31 +01:00
|
|
|
func (b *Backend) keyEnv(key string) string {
|
2017-12-15 23:50:36 +01:00
|
|
|
if b.workspaceKeyPrefix == "" {
|
2017-12-19 15:31:53 +01:00
|
|
|
parts := strings.SplitN(key, "/", 2)
|
|
|
|
if len(parts) > 1 && parts[1] == b.keyName {
|
2017-12-16 03:04:15 +01:00
|
|
|
return parts[0]
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
2017-12-15 23:50:36 +01:00
|
|
|
}
|
2017-12-16 03:04:15 +01:00
|
|
|
|
2018-01-11 15:33:20 +01:00
|
|
|
parts := strings.SplitAfterN(key, b.workspaceKeyPrefix, 2)
|
2017-12-15 23:50:36 +01:00
|
|
|
|
|
|
|
if len(parts) < 2 {
|
2017-03-22 21:33:41 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-04-12 19:30:49 +02:00
|
|
|
// shouldn't happen since we listed by prefix
|
2017-06-22 19:17:37 +02:00
|
|
|
if parts[0] != b.workspaceKeyPrefix {
|
2017-04-12 19:30:49 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-12-18 22:24:34 +01:00
|
|
|
parts = strings.SplitN(parts[1], "/", 3)
|
2017-12-15 23:50:36 +01:00
|
|
|
|
|
|
|
if len(parts) < 3 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-04-12 19:30:49 +02:00
|
|
|
// not our key, so don't include it in our listing
|
|
|
|
if parts[2] != b.keyName {
|
2017-03-22 21:33:41 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts[1]
|
2017-03-21 18:43:31 +01:00
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
func (b *Backend) DeleteWorkspace(name string) error {
|
2017-03-22 20:52:55 +01:00
|
|
|
if name == backend.DefaultStateName || name == "" {
|
|
|
|
return fmt.Errorf("can't delete default state")
|
|
|
|
}
|
|
|
|
|
2017-06-23 16:19:50 +02:00
|
|
|
client, err := b.remoteClient(name)
|
2017-03-22 21:33:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-23 16:19:50 +02:00
|
|
|
return client.Delete()
|
2017-03-21 18:43:31 +01:00
|
|
|
}
|
|
|
|
|
2017-06-23 16:19:50 +02:00
|
|
|
// get a remote client configured for this state
|
|
|
|
func (b *Backend) remoteClient(name string) (*RemoteClient, error) {
|
2017-04-12 19:30:49 +02:00
|
|
|
if name == "" {
|
|
|
|
return nil, errors.New("missing state name")
|
|
|
|
}
|
|
|
|
|
2017-03-22 20:52:55 +01:00
|
|
|
client := &RemoteClient{
|
|
|
|
s3Client: b.s3Client,
|
|
|
|
dynClient: b.dynClient,
|
|
|
|
bucketName: b.bucketName,
|
|
|
|
path: b.path(name),
|
|
|
|
serverSideEncryption: b.serverSideEncryption,
|
|
|
|
acl: b.acl,
|
|
|
|
kmsKeyID: b.kmsKeyID,
|
2017-05-26 01:12:20 +02:00
|
|
|
ddbTable: b.ddbTable,
|
2017-03-22 20:52:55 +01:00
|
|
|
}
|
|
|
|
|
2017-06-23 16:19:50 +02:00
|
|
|
return client, nil
|
|
|
|
}
|
2017-03-22 21:45:21 +01:00
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
func (b *Backend) StateMgr(name string) (state.State, error) {
|
2017-06-23 16:19:50 +02:00
|
|
|
client, err := b.remoteClient(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stateMgr := &remote.State{Client: client}
|
2017-05-19 20:40:59 +02:00
|
|
|
// Check to see if this state already exists.
|
|
|
|
// If we're trying to force-unlock a state, we can't take the lock before
|
|
|
|
// fetching the state. If the state doesn't exist, we have to assume this
|
|
|
|
// is a normal create operation, and take the lock at that point.
|
|
|
|
//
|
|
|
|
// If we need to force-unlock, but for some reason the state no longer
|
|
|
|
// exists, the user will have to use aws tools to manually fix the
|
|
|
|
// situation.
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
existing, err := b.Workspaces()
|
2017-05-19 20:40:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exists := false
|
|
|
|
for _, s := range existing {
|
|
|
|
if s == name {
|
|
|
|
exists = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to create the object so it's listed by States.
|
|
|
|
if !exists {
|
2017-03-22 21:45:21 +01:00
|
|
|
// take a lock on this state while we write it
|
|
|
|
lockInfo := state.NewLockInfo()
|
|
|
|
lockInfo.Operation = "init"
|
|
|
|
lockId, err := client.Lock(lockInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to lock s3 state: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local helper function so we can call it multiple places
|
|
|
|
lockUnlock := func(parent error) error {
|
|
|
|
if err := stateMgr.Unlock(lockId); err != nil {
|
|
|
|
return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err)
|
|
|
|
}
|
|
|
|
return parent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the value
|
2017-05-19 20:40:59 +02:00
|
|
|
// This is to ensure that no one beat us to writing a state between
|
|
|
|
// the `exists` check and taking the lock.
|
2017-03-22 21:45:21 +01:00
|
|
|
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 {
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
if err := stateMgr.WriteState(states.NewState()); err != nil {
|
2017-03-22 21:45:21 +01:00
|
|
|
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 {
|
2017-03-22 21:33:41 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-22 21:45:21 +01:00
|
|
|
|
2017-03-22 21:33:41 +01:00
|
|
|
}
|
2017-03-22 20:52:55 +01:00
|
|
|
|
2017-03-22 21:45:21 +01:00
|
|
|
return stateMgr, nil
|
2017-03-22 20:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) client() *RemoteClient {
|
|
|
|
return &RemoteClient{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Backend) path(name string) string {
|
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return b.keyName
|
|
|
|
}
|
|
|
|
|
2017-12-19 19:14:31 +01:00
|
|
|
if b.workspaceKeyPrefix != "" {
|
|
|
|
return strings.Join([]string{b.workspaceKeyPrefix, name, b.keyName}, "/")
|
|
|
|
} else {
|
|
|
|
// Trim the leading / for no workspace prefix
|
|
|
|
return strings.Join([]string{b.workspaceKeyPrefix, name, b.keyName}, "/")[1:]
|
|
|
|
}
|
2017-03-21 18:43:31 +01:00
|
|
|
}
|
2017-03-22 21:45:21 +01:00
|
|
|
|
|
|
|
const errStateUnlock = `
|
|
|
|
Error unlocking S3 state. Lock ID: %s
|
|
|
|
|
|
|
|
Error: %s
|
|
|
|
|
|
|
|
You may have to force-unlock this state in order to use it again.
|
|
|
|
`
|