opentofu/backend/remote-state/etcdv2/backend.go
Sander van Harmelen 54736b068b backend/remote: use state.v2 for remote state only
The API surface area is much smaller when we use the remote backend for remote state only.

So in order to try and prevent any backwards incompatibilities when TF runs inside of TFE, we’ve split up the discovery services into `state.v2` (which can be used for remote state only configurations, so when running in TFE) and `tfe.v2.1` (which can be used for all remote configurations).
2019-02-19 10:59:51 +01:00

97 lines
2.0 KiB
Go

// legacy etcd2.x backend
package etcdv2
import (
"context"
"strings"
etcdapi "github.com/coreos/etcd/client"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/state/remote"
)
func New() backend.Backend {
s := &schema.Backend{
Schema: map[string]*schema.Schema{
"path": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The path where to store the state",
},
"endpoints": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "A space-separated list of the etcd endpoints",
},
"username": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Username",
},
"password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Password",
},
},
}
result := &Backend{Backend: s}
result.Backend.ConfigureFunc = result.configure
return result
}
type Backend struct {
*schema.Backend
client etcdapi.Client
path string
}
func (b *Backend) configure(ctx context.Context) error {
data := schema.FromContextBackendConfig(ctx)
b.path = data.Get("path").(string)
endpoints := data.Get("endpoints").(string)
username := data.Get("username").(string)
password := data.Get("password").(string)
config := etcdapi.Config{
Endpoints: strings.Split(endpoints, " "),
Username: username,
Password: password,
}
client, err := etcdapi.New(config)
if err != nil {
return err
}
b.client = client
return nil
}
func (b *Backend) Workspaces() ([]string, error) {
return nil, backend.ErrWorkspacesNotSupported
}
func (b *Backend) DeleteWorkspace(string) error {
return backend.ErrWorkspacesNotSupported
}
func (b *Backend) StateMgr(name string) (state.State, error) {
if name != backend.DefaultStateName {
return nil, backend.ErrWorkspacesNotSupported
}
return &remote.State{
Client: &EtcdClient{
Client: b.client,
Path: b.path,
},
}, nil
}