本文整理匯總了Golang中github.com/hashicorp/consul/api.NewClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClient函數的具體用法?Golang NewClient怎麽用?Golang NewClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewConsulClient
func NewConsulClient(config *consulAPI.Config) (KVClient, error) {
var (
c *consulAPI.Client
err error
)
if config != nil {
c, err = consulAPI.NewClient(config)
} else {
c, err = consulAPI.NewClient(consulAPI.DefaultConfig())
}
if err != nil {
return nil, err
}
maxRetries := 30
i := 0
for {
leader, err := c.Status().Leader()
if err != nil || leader == "" {
log.Info("Waiting for consul client to be ready...")
time.Sleep(2 * time.Second)
i++
if i > maxRetries {
e := fmt.Errorf("Unable to contact consul: %s", err)
log.Error(e)
return nil, e
}
} else {
log.Info("Consul client ready")
break
}
}
return &ConsulClient{c}, nil
}
示例2: SetUpSuite
func (s *ConsulCatalogSuite) SetUpSuite(c *check.C) {
dockerHost := os.Getenv("DOCKER_HOST")
if dockerHost == "" {
// FIXME Handle windows -- see if dockerClient already handle that or not
dockerHost = fmt.Sprintf("unix://%s", opts.DefaultUnixSocket)
}
// Make sure we can speak to docker
dockerClient, err := docker.NewClient(dockerHost)
c.Assert(err, checker.IsNil, check.Commentf("Error connecting to docker daemon"))
s.dockerClient = dockerClient
s.createComposeProject(c, "consul_catalog")
err = s.composeProject.Up()
c.Assert(err, checker.IsNil, check.Commentf("Error starting project"))
consul, err := s.GetContainer("integration-test-consul_catalog_consul_1")
c.Assert(err, checker.IsNil, check.Commentf("Error finding consul container"))
s.consulIP = consul.NetworkSettings.IPAddress
config := api.DefaultConfig()
config.Address = s.consulIP + ":8500"
consulClient, err := api.NewClient(config)
if err != nil {
c.Fatalf("Error creating consul client")
}
s.consulClient = consulClient
// Wait for consul to elect itself leader
time.Sleep(2000 * time.Millisecond)
}
示例3: consulFactory
func consulFactory(conf map[string]string) (Client, error) {
path, ok := conf["path"]
if !ok {
return nil, fmt.Errorf("missing 'path' configuration")
}
config := consulapi.DefaultConfig()
if token, ok := conf["access_token"]; ok && token != "" {
config.Token = token
}
if addr, ok := conf["address"]; ok && addr != "" {
config.Address = addr
}
if scheme, ok := conf["scheme"]; ok && scheme != "" {
config.Scheme = scheme
}
client, err := consulapi.NewClient(config)
if err != nil {
return nil, err
}
return &ConsulClient{
Client: client,
Path: path,
}, nil
}
示例4: NewConsulBackend
func NewConsulBackend(address string) (Backend, error) {
if address == "" {
address = "http://127.0.0.1:8500/vault"
}
url, err := url.Parse(address)
if err != nil {
return nil, maskAny(err)
}
path := url.Path
// Ensure path is suffixed but not prefixed
if !strings.HasSuffix(path, "/") {
path += "/"
}
if strings.HasPrefix(path, "/") {
path = strings.TrimPrefix(path, "/")
}
consulConf := api.DefaultConfig()
consulConf.Address = url.Host
client, err := api.NewClient(consulConf)
if err != nil {
return nil, maskAny(err)
}
return &consulBackend{
path: path,
client: client,
kv: client.KV(),
}, nil
}
示例5: createAPIClient
func (c *Consul) createAPIClient() (*api.Client, error) {
config := api.DefaultConfig()
if c.Address != "" {
config.Address = c.Address
}
if c.Scheme != "" {
config.Scheme = c.Scheme
}
if c.Datacentre != "" {
config.Datacenter = c.Datacentre
}
if c.Username != "" {
config.HttpAuth = &api.HttpBasicAuth{
Username: c.Username,
Password: c.Password,
}
}
tlsCfg, err := internal.GetTLSConfig(
c.SSLCert, c.SSLKey, c.SSLCA, c.InsecureSkipVerify)
if err != nil {
return nil, err
}
config.HttpClient.Transport = &http.Transport{
TLSClientConfig: tlsCfg,
}
return api.NewClient(config)
}
示例6: backupAcls
func backupAcls(ipaddress string, token string, outfile string) {
config := api.DefaultConfig()
config.Address = ipaddress
config.Token = token
client, _ := api.NewClient(config)
acl := client.ACL()
tokens, _, err := acl.List(nil)
if err != nil {
panic(err)
}
// sort.Sort(ByCreateIndex(tokens))
outstring := ""
for _, element := range tokens {
// outstring += fmt.Sprintf("%s:%s:%s:%s\n", element.ID, element.Name, element.Type, element.Rules)
outstring += fmt.Sprintf("====\nID: %s\nName: %s\nType: %s\nRules:\n%s\n", element.ID, element.Name, element.Type, element.Rules)
}
file, err := os.Create(outfile)
if err != nil {
panic(err)
}
if _, err := file.Write([]byte(outstring)[:]); err != nil {
panic(err)
}
}
示例7: NewConsulClient
func NewConsulClient() (ConsulClient, error) {
client, err := consul.NewClient(consul.DefaultConfig())
if err != nil {
return ConsulClient{}, err
}
return ConsulClient{client: client}, nil
}
示例8: Sources
// Sources implements the TargetProvider interface.
func (cd *ConsulDiscovery) Sources() []string {
clientConf := *cd.clientConf
clientConf.HttpClient = &http.Client{Timeout: 5 * time.Second}
client, err := consul.NewClient(&clientConf)
if err != nil {
// NewClient always returns a nil error.
panic(fmt.Errorf("discovery.ConsulDiscovery.Sources: %s", err))
}
srvs, _, err := client.Catalog().Services(nil)
if err != nil {
log.Errorf("Error refreshing service list: %s", err)
return nil
}
cd.mu.Lock()
defer cd.mu.Unlock()
srcs := make([]string, 0, len(srvs))
for name := range srvs {
if _, ok := cd.scrapedServices[name]; ok {
srcs = append(srcs, consulSourcePrefix+":"+name)
}
}
return srcs
}
示例9: restoreKv
/* File needs to be in the following format:
KEY1:VALUE1
KEY2:VALUE2
*/
func restoreKv(ipaddress string, token string, infile string) {
config := api.DefaultConfig()
config.Address = ipaddress
config.Token = token
data, err := ioutil.ReadFile(infile)
if err != nil {
panic(err)
}
client, _ := api.NewClient(config)
kv := client.KV()
for _, element := range strings.Split(string(data), "\n") {
split := strings.Split(element, ":")
key := strings.Join(split[:len(split)-1], ":")
value := split[len(split)-1]
if key != "" {
decoded_value, decode_err := base64.StdEncoding.DecodeString(value)
if decode_err != nil {
panic(decode_err)
}
p := &api.KVPair{Key: key, Value: decoded_value}
_, err := kv.Put(p, nil)
if err != nil {
panic(err)
}
}
}
}
示例10: NewDiscovery
// NewDiscovery returns a new Discovery for the given config.
func NewDiscovery(conf *config.ConsulSDConfig) (*Discovery, error) {
clientConf := &consul.Config{
Address: conf.Server,
Scheme: conf.Scheme,
Datacenter: conf.Datacenter,
Token: conf.Token,
HttpAuth: &consul.HttpBasicAuth{
Username: conf.Username,
Password: conf.Password,
},
}
client, err := consul.NewClient(clientConf)
if err != nil {
return nil, err
}
cd := &Discovery{
client: client,
clientConf: clientConf,
tagSeparator: conf.TagSeparator,
watchedServices: conf.Services,
}
// If the datacenter isn't set in the clientConf, let's get it from the local Consul agent
// (Consul default is to use local node's datacenter if one isn't given for a query).
if clientConf.Datacenter == "" {
info, err := client.Agent().Self()
if err != nil {
return nil, err
}
cd.clientDatacenter = info["Config"]["Datacenter"].(string)
} else {
cd.clientDatacenter = clientConf.Datacenter
}
return cd, nil
}
示例11: NewSyncer
// NewSyncer returns a new consul.Syncer
func NewSyncer(consulConfig *config.ConsulConfig, shutdownCh chan struct{}, logger *log.Logger) (*Syncer, error) {
var consulClientConfig *consul.Config
var err error
consulClientConfig, err = consulConfig.ApiConfig()
if err != nil {
return nil, err
}
var consulClient *consul.Client
if consulClient, err = consul.NewClient(consulClientConfig); err != nil {
return nil, err
}
consulSyncer := Syncer{
client: consulClient,
logger: logger,
consulAvailable: true,
shutdownCh: shutdownCh,
servicesGroups: make(map[ServiceDomain]map[ServiceKey]*consul.AgentServiceRegistration),
checkGroups: make(map[ServiceDomain]map[ServiceKey][]*consul.AgentCheckRegistration),
trackedServices: make(map[consulServiceID]*consul.AgentServiceRegistration),
trackedChecks: make(map[consulCheckID]*consul.AgentCheckRegistration),
checkRunners: make(map[consulCheckID]*CheckRunner),
periodicCallbacks: make(map[string]types.PeriodicCallback),
}
return &consulSyncer, nil
}
示例12: Get
func (consulSource *ConsulSource) Get() (map[string]interface{}, error) {
config := consul.DefaultConfig()
config.Address = consulSource.Address
if consulSource.Scheme != "" {
config.Scheme = consulSource.Scheme
}
client, err := consul.NewClient(config)
if err != nil {
return nil, err
}
pairs, _, err := client.KV().List(consulSource.Prefix, nil)
if err != nil {
return nil, err
}
result := make(map[string]interface{})
for _, pair := range pairs {
parts := strings.Split(pair.Key, "/")
result[parts[len(parts)-1]] = string(pair.Value)
}
return result, nil
}
示例13: NewSource
func NewSource(opts ...config.SourceOption) config.Source {
options := config.SourceOptions{
Name: DefaultPath,
}
for _, o := range opts {
o(&options)
}
// use default config
config := api.DefaultConfig()
// check if there are any addrs
if len(options.Hosts) > 0 {
addr, port, err := net.SplitHostPort(options.Hosts[0])
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
port = "8500"
addr = options.Hosts[0]
config.Address = fmt.Sprintf("%s:%s", addr, port)
} else if err == nil {
config.Address = fmt.Sprintf("%s:%s", addr, port)
}
}
// create the client
client, _ := api.NewClient(config)
return &consul{
addr: config.Address,
opts: options,
client: client,
}
}
示例14: Provide
func (provider *ConsulProvider) Provide(configurationChan chan<- *Configuration) {
config := &api.Config{
Address: provider.Endpoint,
Scheme: "http",
HttpClient: http.DefaultClient,
}
consulClient, _ := api.NewClient(config)
provider.consulClient = consulClient
if provider.Watch {
var waitIndex uint64
keypairs, meta, err := consulClient.KV().Keys("", "", nil)
if keypairs == nil && err == nil {
log.Error("Key was not found.")
}
waitIndex = meta.LastIndex
go func() {
for {
opts := api.QueryOptions{
WaitIndex: waitIndex,
}
keypairs, meta, err := consulClient.KV().Keys("", "", &opts)
if keypairs == nil && err == nil {
log.Error("Key was not found.")
}
waitIndex = meta.LastIndex
configuration := provider.loadConsulConfig()
if configuration != nil {
configurationChan <- configuration
}
}
}()
}
configuration := provider.loadConsulConfig()
configurationChan <- configuration
}
示例15: verifyConsulUp
func verifyConsulUp(timeout string) error {
timeoutDur, err := time.ParseDuration(timeout)
if err != nil {
return err
}
if timeoutDur == 0 {
return nil
}
config := api.DefaultConfig()
config.Token = *consulToken
client, err := api.NewClient(config)
if err != nil {
return util.Errorf("Could not construct consul client: '%s'", err)
}
consulIsUp := make(chan struct{})
go func() {
for {
time.Sleep(200 * time.Millisecond)
err := Ping(client)
if err == nil {
consulIsUp <- struct{}{}
return
}
}
}()
select {
case <-time.After(timeoutDur):
return util.Errorf("Consul did not start or was not available after %v", timeoutDur)
case <-consulIsUp:
return nil
}
}