move legacy etcd remote state to a backend
This commit is contained in:
parent
9a004e3f9d
commit
fe527ec9d7
|
@ -3,25 +3,24 @@
|
||||||
package init
|
package init
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/backend"
|
"github.com/hashicorp/terraform/backend"
|
||||||
"github.com/hashicorp/terraform/svchost/disco"
|
"github.com/hashicorp/terraform/tfdiags"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
|
||||||
|
|
||||||
backendAtlas "github.com/hashicorp/terraform/backend/atlas"
|
backendatlas "github.com/hashicorp/terraform/backend/atlas"
|
||||||
backendLegacy "github.com/hashicorp/terraform/backend/legacy"
|
backendlocal "github.com/hashicorp/terraform/backend/local"
|
||||||
backendLocal "github.com/hashicorp/terraform/backend/local"
|
|
||||||
backendRemote "github.com/hashicorp/terraform/backend/remote"
|
|
||||||
backendAzure "github.com/hashicorp/terraform/backend/remote-state/azure"
|
backendAzure "github.com/hashicorp/terraform/backend/remote-state/azure"
|
||||||
backendConsul "github.com/hashicorp/terraform/backend/remote-state/consul"
|
backendconsul "github.com/hashicorp/terraform/backend/remote-state/consul"
|
||||||
backendEtcdv3 "github.com/hashicorp/terraform/backend/remote-state/etcdv3"
|
backendetcdv2 "github.com/hashicorp/terraform/backend/remote-state/etcdv2"
|
||||||
|
backendetcdv3 "github.com/hashicorp/terraform/backend/remote-state/etcdv3"
|
||||||
backendGCS "github.com/hashicorp/terraform/backend/remote-state/gcs"
|
backendGCS "github.com/hashicorp/terraform/backend/remote-state/gcs"
|
||||||
backendInmem "github.com/hashicorp/terraform/backend/remote-state/inmem"
|
backendinmem "github.com/hashicorp/terraform/backend/remote-state/inmem"
|
||||||
backendManta "github.com/hashicorp/terraform/backend/remote-state/manta"
|
backendManta "github.com/hashicorp/terraform/backend/remote-state/manta"
|
||||||
backendS3 "github.com/hashicorp/terraform/backend/remote-state/s3"
|
backendS3 "github.com/hashicorp/terraform/backend/remote-state/s3"
|
||||||
backendSwift "github.com/hashicorp/terraform/backend/remote-state/swift"
|
backendSwift "github.com/hashicorp/terraform/backend/remote-state/swift"
|
||||||
|
|
||||||
|
"github.com/zclconf/go-cty/cty"
|
||||||
)
|
)
|
||||||
|
|
||||||
// backends is the list of available backends. This is a global variable
|
// backends is the list of available backends. This is a global variable
|
||||||
|
@ -35,49 +34,37 @@ import (
|
||||||
// complex structures and supporting that over the plugin system is currently
|
// complex structures and supporting that over the plugin system is currently
|
||||||
// prohibitively difficult. For those wanting to implement a custom backend,
|
// prohibitively difficult. For those wanting to implement a custom backend,
|
||||||
// they can do so with recompilation.
|
// they can do so with recompilation.
|
||||||
var backends map[string]backend.InitFn
|
var backends map[string]func() backend.Backend
|
||||||
var backendsLock sync.Mutex
|
var backendsLock sync.Mutex
|
||||||
|
|
||||||
// Init initializes the backends map with all our hardcoded backends.
|
func init() {
|
||||||
func Init(services *disco.Disco) {
|
// Our hardcoded backends. We don't need to acquire a lock here
|
||||||
backendsLock.Lock()
|
// since init() code is serial and can't spawn goroutines.
|
||||||
defer backendsLock.Unlock()
|
backends = map[string]func() backend.Backend{
|
||||||
|
"atlas": func() backend.Backend { return &backendatlas.Backend{} },
|
||||||
backends = map[string]backend.InitFn{
|
"local": func() backend.Backend { return &backendlocal.Local{} },
|
||||||
// Enhanced backends.
|
"consul": func() backend.Backend { return backendconsul.New() },
|
||||||
"local": func() backend.Backend { return backendLocal.New() },
|
"inmem": func() backend.Backend { return backendinmem.New() },
|
||||||
"remote": func() backend.Backend {
|
|
||||||
b := backendRemote.New(services)
|
|
||||||
if os.Getenv("TF_FORCE_LOCAL_BACKEND") != "" {
|
|
||||||
return backendLocal.NewWithBackend(b)
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
},
|
|
||||||
|
|
||||||
// Remote State backends.
|
|
||||||
"atlas": func() backend.Backend { return backendAtlas.New() },
|
|
||||||
"azurerm": func() backend.Backend { return backendAzure.New() },
|
|
||||||
"consul": func() backend.Backend { return backendConsul.New() },
|
|
||||||
"etcdv3": func() backend.Backend { return backendEtcdv3.New() },
|
|
||||||
"gcs": func() backend.Backend { return backendGCS.New() },
|
|
||||||
"inmem": func() backend.Backend { return backendInmem.New() },
|
|
||||||
"manta": func() backend.Backend { return backendManta.New() },
|
|
||||||
"s3": func() backend.Backend { return backendS3.New() },
|
|
||||||
"swift": func() backend.Backend { return backendSwift.New() },
|
"swift": func() backend.Backend { return backendSwift.New() },
|
||||||
|
"s3": func() backend.Backend { return backendS3.New() },
|
||||||
|
"azurerm": func() backend.Backend { return backendAzure.New() },
|
||||||
|
"etcd": func() backend.Backend { return backendetcdv2.New() },
|
||||||
|
"etcdv3": func() backend.Backend { return backendetcdv3.New() },
|
||||||
|
"gcs": func() backend.Backend { return backendGCS.New() },
|
||||||
|
"manta": func() backend.Backend { return backendManta.New() },
|
||||||
|
|
||||||
// Deprecated backends.
|
"azure": func() backend.Backend {
|
||||||
"azure": deprecateBackend(backendAzure.New(),
|
return deprecateBackend(
|
||||||
`Warning: "azure" name is deprecated, please use "azurerm"`),
|
backendAzure.New(),
|
||||||
|
`Warning: "azure" name is deprecated, please use "azurerm"`,
|
||||||
|
)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the legacy remote backends that haven't yet been converted to
|
|
||||||
// the new backend API.
|
|
||||||
backendLegacy.Init(backends)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Backend returns the initialization factory for the given backend, or
|
// Backend returns the initialization factory for the given backend, or
|
||||||
// nil if none exists.
|
// nil if none exists.
|
||||||
func Backend(name string) backend.InitFn {
|
func Backend(name string) func() backend.Backend {
|
||||||
backendsLock.Lock()
|
backendsLock.Lock()
|
||||||
defer backendsLock.Unlock()
|
defer backendsLock.Unlock()
|
||||||
return backends[name]
|
return backends[name]
|
||||||
|
@ -90,7 +77,7 @@ func Backend(name string) backend.InitFn {
|
||||||
// This method sets this backend globally and care should be taken to do
|
// This method sets this backend globally and care should be taken to do
|
||||||
// this only before Terraform is executing to prevent odd behavior of backends
|
// this only before Terraform is executing to prevent odd behavior of backends
|
||||||
// changing mid-execution.
|
// changing mid-execution.
|
||||||
func Set(name string, f backend.InitFn) {
|
func Set(name string, f func() backend.Backend) {
|
||||||
backendsLock.Lock()
|
backendsLock.Lock()
|
||||||
defer backendsLock.Unlock()
|
defer backendsLock.Unlock()
|
||||||
|
|
||||||
|
@ -109,16 +96,16 @@ type deprecatedBackendShim struct {
|
||||||
Message string
|
Message string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the Backend then add the deprecation warning.
|
// ValidateConfig delegates to the wrapped backend to validate its config
|
||||||
func (b deprecatedBackendShim) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
// and then appends shim's deprecation warning.
|
||||||
warns, errs := b.Backend.Validate(c)
|
func (b deprecatedBackendShim) ValidateConfig(obj cty.Value) tfdiags.Diagnostics {
|
||||||
warns = append(warns, b.Message)
|
diags := b.Backend.ValidateConfig(obj)
|
||||||
return warns, errs
|
return diags.Append(tfdiags.SimpleWarning(b.Message))
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeprecateBackend can be used to wrap a backend to retrun a deprecation
|
// DeprecateBackend can be used to wrap a backend to retrun a deprecation
|
||||||
// warning during validation.
|
// warning during validation.
|
||||||
func deprecateBackend(b backend.Backend, message string) backend.InitFn {
|
func deprecateBackend(b backend.Backend, message string) backend.Backend {
|
||||||
// Since a Backend wrapped by deprecatedBackendShim can no longer be
|
// Since a Backend wrapped by deprecatedBackendShim can no longer be
|
||||||
// asserted as an Enhanced or Local backend, disallow those types here
|
// asserted as an Enhanced or Local backend, disallow those types here
|
||||||
// entirely. If something other than a basic backend.Backend needs to be
|
// entirely. If something other than a basic backend.Backend needs to be
|
||||||
|
@ -132,10 +119,8 @@ func deprecateBackend(b backend.Backend, message string) backend.InitFn {
|
||||||
panic("cannot use DeprecateBackend on a Local Backend")
|
panic("cannot use DeprecateBackend on a Local Backend")
|
||||||
}
|
}
|
||||||
|
|
||||||
return func() backend.Backend {
|
|
||||||
return deprecatedBackendShim{
|
return deprecatedBackendShim{
|
||||||
Backend: b,
|
Backend: b,
|
||||||
Message: message,
|
Message: message,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,96 @@
|
||||||
|
// 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<Paste>",
|
||||||
|
},
|
||||||
|
"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) States() ([]string, error) {
|
||||||
|
return nil, backend.ErrNamedStatesNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Backend) DeleteState(string) error {
|
||||||
|
return backend.ErrNamedStatesNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Backend) State(name string) (state.State, error) {
|
||||||
|
if name != backend.DefaultStateName {
|
||||||
|
return nil, backend.ErrNamedStatesNotSupported
|
||||||
|
}
|
||||||
|
return &remote.State{
|
||||||
|
Client: &EtcdClient{
|
||||||
|
Client: b.client,
|
||||||
|
Path: b.path,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package etcdv2
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/backend"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBackend_impl(t *testing.T) {
|
||||||
|
var _ backend.Backend = new(Backend)
|
||||||
|
}
|
|
@ -1,53 +1,21 @@
|
||||||
package remote
|
package etcdv2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
etcdapi "github.com/coreos/etcd/client"
|
etcdapi "github.com/coreos/etcd/client"
|
||||||
"golang.org/x/net/context"
|
"github.com/hashicorp/terraform/state/remote"
|
||||||
)
|
)
|
||||||
|
|
||||||
func etcdFactory(conf map[string]string) (Client, error) {
|
|
||||||
path, ok := conf["path"]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("missing 'path' configuration")
|
|
||||||
}
|
|
||||||
|
|
||||||
endpoints, ok := conf["endpoints"]
|
|
||||||
if !ok || endpoints == "" {
|
|
||||||
return nil, fmt.Errorf("missing 'endpoints' configuration")
|
|
||||||
}
|
|
||||||
|
|
||||||
config := etcdapi.Config{
|
|
||||||
Endpoints: strings.Split(endpoints, " "),
|
|
||||||
}
|
|
||||||
if username, ok := conf["username"]; ok && username != "" {
|
|
||||||
config.Username = username
|
|
||||||
}
|
|
||||||
if password, ok := conf["password"]; ok && password != "" {
|
|
||||||
config.Password = password
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := etcdapi.New(config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &EtcdClient{
|
|
||||||
Client: client,
|
|
||||||
Path: path,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// EtcdClient is a remote client that stores data in etcd.
|
// EtcdClient is a remote client that stores data in etcd.
|
||||||
type EtcdClient struct {
|
type EtcdClient struct {
|
||||||
Client etcdapi.Client
|
Client etcdapi.Client
|
||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *EtcdClient) Get() (*Payload, error) {
|
func (c *EtcdClient) Get() (*remote.Payload, error) {
|
||||||
resp, err := etcdapi.NewKeysAPI(c.Client).Get(context.Background(), c.Path, &etcdapi.GetOptions{Quorum: true})
|
resp, err := etcdapi.NewKeysAPI(c.Client).Get(context.Background(), c.Path, &etcdapi.GetOptions{Quorum: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err, ok := err.(etcdapi.Error); ok && err.Code == etcdapi.ErrorCodeKeyNotFound {
|
if err, ok := err.(etcdapi.Error); ok && err.Code == etcdapi.ErrorCodeKeyNotFound {
|
||||||
|
@ -61,7 +29,7 @@ func (c *EtcdClient) Get() (*Payload, error) {
|
||||||
|
|
||||||
data := []byte(resp.Node.Value)
|
data := []byte(resp.Node.Value)
|
||||||
md5 := md5.Sum(data)
|
md5 := md5.Sum(data)
|
||||||
return &Payload{
|
return &remote.Payload{
|
||||||
Data: data,
|
Data: data,
|
||||||
MD5: md5[:],
|
MD5: md5[:],
|
||||||
}, nil
|
}, nil
|
|
@ -1,14 +1,17 @@
|
||||||
package remote
|
package etcdv2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/backend"
|
||||||
|
"github.com/hashicorp/terraform/state/remote"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEtcdClient_impl(t *testing.T) {
|
func TestEtcdClient_impl(t *testing.T) {
|
||||||
var _ Client = new(EtcdClient)
|
var _ remote.Client = new(EtcdClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEtcdClient(t *testing.T) {
|
func TestEtcdClient(t *testing.T) {
|
||||||
|
@ -17,7 +20,8 @@ func TestEtcdClient(t *testing.T) {
|
||||||
t.Skipf("skipping; ETCD_ENDPOINT must be set")
|
t.Skipf("skipping; ETCD_ENDPOINT must be set")
|
||||||
}
|
}
|
||||||
|
|
||||||
config := map[string]string{
|
// Get the backend
|
||||||
|
config := map[string]interface{}{
|
||||||
"endpoints": endpoint,
|
"endpoints": endpoint,
|
||||||
"path": fmt.Sprintf("tf-unit/%s", time.Now().String()),
|
"path": fmt.Sprintf("tf-unit/%s", time.Now().String()),
|
||||||
}
|
}
|
||||||
|
@ -29,10 +33,11 @@ func TestEtcdClient(t *testing.T) {
|
||||||
config["password"] = password
|
config["password"] = password
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := etcdFactory(config)
|
b := backend.TestBackendConfig(t, New(), config)
|
||||||
|
state, err := b.State(backend.DefaultStateName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error for valid config: %s", err)
|
t.Fatalf("Error for valid config: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
testClient(t, client)
|
remote.TestClient(t, state.(*remote.State).Client)
|
||||||
}
|
}
|
|
@ -46,7 +46,6 @@ func NewClient(t string, conf map[string]string) (Client, error) {
|
||||||
// NewClient.
|
// NewClient.
|
||||||
var BuiltinClients = map[string]Factory{
|
var BuiltinClients = map[string]Factory{
|
||||||
"artifactory": artifactoryFactory,
|
"artifactory": artifactoryFactory,
|
||||||
"etcd": etcdFactory,
|
|
||||||
"http": httpFactory,
|
"http": httpFactory,
|
||||||
"local": fileFactory,
|
"local": fileFactory,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue