2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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"
|
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 16:24:45 -05:00
|
|
|
"google.golang.org/api/iterator"
|
|
|
|
|
2023-08-17 07:45:11 -05:00
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/backend"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/states"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/states/remote"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/states/statemgr"
|
2017-07-19 04:07:24 -05:00
|
|
|
)
|
|
|
|
|
2017-09-08 02:35:09 -05: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 16:24:45 -05:00
|
|
|
// Workspaces returns a list of names for the workspaces found on GCS. The default
|
2017-09-08 02:25:20 -05: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 16:24:45 -05:00
|
|
|
func (b *Backend) Workspaces() ([]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
|
|
|
}
|
|
|
|
|
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 16:24:45 -05:00
|
|
|
// DeleteWorkspace deletes the named workspaces. The "default" state cannot be deleted.
|
2022-10-05 14:57:09 -05:00
|
|
|
func (b *Backend) DeleteWorkspace(name string, _ bool) 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.
|
2018-07-04 05:11:35 -05:00
|
|
|
func (b *Backend) 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,
|
2022-10-04 04:10:49 -05:00
|
|
|
kmsKeyName: b.kmsKeyName,
|
2017-09-08 02:35:09 -05:00
|
|
|
}, nil
|
2017-07-19 04:07:24 -05: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 16:24:45 -05:00
|
|
|
// StateMgr reads and returns the named state from GCS. If the named state does
|
2017-09-08 03:36:28 -05:00
|
|
|
// not yet exist, a new state file is created.
|
2020-08-11 10:43:01 -05:00
|
|
|
func (b *Backend) StateMgr(name string) (statemgr.Full, 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
|
|
|
|
2020-08-11 10:43:01 -05:00
|
|
|
lockInfo := statemgr.NewLockInfo()
|
2017-12-06 11:36:16 -06:00
|
|
|
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
|
|
|
|
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 16:24:45 -05:00
|
|
|
if err := st.WriteState(states.NewState()); err != nil {
|
2017-09-08 03:36:28 -05:00
|
|
|
return nil, unlock(err)
|
2017-07-19 04:07:24 -05:00
|
|
|
}
|
2022-08-25 14:57:40 -05:00
|
|
|
if err := st.PersistState(nil); err != nil {
|
2017-09-08 03:36:28 -05:00
|
|
|
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
|
|
|
|
2018-07-04 05:11:35 -05:00
|
|
|
func (b *Backend) stateFile(name string) string {
|
2017-09-11 01:25:42 -05:00
|
|
|
return path.Join(b.prefix, name+stateFileSuffix)
|
2017-09-08 07:32:00 -05:00
|
|
|
}
|
|
|
|
|
2018-07-04 05:11:35 -05:00
|
|
|
func (b *Backend) lockFile(name string) string {
|
2017-09-11 01:25:42 -05:00
|
|
|
return path.Join(b.prefix, name+lockFileSuffix)
|
2017-09-08 07:32:00 -05:00
|
|
|
}
|