2017-09-08 16:11:41 +02:00
|
|
|
package gcs
|
2017-07-19 11:07:24 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-09-08 09:25:20 +02:00
|
|
|
"path"
|
2017-09-07 13:10:56 +02:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
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
|
|
|
"google.golang.org/api/iterator"
|
|
|
|
|
2021-05-17 17:42:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
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"
|
2020-08-11 17:43:01 +02:00
|
|
|
"github.com/hashicorp/terraform/states/remote"
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2017-07-19 11:07:24 +02:00
|
|
|
)
|
|
|
|
|
2017-09-08 09:35:09 +02:00
|
|
|
const (
|
|
|
|
stateFileSuffix = ".tfstate"
|
|
|
|
lockFileSuffix = ".tflock"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
// Workspaces returns a list of names for the workspaces found on GCS. The default
|
2017-09-08 09:25:20 +02:00
|
|
|
// state is always returned as the first element in the slice.
|
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-09-08 09:25:20 +02:00
|
|
|
states := []string{backend.DefaultStateName}
|
2017-07-19 11:07:24 +02:00
|
|
|
|
|
|
|
bucket := b.storageClient.Bucket(b.bucketName)
|
2017-09-08 09:25:20 +02:00
|
|
|
objs := bucket.Objects(b.storageContext, &storage.Query{
|
|
|
|
Delimiter: "/",
|
2017-09-11 08:25:42 +02:00
|
|
|
Prefix: b.prefix,
|
2017-09-08 09:25:20 +02:00
|
|
|
})
|
2017-07-19 11:07:24 +02:00
|
|
|
for {
|
2017-09-08 09:25:20 +02:00
|
|
|
attrs, err := objs.Next()
|
2017-07-19 11:07:24 +02:00
|
|
|
if err == iterator.Done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-09-08 09:25:20 +02:00
|
|
|
return nil, fmt.Errorf("querying Cloud Storage failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := path.Base(attrs.Name)
|
2017-09-08 09:35:09 +02:00
|
|
|
if !strings.HasSuffix(name, stateFileSuffix) {
|
2017-09-08 09:25:20 +02:00
|
|
|
continue
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
2017-09-08 09:35:09 +02:00
|
|
|
st := strings.TrimSuffix(name, stateFileSuffix)
|
2017-07-19 11:07:24 +02:00
|
|
|
|
2017-09-08 09:25:20 +02:00
|
|
|
if st != backend.DefaultStateName {
|
|
|
|
states = append(states, st)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:25:20 +02:00
|
|
|
sort.Strings(states[1:])
|
|
|
|
return states, nil
|
2017-07-19 11:07:24 +02: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
|
|
|
// DeleteWorkspace deletes the named workspaces. The "default" state cannot be deleted.
|
|
|
|
func (b *Backend) DeleteWorkspace(name string) error {
|
2017-09-08 09:30:48 +02:00
|
|
|
if name == backend.DefaultStateName {
|
|
|
|
return fmt.Errorf("cowardly refusing to delete the %q state", name)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
c, err := b.client(name)
|
2017-07-19 11:07:24 +02:00
|
|
|
if err != nil {
|
2017-09-08 09:30:48 +02:00
|
|
|
return err
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
return c.Delete()
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
// client returns a remoteClient for the named state.
|
2018-07-04 12:11:35 +02:00
|
|
|
func (b *Backend) client(name string) (*remoteClient, error) {
|
2017-07-19 11:07:24 +02:00
|
|
|
if name == "" {
|
2017-09-08 09:35:09 +02:00
|
|
|
return nil, fmt.Errorf("%q is not a valid state name", name)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
return &remoteClient{
|
2017-07-19 11:07:24 +02:00
|
|
|
storageContext: b.storageContext,
|
|
|
|
storageClient: b.storageClient,
|
|
|
|
bucketName: b.bucketName,
|
2017-09-08 14:32:00 +02:00
|
|
|
stateFilePath: b.stateFile(name),
|
|
|
|
lockFilePath: b.lockFile(name),
|
2017-12-18 01:59:10 +01:00
|
|
|
encryptionKey: b.encryptionKey,
|
2017-09-08 09:35:09 +02:00
|
|
|
}, nil
|
2017-07-19 11:07:24 +02: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
|
|
|
// StateMgr reads and returns the named state from GCS. If the named state does
|
2017-09-08 10:36:28 +02:00
|
|
|
// not yet exist, a new state file is created.
|
2020-08-11 17:43:01 +02:00
|
|
|
func (b *Backend) StateMgr(name string) (statemgr.Full, error) {
|
2017-09-08 13:36:40 +02:00
|
|
|
c, err := b.client(name)
|
2017-07-19 11:07:24 +02:00
|
|
|
if err != nil {
|
2017-09-08 10:36:28 +02:00
|
|
|
return nil, err
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:36:40 +02:00
|
|
|
st := &remote.State{Client: c}
|
2017-12-06 18:36:16 +01:00
|
|
|
|
|
|
|
// Grab the value
|
|
|
|
if err := st.RefreshState(); err != nil {
|
2017-09-08 10:36:28 +02:00
|
|
|
return nil, err
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 18:36:16 +01:00
|
|
|
// If we have no state, we have to create an empty state
|
|
|
|
if v := st.State(); v == nil {
|
2017-09-08 10:36:28 +02:00
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
lockInfo := statemgr.NewLockInfo()
|
2017-12-06 18:36:16 +01:00
|
|
|
lockInfo.Operation = "init"
|
|
|
|
lockID, err := st.Lock(lockInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 18:36:16 +01: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 11:07:24 +02:00
|
|
|
|
2017-12-06 18:36:16 +01: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 11:07:24 +02: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
|
|
|
if err := st.WriteState(states.NewState()); err != nil {
|
2017-09-08 10:36:28 +02:00
|
|
|
return nil, unlock(err)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
2017-09-08 10:36:28 +02:00
|
|
|
if err := st.PersistState(); err != nil {
|
|
|
|
return nil, unlock(err)
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 18:36:16 +01:00
|
|
|
// Unlock, the state should now be initialized
|
|
|
|
if err := unlock(nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:36:28 +02:00
|
|
|
return st, nil
|
2017-07-19 11:07:24 +02:00
|
|
|
}
|
2017-09-08 14:32:00 +02:00
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
func (b *Backend) stateFile(name string) string {
|
2017-09-11 08:25:42 +02:00
|
|
|
return path.Join(b.prefix, name+stateFileSuffix)
|
2017-09-08 14:32:00 +02:00
|
|
|
}
|
|
|
|
|
2018-07-04 12:11:35 +02:00
|
|
|
func (b *Backend) lockFile(name string) string {
|
2017-09-11 08:25:42 +02:00
|
|
|
return path.Join(b.prefix, name+lockFileSuffix)
|
2017-09-08 14:32:00 +02:00
|
|
|
}
|