terraform/backend/remote-state/etcdv3/backend.go

105 lines
2.4 KiB
Go
Raw Normal View History

package etcd
import (
"context"
etcdv3 "github.com/coreos/etcd/clientv3"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/helper/schema"
)
func New() backend.Backend {
s := &schema.Backend{
Schema: map[string]*schema.Schema{
"endpoints": &schema.Schema{
2017-09-08 13:16:00 +02:00
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
MinItems: 1,
Required: true,
2017-09-08 13:16:00 +02:00
Description: "Endpoints for the etcd cluster.",
},
"username": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Username used to connect to the etcd cluster.",
Default: "", // To prevent input.
},
"password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Password used to connect to the etcd cluster.",
Default: "", // To prevent input.
},
"prefix": &schema.Schema{
Type: schema.TypeString,
2017-09-08 23:34:15 +02:00
Optional: true,
Description: "An optional prefix to be added to keys when to storing state in etcd.",
Default: "",
},
"lock": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
2017-09-09 00:40:05 +02:00
Description: "Whether to lock state access.",
Default: true,
},
},
}
result := &Backend{Backend: s}
result.Backend.ConfigureFunc = result.configure
return result
}
type Backend struct {
*schema.Backend
// The fields below are set from configure.
data *schema.ResourceData
lock bool
prefix string
}
func (b *Backend) configure(ctx context.Context) error {
// Grab the resource data.
b.data = schema.FromContextBackendConfig(ctx)
// Store the lock information.
b.lock = b.data.Get("lock").(bool)
// Store the prefix information.
b.prefix = b.data.Get("prefix").(string)
// Initialize a client to test config.
_, err := b.rawClient()
// Return err, if any.
return err
}
func (b *Backend) rawClient() (*etcdv3.Client, error) {
config := etcdv3.Config{}
2017-09-08 13:16:00 +02:00
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)
}
if v, ok := b.data.GetOk("password"); ok && v.(string) != "" {
config.Password = v.(string)
}
return etcdv3.New(config)
}
2017-09-08 13:16:00 +02:00
func retrieveEndpoints(v interface{}) []string {
var endpoints []string
list := v.([]interface{})
for _, ep := range list {
endpoints = append(endpoints, ep.(string))
}
return endpoints
}