github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/consul/config.go (about)

     1  package consul
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	consulapi "github.com/hashicorp/consul/api"
     8  )
     9  
    10  type Config struct {
    11  	Datacenter string `mapstructure:"datacenter"`
    12  	Address    string `mapstructure:"address"`
    13  	Scheme     string `mapstructure:"scheme"`
    14  	Token      string `mapstructure:"token"`
    15  	CAFile     string `mapstructure:"ca_file"`
    16  	CertFile   string `mapstructure:"cert_file"`
    17  	KeyFile    string `mapstructure:"key_file"`
    18  }
    19  
    20  // Client() returns a new client for accessing consul.
    21  //
    22  func (c *Config) Client() (*consulapi.Client, error) {
    23  	config := consulapi.DefaultConfig()
    24  	if c.Datacenter != "" {
    25  		config.Datacenter = c.Datacenter
    26  	}
    27  	if c.Address != "" {
    28  		config.Address = c.Address
    29  	}
    30  	if c.Scheme != "" {
    31  		config.Scheme = c.Scheme
    32  	}
    33  
    34  	tlsConfig := &consulapi.TLSConfig{}
    35  	tlsConfig.CAFile = c.CAFile
    36  	tlsConfig.CertFile = c.CertFile
    37  	tlsConfig.KeyFile = c.KeyFile
    38  	cc, err := consulapi.SetupTLSConfig(tlsConfig)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	config.HttpClient.Transport.(*http.Transport).TLSClientConfig = cc
    43  
    44  	if c.Token != "" {
    45  		config.Token = c.Token
    46  	}
    47  
    48  	client, err := consulapi.NewClient(config)
    49  
    50  	log.Printf("[INFO] Consul Client configured with address: '%s', scheme: '%s', datacenter: '%s'",
    51  		config.Address, config.Scheme, config.Datacenter)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	return client, nil
    56  }