github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/consul/config.go (about)

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