Make 'endpoints' a schema.TypeList.

This commit is contained in:
Bruno Miguel Custodio 2017-09-08 12:16:00 +01:00
parent fee5cabeda
commit 70aad79b6e
No known key found for this signature in database
GPG Key ID: 84CDD9E18A1A6B2C
2 changed files with 26 additions and 10 deletions

View File

@ -6,16 +6,19 @@ import (
etcdv3 "github.com/coreos/etcd/clientv3" etcdv3 "github.com/coreos/etcd/clientv3"
"github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"strings"
) )
func New() backend.Backend { func New() backend.Backend {
s := &schema.Backend{ s := &schema.Backend{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"endpoints": &schema.Schema{ "endpoints": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
MinItems: 1,
Required: true, Required: true,
Description: "Comma-separated list of endpoints for the etcd cluster.", Description: "Endpoints for the etcd cluster.",
}, },
"username": &schema.Schema{ "username": &schema.Schema{
@ -77,8 +80,8 @@ func (b *Backend) configure(ctx context.Context) error {
func (b *Backend) rawClient() (*etcdv3.Client, error) { func (b *Backend) rawClient() (*etcdv3.Client, error) {
config := etcdv3.Config{} config := etcdv3.Config{}
if v, ok := b.data.GetOk("endpoints"); ok && v.(string) != "" { if v, ok := b.data.GetOk("endpoints"); ok {
config.Endpoints = strings.Split(v.(string), ",") config.Endpoints = retrieveEndpoints(v)
} }
if v, ok := b.data.GetOk("username"); ok && v.(string) != "" { if v, ok := b.data.GetOk("username"); ok && v.(string) != "" {
config.Username = v.(string) config.Username = v.(string)
@ -89,3 +92,12 @@ func (b *Backend) rawClient() (*etcdv3.Client, error) {
return etcdv3.New(config) return etcdv3.New(config)
} }
func retrieveEndpoints(v interface{}) []string {
var endpoints []string
list := v.([]interface{})
for _, ep := range list {
endpoints = append(endpoints, ep.(string))
}
return endpoints
}

View File

@ -12,6 +12,10 @@ import (
"github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/backend"
) )
var (
etcdv3Endpoints = strings.Split(os.Getenv("TF_ETCDV3_ENDPOINTS"), ",")
)
const ( const (
keyPrefix = "tf-unit" keyPrefix = "tf-unit"
) )
@ -28,7 +32,7 @@ func prepareEtcdv3(t *testing.T) {
} }
client, err := etcdv3.New(etcdv3.Config{ client, err := etcdv3.New(etcdv3.Config{
Endpoints: strings.Split(os.Getenv("TF_ETCDV3_ENDPOINTS"), ","), Endpoints: etcdv3Endpoints,
}) })
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -49,12 +53,12 @@ func TestBackend(t *testing.T) {
// Get the backend. We need two to test locking. // Get the backend. We need two to test locking.
b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{ b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{
"endpoints": os.Getenv("TF_ETCDV3_ENDPOINTS"), "endpoints": etcdv3Endpoints,
"prefix": path, "prefix": path,
}) })
b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{ b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{
"endpoints": os.Getenv("TF_ETCDV3_ENDPOINTS"), "endpoints": etcdv3Endpoints,
"prefix": path, "prefix": path,
}) })
@ -69,13 +73,13 @@ func TestBackend_lockDisabled(t *testing.T) {
// Get the backend. We need two to test locking. // Get the backend. We need two to test locking.
b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{ b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{
"endpoints": os.Getenv("TF_ETCDV3_ENDPOINTS"), "endpoints": etcdv3Endpoints,
"lock": false, "lock": false,
"prefix": key, "prefix": key,
}) })
b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{ b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{
"endpoints": os.Getenv("TF_ETCDV3_ENDPOINTS"), "endpoints": etcdv3Endpoints,
"lock": false, "lock": false,
"prefix": key + "/" + "different", // Diff so locking test would fail if it was locking "prefix": key + "/" + "different", // Diff so locking test would fail if it was locking
}) })