mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-07 06:33:53 -06:00
a3403f2766
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.
105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
package etcd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
etcdv3 "github.com/coreos/etcd/clientv3"
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
"github.com/hashicorp/terraform/state"
|
|
"github.com/hashicorp/terraform/state/remote"
|
|
"github.com/hashicorp/terraform/states"
|
|
)
|
|
|
|
func (b *Backend) Workspaces() ([]string, error) {
|
|
res, err := b.client.Get(context.TODO(), b.prefix, etcdv3.WithPrefix(), etcdv3.WithKeysOnly())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make([]string, 1, len(res.Kvs)+1)
|
|
result[0] = backend.DefaultStateName
|
|
for _, kv := range res.Kvs {
|
|
result = append(result, strings.TrimPrefix(string(kv.Key), b.prefix))
|
|
}
|
|
sort.Strings(result[1:])
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (b *Backend) DeleteWorkspace(name string) error {
|
|
if name == backend.DefaultStateName || name == "" {
|
|
return fmt.Errorf("Can't delete default state.")
|
|
}
|
|
|
|
key := b.determineKey(name)
|
|
|
|
_, err := b.client.Delete(context.TODO(), key)
|
|
return err
|
|
}
|
|
|
|
func (b *Backend) StateMgr(name string) (state.State, error) {
|
|
var stateMgr state.State = &remote.State{
|
|
Client: &RemoteClient{
|
|
Client: b.client,
|
|
DoLock: b.lock,
|
|
Key: b.determineKey(name),
|
|
},
|
|
}
|
|
|
|
if !b.lock {
|
|
stateMgr = &state.LockDisabled{Inner: stateMgr}
|
|
}
|
|
|
|
lockInfo := state.NewLockInfo()
|
|
lockInfo.Operation = "init"
|
|
lockId, err := stateMgr.Lock(lockInfo)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to lock state in etcd: %s.", err)
|
|
}
|
|
|
|
lockUnlock := func(parent error) error {
|
|
if err := stateMgr.Unlock(lockId); err != nil {
|
|
return fmt.Errorf(strings.TrimSpace(errStateUnlock), lockId, err)
|
|
}
|
|
return parent
|
|
}
|
|
|
|
if err := stateMgr.RefreshState(); err != nil {
|
|
err = lockUnlock(err)
|
|
return nil, err
|
|
}
|
|
|
|
if v := stateMgr.State(); v == nil {
|
|
if err := stateMgr.WriteState(states.NewState()); err != nil {
|
|
err = lockUnlock(err)
|
|
return nil, err
|
|
}
|
|
if err := stateMgr.PersistState(); err != nil {
|
|
err = lockUnlock(err)
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if err := lockUnlock(nil); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return stateMgr, nil
|
|
}
|
|
|
|
func (b *Backend) determineKey(name string) string {
|
|
return b.prefix + name
|
|
}
|
|
|
|
const errStateUnlock = `
|
|
Error unlocking etcd state. Lock ID: %s
|
|
|
|
Error: %s
|
|
|
|
You may have to force-unlock this state in order to use it again.
|
|
`
|