Skip to content

Commit

Permalink
Make 'endpoints' a schema.TypeList.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmcustodio committed Sep 8, 2017
1 parent fee5cab commit 70aad79
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
22 changes: 17 additions & 5 deletions backend/remote-state/etcdv3/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import (
etcdv3 "github.com/coreos/etcd/clientv3"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/helper/schema"
"strings"
)

func New() backend.Backend {
s := &schema.Backend{
Schema: map[string]*schema.Schema{
"endpoints": &schema.Schema{
Type: schema.TypeString,
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
MinItems: 1,
Required: true,
Description: "Comma-separated list of endpoints for the etcd cluster.",
Description: "Endpoints for the etcd cluster.",
},

"username": &schema.Schema{
Expand Down Expand Up @@ -77,8 +80,8 @@ func (b *Backend) configure(ctx context.Context) error {
func (b *Backend) rawClient() (*etcdv3.Client, error) {
config := etcdv3.Config{}

if v, ok := b.data.GetOk("endpoints"); ok && v.(string) != "" {
config.Endpoints = strings.Split(v.(string), ",")
if v, ok := b.data.GetOk("endpoints"); ok {
config.Endpoints = retrieveEndpoints(v)
}
if v, ok := b.data.GetOk("username"); ok && v.(string) != "" {
config.Username = v.(string)
Expand All @@ -89,3 +92,12 @@ func (b *Backend) rawClient() (*etcdv3.Client, error) {

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
}
14 changes: 9 additions & 5 deletions backend/remote-state/etcdv3/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"github.com/hashicorp/terraform/backend"
)

var (
etcdv3Endpoints = strings.Split(os.Getenv("TF_ETCDV3_ENDPOINTS"), ",")
)

const (
keyPrefix = "tf-unit"
)
Expand All @@ -28,7 +32,7 @@ func prepareEtcdv3(t *testing.T) {
}

client, err := etcdv3.New(etcdv3.Config{
Endpoints: strings.Split(os.Getenv("TF_ETCDV3_ENDPOINTS"), ","),
Endpoints: etcdv3Endpoints,
})
if err != nil {
t.Fatal(err)
Expand All @@ -49,12 +53,12 @@ func TestBackend(t *testing.T) {

// Get the backend. We need two to test locking.
b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{
"endpoints": os.Getenv("TF_ETCDV3_ENDPOINTS"),
"endpoints": etcdv3Endpoints,
"prefix": path,
})

b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{
"endpoints": os.Getenv("TF_ETCDV3_ENDPOINTS"),
"endpoints": etcdv3Endpoints,
"prefix": path,
})

Expand All @@ -69,13 +73,13 @@ func TestBackend_lockDisabled(t *testing.T) {

// Get the backend. We need two to test locking.
b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{
"endpoints": os.Getenv("TF_ETCDV3_ENDPOINTS"),
"endpoints": etcdv3Endpoints,
"lock": false,
"prefix": key,
})

b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{
"endpoints": os.Getenv("TF_ETCDV3_ENDPOINTS"),
"endpoints": etcdv3Endpoints,
"lock": false,
"prefix": key + "/" + "different", // Diff so locking test would fail if it was locking
})
Expand Down

0 comments on commit 70aad79

Please sign in to comment.