/
agent.go
66 lines (52 loc) · 1.43 KB
/
agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package consul
import (
"fmt"
"sync/atomic"
log "github.com/Sirupsen/logrus"
consulapi "github.com/hashicorp/consul/api"
)
type Agent struct {
Client *consulapi.Client
IP string
failures uint32
}
func (a *Agent) IncFailures() uint32 {
return atomic.AddUint32(&a.failures, 1)
}
func (a *Agent) ClearFailures() {
atomic.StoreUint32(&a.failures, 0)
}
func (a *ConcurrentAgents) createAgent(ipAddress string) (*Agent, error) {
client, err := a.newConsulClient(ipAddress)
agent := &Agent{
Client: client,
IP: ipAddress,
}
return agent, err
}
func (a *ConcurrentAgents) newConsulClient(ipAddress string) (*consulapi.Client, error) {
config := consulapi.DefaultConfig()
config.HttpClient = a.client
config.Address = fmt.Sprintf("%s:%s", ipAddress, a.config.Port)
if a.config.Token != "" {
config.Token = a.config.Token
}
if a.config.SslEnabled {
config.Scheme = "https"
}
if a.config.Auth.Enabled {
config.HttpAuth = &consulapi.HttpBasicAuth{
Username: a.config.Auth.Username,
Password: a.config.Auth.Password,
}
}
log.WithFields(log.Fields{
"Address": config.Address,
"Scheme": config.Scheme,
"Timeout": config.HttpClient.Timeout,
"BasicAuthEnabled": a.config.Auth.Enabled,
"TokenEnabled": a.config.Token != "",
"SslVerificationEnabled": a.config.SslVerify,
}).Debug("Creating Consul client")
return consulapi.NewClient(config)
}