terraform/builtin/providers/consul/config.go

57 lines
1.3 KiB
Go
Raw Normal View History

2014-07-25 23:03:17 +02:00
package consul
import (
"log"
"net/http"
2014-07-25 23:03:17 +02:00
consulapi "github.com/hashicorp/consul/api"
2014-07-25 23:03:17 +02:00
)
type Config struct {
Datacenter string `mapstructure:"datacenter"`
Address string `mapstructure:"address"`
Scheme string `mapstructure:"scheme"`
Token string `mapstructure:"token"`
CAFile string `mapstructure:"ca_file"`
CertFile string `mapstructure:"cert_file"`
KeyFile string `mapstructure:"key_file"`
2014-07-25 23:03:17 +02:00
}
// Client() returns a new client for accessing consul.
2014-07-25 23:03:17 +02:00
//
func (c *Config) Client() (*consulapi.Client, error) {
config := consulapi.DefaultConfig()
if c.Datacenter != "" {
config.Datacenter = c.Datacenter
}
if c.Address != "" {
config.Address = c.Address
}
if c.Scheme != "" {
config.Scheme = c.Scheme
}
tlsConfig := &consulapi.TLSConfig{}
tlsConfig.CAFile = c.CAFile
tlsConfig.CertFile = c.CertFile
tlsConfig.KeyFile = c.KeyFile
cc, err := consulapi.SetupTLSConfig(tlsConfig)
if err != nil {
return nil, err
}
config.HttpClient.Transport.(*http.Transport).TLSClientConfig = cc
if c.Token != "" {
config.Token = c.Token
}
2014-07-25 23:03:17 +02:00
client, err := consulapi.NewClient(config)
log.Printf("[INFO] Consul Client configured with address: '%s', scheme: '%s', datacenter: '%s'",
config.Address, config.Scheme, config.Datacenter)
2014-07-25 23:03:17 +02:00
if err != nil {
return nil, err
}
return client, nil
}