2015-01-06 18:40:00 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-03-18 14:41:00 +00:00
|
|
|
"crypto/tls"
|
2015-01-06 18:40:00 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-03-18 14:41:00 +00:00
|
|
|
"log"
|
2015-01-08 16:38:09 +00:00
|
|
|
"net"
|
2015-01-06 18:40:00 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2015-01-08 16:38:09 +00:00
|
|
|
"os"
|
2015-01-06 18:40:00 +00:00
|
|
|
"strconv"
|
2015-01-08 16:38:09 +00:00
|
|
|
"strings"
|
2015-01-06 18:40:00 +00:00
|
|
|
"time"
|
2015-10-22 14:47:50 +00:00
|
|
|
|
2015-10-22 18:14:22 +00:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2015-01-06 18:40:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// QueryOptions are used to parameterize a query
|
|
|
|
type QueryOptions struct {
|
|
|
|
// Providing a datacenter overwrites the DC provided
|
|
|
|
// by the Config
|
|
|
|
Datacenter string
|
|
|
|
|
|
|
|
// AllowStale allows any Consul server (non-leader) to service
|
|
|
|
// a read. This allows for lower latency and higher throughput
|
|
|
|
AllowStale bool
|
|
|
|
|
|
|
|
// RequireConsistent forces the read to be fully consistent.
|
|
|
|
// This is more expensive but prevents ever performing a stale
|
|
|
|
// read.
|
|
|
|
RequireConsistent bool
|
|
|
|
|
|
|
|
// WaitIndex is used to enable a blocking query. Waits
|
|
|
|
// until the timeout or the next index is reached
|
|
|
|
WaitIndex uint64
|
|
|
|
|
|
|
|
// WaitTime is used to bound the duration of a wait.
|
2015-09-15 12:22:08 +00:00
|
|
|
// Defaults to that of the Config, but can be overridden.
|
2015-01-06 18:40:00 +00:00
|
|
|
WaitTime time.Duration
|
|
|
|
|
|
|
|
// Token is used to provide a per-request ACL token
|
|
|
|
// which overrides the agent's default token.
|
|
|
|
Token string
|
2015-10-16 04:42:09 +00:00
|
|
|
|
|
|
|
// Near is used to provide a node name that will sort the results
|
|
|
|
// in ascending order based on the estimated round trip time from
|
|
|
|
// that node. Setting this to "_agent" will use the agent's node
|
|
|
|
// for the sort.
|
|
|
|
Near string
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteOptions are used to parameterize a write
|
|
|
|
type WriteOptions struct {
|
|
|
|
// Providing a datacenter overwrites the DC provided
|
|
|
|
// by the Config
|
|
|
|
Datacenter string
|
|
|
|
|
|
|
|
// Token is used to provide a per-request ACL token
|
|
|
|
// which overrides the agent's default token.
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryMeta is used to return meta data about a query
|
|
|
|
type QueryMeta struct {
|
|
|
|
// LastIndex. This can be used as a WaitIndex to perform
|
|
|
|
// a blocking query
|
|
|
|
LastIndex uint64
|
|
|
|
|
|
|
|
// Time of last contact from the leader for the
|
|
|
|
// server servicing the request
|
|
|
|
LastContact time.Duration
|
|
|
|
|
|
|
|
// Is there a known leader
|
|
|
|
KnownLeader bool
|
|
|
|
|
|
|
|
// How long did the request take
|
|
|
|
RequestTime time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMeta is used to return meta data about a write
|
|
|
|
type WriteMeta struct {
|
|
|
|
// How long did the request take
|
|
|
|
RequestTime time.Duration
|
|
|
|
}
|
|
|
|
|
2015-01-07 21:01:44 +00:00
|
|
|
// HttpBasicAuth is used to authenticate http client with HTTP Basic Authentication
|
|
|
|
type HttpBasicAuth struct {
|
|
|
|
// Username to use for HTTP Basic Authentication
|
|
|
|
Username string
|
|
|
|
|
|
|
|
// Password to use for HTTP Basic Authentication
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// Config is used to configure the creation of a client
|
|
|
|
type Config struct {
|
|
|
|
// Address is the address of the Consul server
|
|
|
|
Address string
|
|
|
|
|
|
|
|
// Scheme is the URI scheme for the Consul server
|
|
|
|
Scheme string
|
|
|
|
|
|
|
|
// Datacenter to use. If not provided, the default agent datacenter is used.
|
|
|
|
Datacenter string
|
|
|
|
|
|
|
|
// HttpClient is the client to use. Default will be
|
|
|
|
// used if not provided.
|
|
|
|
HttpClient *http.Client
|
|
|
|
|
2015-01-07 21:01:44 +00:00
|
|
|
// HttpAuth is the auth info to use for http access.
|
|
|
|
HttpAuth *HttpBasicAuth
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// WaitTime limits how long a Watch will block. If not provided,
|
|
|
|
// the agent default values will be used.
|
|
|
|
WaitTime time.Duration
|
|
|
|
|
|
|
|
// Token is used to provide a per-request ACL token
|
|
|
|
// which overrides the agent's default token.
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultConfig returns a default configuration for the client
|
|
|
|
func DefaultConfig() *Config {
|
2015-01-08 16:38:09 +00:00
|
|
|
config := &Config{
|
2015-01-06 18:40:00 +00:00
|
|
|
Address: "127.0.0.1:8500",
|
|
|
|
Scheme: "http",
|
2015-10-22 14:47:50 +00:00
|
|
|
HttpClient: cleanhttp.DefaultClient(),
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2015-01-08 16:38:09 +00:00
|
|
|
|
2015-01-16 01:24:15 +00:00
|
|
|
if addr := os.Getenv("CONSUL_HTTP_ADDR"); addr != "" {
|
|
|
|
config.Address = addr
|
2015-01-08 16:38:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 14:41:00 +00:00
|
|
|
if token := os.Getenv("CONSUL_HTTP_TOKEN"); token != "" {
|
|
|
|
config.Token = token
|
|
|
|
}
|
|
|
|
|
|
|
|
if auth := os.Getenv("CONSUL_HTTP_AUTH"); auth != "" {
|
|
|
|
var username, password string
|
|
|
|
if strings.Contains(auth, ":") {
|
|
|
|
split := strings.SplitN(auth, ":", 2)
|
|
|
|
username = split[0]
|
|
|
|
password = split[1]
|
|
|
|
} else {
|
|
|
|
username = auth
|
|
|
|
}
|
|
|
|
|
|
|
|
config.HttpAuth = &HttpBasicAuth{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ssl := os.Getenv("CONSUL_HTTP_SSL"); ssl != "" {
|
|
|
|
enabled, err := strconv.ParseBool(ssl)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] client: could not parse CONSUL_HTTP_SSL: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if enabled {
|
|
|
|
config.Scheme = "https"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if verify := os.Getenv("CONSUL_HTTP_SSL_VERIFY"); verify != "" {
|
|
|
|
doVerify, err := strconv.ParseBool(verify)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[WARN] client: could not parse CONSUL_HTTP_SSL_VERIFY: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !doVerify {
|
|
|
|
config.HttpClient.Transport = &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 16:38:09 +00:00
|
|
|
return config
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client provides a client to the Consul API
|
|
|
|
type Client struct {
|
|
|
|
config Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new client
|
|
|
|
func NewClient(config *Config) (*Client, error) {
|
|
|
|
// bootstrap the config
|
|
|
|
defConfig := DefaultConfig()
|
|
|
|
|
2015-01-16 01:24:15 +00:00
|
|
|
if len(config.Address) == 0 {
|
2015-01-06 18:40:00 +00:00
|
|
|
config.Address = defConfig.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.Scheme) == 0 {
|
|
|
|
config.Scheme = defConfig.Scheme
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.HttpClient == nil {
|
|
|
|
config.HttpClient = defConfig.HttpClient
|
|
|
|
}
|
|
|
|
|
2015-01-16 01:24:15 +00:00
|
|
|
if parts := strings.SplitN(config.Address, "unix://", 2); len(parts) == 2 {
|
2015-10-22 14:47:50 +00:00
|
|
|
trans := cleanhttp.DefaultTransport()
|
|
|
|
trans.Dial = func(_, _ string) (net.Conn, error) {
|
|
|
|
return net.Dial("unix", parts[1])
|
|
|
|
}
|
2015-01-16 01:24:15 +00:00
|
|
|
config.HttpClient = &http.Client{
|
2015-10-22 14:47:50 +00:00
|
|
|
Transport: trans,
|
2015-01-08 16:38:09 +00:00
|
|
|
}
|
2015-01-16 01:24:15 +00:00
|
|
|
config.Address = parts[1]
|
2015-01-08 16:38:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
client := &Client{
|
|
|
|
config: *config,
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// request is used to help build up a request
|
|
|
|
type request struct {
|
|
|
|
config *Config
|
|
|
|
method string
|
|
|
|
url *url.URL
|
|
|
|
params url.Values
|
|
|
|
body io.Reader
|
|
|
|
obj interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setQueryOptions is used to annotate the request with
|
|
|
|
// additional query options
|
|
|
|
func (r *request) setQueryOptions(q *QueryOptions) {
|
|
|
|
if q == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if q.Datacenter != "" {
|
|
|
|
r.params.Set("dc", q.Datacenter)
|
|
|
|
}
|
|
|
|
if q.AllowStale {
|
|
|
|
r.params.Set("stale", "")
|
|
|
|
}
|
|
|
|
if q.RequireConsistent {
|
|
|
|
r.params.Set("consistent", "")
|
|
|
|
}
|
|
|
|
if q.WaitIndex != 0 {
|
|
|
|
r.params.Set("index", strconv.FormatUint(q.WaitIndex, 10))
|
|
|
|
}
|
|
|
|
if q.WaitTime != 0 {
|
|
|
|
r.params.Set("wait", durToMsec(q.WaitTime))
|
|
|
|
}
|
|
|
|
if q.Token != "" {
|
|
|
|
r.params.Set("token", q.Token)
|
|
|
|
}
|
2015-10-16 04:42:09 +00:00
|
|
|
if q.Near != "" {
|
|
|
|
r.params.Set("near", q.Near)
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// durToMsec converts a duration to a millisecond specified string
|
|
|
|
func durToMsec(dur time.Duration) string {
|
|
|
|
return fmt.Sprintf("%dms", dur/time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setWriteOptions is used to annotate the request with
|
|
|
|
// additional write options
|
|
|
|
func (r *request) setWriteOptions(q *WriteOptions) {
|
|
|
|
if q == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if q.Datacenter != "" {
|
|
|
|
r.params.Set("dc", q.Datacenter)
|
|
|
|
}
|
|
|
|
if q.Token != "" {
|
|
|
|
r.params.Set("token", q.Token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// toHTTP converts the request to an HTTP request
|
|
|
|
func (r *request) toHTTP() (*http.Request, error) {
|
|
|
|
// Encode the query parameters
|
|
|
|
r.url.RawQuery = r.params.Encode()
|
|
|
|
|
|
|
|
// Check if we should encode the body
|
|
|
|
if r.body == nil && r.obj != nil {
|
|
|
|
if b, err := encodeBody(r.obj); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
r.body = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the HTTP request
|
2015-01-08 16:38:09 +00:00
|
|
|
req, err := http.NewRequest(r.method, r.url.RequestURI(), r.body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.URL.Host = r.url.Host
|
|
|
|
req.URL.Scheme = r.url.Scheme
|
|
|
|
req.Host = r.url.Host
|
2015-01-07 21:01:44 +00:00
|
|
|
|
|
|
|
// Setup auth
|
2015-01-14 21:25:12 +00:00
|
|
|
if r.config.HttpAuth != nil {
|
2015-01-07 21:01:44 +00:00
|
|
|
req.SetBasicAuth(r.config.HttpAuth.Username, r.config.HttpAuth.Password)
|
|
|
|
}
|
|
|
|
|
2015-01-08 16:38:09 +00:00
|
|
|
return req, nil
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newRequest is used to create a new request
|
|
|
|
func (c *Client) newRequest(method, path string) *request {
|
|
|
|
r := &request{
|
|
|
|
config: &c.config,
|
|
|
|
method: method,
|
|
|
|
url: &url.URL{
|
|
|
|
Scheme: c.config.Scheme,
|
|
|
|
Host: c.config.Address,
|
|
|
|
Path: path,
|
|
|
|
},
|
|
|
|
params: make(map[string][]string),
|
|
|
|
}
|
|
|
|
if c.config.Datacenter != "" {
|
|
|
|
r.params.Set("dc", c.config.Datacenter)
|
|
|
|
}
|
|
|
|
if c.config.WaitTime != 0 {
|
|
|
|
r.params.Set("wait", durToMsec(r.config.WaitTime))
|
|
|
|
}
|
|
|
|
if c.config.Token != "" {
|
|
|
|
r.params.Set("token", r.config.Token)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// doRequest runs a request with our client
|
|
|
|
func (c *Client) doRequest(r *request) (time.Duration, *http.Response, error) {
|
|
|
|
req, err := r.toHTTP()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
start := time.Now()
|
|
|
|
resp, err := c.config.HttpClient.Do(req)
|
|
|
|
diff := time.Now().Sub(start)
|
|
|
|
return diff, resp, err
|
|
|
|
}
|
|
|
|
|
2015-02-11 21:53:33 +00:00
|
|
|
// Query is used to do a GET request against an endpoint
|
|
|
|
// and deserialize the response into an interface using
|
|
|
|
// standard Consul conventions.
|
|
|
|
func (c *Client) query(endpoint string, out interface{}, q *QueryOptions) (*QueryMeta, error) {
|
|
|
|
r := c.newRequest("GET", endpoint)
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
rtt, resp, err := requireOK(c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
parseQueryMeta(resp, qm)
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
|
|
|
if err := decodeBody(resp, out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return qm, nil
|
|
|
|
}
|
|
|
|
|
2015-02-11 22:01:29 +00:00
|
|
|
// write is used to do a PUT request against an endpoint
|
|
|
|
// and serialize/deserialized using the standard Consul conventions.
|
|
|
|
func (c *Client) write(endpoint string, in, out interface{}, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
r := c.newRequest("PUT", endpoint)
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
r.obj = in
|
|
|
|
rtt, resp, err := requireOK(c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
wm := &WriteMeta{RequestTime: rtt}
|
|
|
|
if out != nil {
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm, nil
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// parseQueryMeta is used to help parse query meta-data
|
|
|
|
func parseQueryMeta(resp *http.Response, q *QueryMeta) error {
|
|
|
|
header := resp.Header
|
|
|
|
|
|
|
|
// Parse the X-Consul-Index
|
|
|
|
index, err := strconv.ParseUint(header.Get("X-Consul-Index"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse X-Consul-Index: %v", err)
|
|
|
|
}
|
|
|
|
q.LastIndex = index
|
|
|
|
|
|
|
|
// Parse the X-Consul-LastContact
|
|
|
|
last, err := strconv.ParseUint(header.Get("X-Consul-LastContact"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse X-Consul-LastContact: %v", err)
|
|
|
|
}
|
|
|
|
q.LastContact = time.Duration(last) * time.Millisecond
|
|
|
|
|
|
|
|
// Parse the X-Consul-KnownLeader
|
|
|
|
switch header.Get("X-Consul-KnownLeader") {
|
|
|
|
case "true":
|
|
|
|
q.KnownLeader = true
|
|
|
|
default:
|
|
|
|
q.KnownLeader = false
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodeBody is used to JSON decode a body
|
|
|
|
func decodeBody(resp *http.Response, out interface{}) error {
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
return dec.Decode(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// encodeBody is used to encode a request body
|
|
|
|
func encodeBody(obj interface{}) (io.Reader, error) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
if err := enc.Encode(obj); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// requireOK is used to wrap doRequest and check for a 200
|
|
|
|
func requireOK(d time.Duration, resp *http.Response, e error) (time.Duration, *http.Response, error) {
|
|
|
|
if e != nil {
|
2015-01-19 02:53:21 +00:00
|
|
|
if resp != nil {
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
return d, nil, e
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, resp.Body)
|
2015-01-19 02:53:21 +00:00
|
|
|
resp.Body.Close()
|
|
|
|
return d, nil, fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, buf.Bytes())
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2015-01-19 02:53:21 +00:00
|
|
|
return d, resp, nil
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|