mirror of https://github.com/hashicorp/consul
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
441 lines
15 KiB
441 lines
15 KiB
11 years ago
|
package agent
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
10 years ago
|
"fmt"
|
||
11 years ago
|
"net/http"
|
||
11 years ago
|
"net/http/pprof"
|
||
10 years ago
|
"net/url"
|
||
11 years ago
|
"strconv"
|
||
10 years ago
|
"strings"
|
||
11 years ago
|
"time"
|
||
10 years ago
|
|
||
8 years ago
|
"github.com/armon/go-metrics"
|
||
8 years ago
|
"github.com/hashicorp/consul/agent/consul/structs"
|
||
10 years ago
|
"github.com/mitchellh/mapstructure"
|
||
11 years ago
|
)
|
||
|
|
||
8 years ago
|
// HTTPServer provides an HTTP api for an agent.
|
||
11 years ago
|
type HTTPServer struct {
|
||
8 years ago
|
*http.Server
|
||
|
agent *Agent
|
||
8 years ago
|
proto string
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func NewHTTPServer(addr string, a *Agent) *HTTPServer {
|
||
8 years ago
|
s := &HTTPServer{Server: &http.Server{Addr: addr}, agent: a}
|
||
8 years ago
|
s.Server.Handler = s.handler(s.agent.config.EnableDebug)
|
||
|
return s
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
// handler is used to attach our handlers to the mux
|
||
|
func (s *HTTPServer) handler(enableDebug bool) http.Handler {
|
||
|
mux := http.NewServeMux()
|
||
|
|
||
|
// handleFuncMetrics takes the given pattern and handler and wraps to produce
|
||
|
// metrics based on the pattern and request.
|
||
|
handleFuncMetrics := func(pattern string, handler http.HandlerFunc) {
|
||
|
// Get the parts of the pattern. We omit any initial empty for the
|
||
|
// leading slash, and put an underscore as a "thing" placeholder if we
|
||
|
// see a trailing slash, which means the part after is parsed. This lets
|
||
|
// us distinguish from things like /v1/query and /v1/query/<query id>.
|
||
|
var parts []string
|
||
|
for i, part := range strings.Split(pattern, "/") {
|
||
|
if part == "" {
|
||
|
if i == 0 {
|
||
|
continue
|
||
|
}
|
||
8 years ago
|
part = "_"
|
||
|
}
|
||
8 years ago
|
parts = append(parts, part)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// Register the wrapper, which will close over the expensive-to-compute
|
||
|
// parts from above.
|
||
|
wrapper := func(resp http.ResponseWriter, req *http.Request) {
|
||
|
start := time.Now()
|
||
|
handler(resp, req)
|
||
|
key := append([]string{"consul", "http", req.Method}, parts...)
|
||
|
metrics.MeasureSince(key, start)
|
||
|
}
|
||
|
mux.HandleFunc(pattern, wrapper)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
mux.HandleFunc("/", s.Index)
|
||
11 years ago
|
|
||
8 years ago
|
// API V1.
|
||
|
if s.agent.config.ACLDatacenter != "" {
|
||
8 years ago
|
handleFuncMetrics("/v1/acl/create", s.wrap(s.ACLCreate))
|
||
|
handleFuncMetrics("/v1/acl/update", s.wrap(s.ACLUpdate))
|
||
|
handleFuncMetrics("/v1/acl/destroy/", s.wrap(s.ACLDestroy))
|
||
|
handleFuncMetrics("/v1/acl/info/", s.wrap(s.ACLGet))
|
||
|
handleFuncMetrics("/v1/acl/clone/", s.wrap(s.ACLClone))
|
||
|
handleFuncMetrics("/v1/acl/list", s.wrap(s.ACLList))
|
||
|
handleFuncMetrics("/v1/acl/replication", s.wrap(s.ACLReplicationStatus))
|
||
9 years ago
|
} else {
|
||
8 years ago
|
handleFuncMetrics("/v1/acl/create", s.wrap(ACLDisabled))
|
||
|
handleFuncMetrics("/v1/acl/update", s.wrap(ACLDisabled))
|
||
|
handleFuncMetrics("/v1/acl/destroy/", s.wrap(ACLDisabled))
|
||
|
handleFuncMetrics("/v1/acl/info/", s.wrap(ACLDisabled))
|
||
|
handleFuncMetrics("/v1/acl/clone/", s.wrap(ACLDisabled))
|
||
|
handleFuncMetrics("/v1/acl/list", s.wrap(ACLDisabled))
|
||
|
handleFuncMetrics("/v1/acl/replication", s.wrap(ACLDisabled))
|
||
9 years ago
|
}
|
||
8 years ago
|
handleFuncMetrics("/v1/agent/self", s.wrap(s.AgentSelf))
|
||
|
handleFuncMetrics("/v1/agent/maintenance", s.wrap(s.AgentNodeMaintenance))
|
||
|
handleFuncMetrics("/v1/agent/reload", s.wrap(s.AgentReload))
|
||
|
handleFuncMetrics("/v1/agent/monitor", s.wrap(s.AgentMonitor))
|
||
|
handleFuncMetrics("/v1/agent/services", s.wrap(s.AgentServices))
|
||
|
handleFuncMetrics("/v1/agent/checks", s.wrap(s.AgentChecks))
|
||
|
handleFuncMetrics("/v1/agent/members", s.wrap(s.AgentMembers))
|
||
|
handleFuncMetrics("/v1/agent/join/", s.wrap(s.AgentJoin))
|
||
|
handleFuncMetrics("/v1/agent/leave", s.wrap(s.AgentLeave))
|
||
|
handleFuncMetrics("/v1/agent/force-leave/", s.wrap(s.AgentForceLeave))
|
||
|
handleFuncMetrics("/v1/agent/check/register", s.wrap(s.AgentRegisterCheck))
|
||
|
handleFuncMetrics("/v1/agent/check/deregister/", s.wrap(s.AgentDeregisterCheck))
|
||
|
handleFuncMetrics("/v1/agent/check/pass/", s.wrap(s.AgentCheckPass))
|
||
|
handleFuncMetrics("/v1/agent/check/warn/", s.wrap(s.AgentCheckWarn))
|
||
|
handleFuncMetrics("/v1/agent/check/fail/", s.wrap(s.AgentCheckFail))
|
||
|
handleFuncMetrics("/v1/agent/check/update/", s.wrap(s.AgentCheckUpdate))
|
||
|
handleFuncMetrics("/v1/agent/service/register", s.wrap(s.AgentRegisterService))
|
||
|
handleFuncMetrics("/v1/agent/service/deregister/", s.wrap(s.AgentDeregisterService))
|
||
|
handleFuncMetrics("/v1/agent/service/maintenance/", s.wrap(s.AgentServiceMaintenance))
|
||
|
handleFuncMetrics("/v1/catalog/register", s.wrap(s.CatalogRegister))
|
||
|
handleFuncMetrics("/v1/catalog/deregister", s.wrap(s.CatalogDeregister))
|
||
|
handleFuncMetrics("/v1/catalog/datacenters", s.wrap(s.CatalogDatacenters))
|
||
|
handleFuncMetrics("/v1/catalog/nodes", s.wrap(s.CatalogNodes))
|
||
|
handleFuncMetrics("/v1/catalog/services", s.wrap(s.CatalogServices))
|
||
|
handleFuncMetrics("/v1/catalog/service/", s.wrap(s.CatalogServiceNodes))
|
||
|
handleFuncMetrics("/v1/catalog/node/", s.wrap(s.CatalogNodeServices))
|
||
8 years ago
|
if !s.agent.config.DisableCoordinates {
|
||
8 years ago
|
handleFuncMetrics("/v1/coordinate/datacenters", s.wrap(s.CoordinateDatacenters))
|
||
|
handleFuncMetrics("/v1/coordinate/nodes", s.wrap(s.CoordinateNodes))
|
||
8 years ago
|
} else {
|
||
8 years ago
|
handleFuncMetrics("/v1/coordinate/datacenters", s.wrap(coordinateDisabled))
|
||
|
handleFuncMetrics("/v1/coordinate/nodes", s.wrap(coordinateDisabled))
|
||
8 years ago
|
}
|
||
8 years ago
|
handleFuncMetrics("/v1/event/fire/", s.wrap(s.EventFire))
|
||
|
handleFuncMetrics("/v1/event/list", s.wrap(s.EventList))
|
||
|
handleFuncMetrics("/v1/health/node/", s.wrap(s.HealthNodeChecks))
|
||
|
handleFuncMetrics("/v1/health/checks/", s.wrap(s.HealthServiceChecks))
|
||
|
handleFuncMetrics("/v1/health/state/", s.wrap(s.HealthChecksInState))
|
||
|
handleFuncMetrics("/v1/health/service/", s.wrap(s.HealthServiceNodes))
|
||
|
handleFuncMetrics("/v1/internal/ui/nodes", s.wrap(s.UINodes))
|
||
|
handleFuncMetrics("/v1/internal/ui/node/", s.wrap(s.UINodeInfo))
|
||
|
handleFuncMetrics("/v1/internal/ui/services", s.wrap(s.UIServices))
|
||
|
handleFuncMetrics("/v1/kv/", s.wrap(s.KVSEndpoint))
|
||
|
handleFuncMetrics("/v1/operator/raft/configuration", s.wrap(s.OperatorRaftConfiguration))
|
||
|
handleFuncMetrics("/v1/operator/raft/peer", s.wrap(s.OperatorRaftPeer))
|
||
|
handleFuncMetrics("/v1/operator/keyring", s.wrap(s.OperatorKeyringEndpoint))
|
||
|
handleFuncMetrics("/v1/operator/autopilot/configuration", s.wrap(s.OperatorAutopilotConfiguration))
|
||
|
handleFuncMetrics("/v1/operator/autopilot/health", s.wrap(s.OperatorServerHealth))
|
||
|
handleFuncMetrics("/v1/query", s.wrap(s.PreparedQueryGeneral))
|
||
|
handleFuncMetrics("/v1/query/", s.wrap(s.PreparedQuerySpecific))
|
||
|
handleFuncMetrics("/v1/session/create", s.wrap(s.SessionCreate))
|
||
|
handleFuncMetrics("/v1/session/destroy/", s.wrap(s.SessionDestroy))
|
||
|
handleFuncMetrics("/v1/session/renew/", s.wrap(s.SessionRenew))
|
||
|
handleFuncMetrics("/v1/session/info/", s.wrap(s.SessionGet))
|
||
|
handleFuncMetrics("/v1/session/node/", s.wrap(s.SessionsForNode))
|
||
|
handleFuncMetrics("/v1/session/list", s.wrap(s.SessionList))
|
||
|
handleFuncMetrics("/v1/status/leader", s.wrap(s.StatusLeader))
|
||
|
handleFuncMetrics("/v1/status/peers", s.wrap(s.StatusPeers))
|
||
|
handleFuncMetrics("/v1/snapshot", s.wrap(s.Snapshot))
|
||
|
handleFuncMetrics("/v1/txn", s.wrap(s.Txn))
|
||
9 years ago
|
|
||
8 years ago
|
// Debug endpoints.
|
||
11 years ago
|
if enableDebug {
|
||
8 years ago
|
handleFuncMetrics("/debug/pprof/", pprof.Index)
|
||
|
handleFuncMetrics("/debug/pprof/cmdline", pprof.Cmdline)
|
||
|
handleFuncMetrics("/debug/pprof/profile", pprof.Profile)
|
||
|
handleFuncMetrics("/debug/pprof/symbol", pprof.Symbol)
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
9 years ago
|
// Use the custom UI dir if provided.
|
||
8 years ago
|
if s.agent.config.UIDir != "" {
|
||
8 years ago
|
mux.Handle("/ui/", http.StripPrefix("/ui/", http.FileServer(http.Dir(s.agent.config.UIDir))))
|
||
8 years ago
|
} else if s.agent.config.EnableUI {
|
||
8 years ago
|
mux.Handle("/ui/", http.StripPrefix("/ui/", http.FileServer(assetFS())))
|
||
10 years ago
|
}
|
||
8 years ago
|
return mux
|
||
11 years ago
|
}
|
||
|
|
||
|
// wrap is used to wrap functions to make them more convenient
|
||
8 years ago
|
func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Request) (interface{}, error)) http.HandlerFunc {
|
||
|
return func(resp http.ResponseWriter, req *http.Request) {
|
||
10 years ago
|
setHeaders(resp, s.agent.config.HTTPAPIResponseHeaders)
|
||
8 years ago
|
setTranslateAddr(resp, s.agent.config.TranslateWanAddrs)
|
||
10 years ago
|
|
||
10 years ago
|
// Obfuscate any tokens from appearing in the logs
|
||
10 years ago
|
formVals, err := url.ParseQuery(req.URL.RawQuery)
|
||
|
if err != nil {
|
||
8 years ago
|
s.agent.logger.Printf("[ERR] http: Failed to decode query: %s from=%s", err, req.RemoteAddr)
|
||
9 years ago
|
resp.WriteHeader(http.StatusInternalServerError) // 500
|
||
10 years ago
|
return
|
||
|
}
|
||
10 years ago
|
logURL := req.URL.String()
|
||
10 years ago
|
if tokens, ok := formVals["token"]; ok {
|
||
10 years ago
|
for _, token := range tokens {
|
||
10 years ago
|
if token == "" {
|
||
|
logURL += "<hidden>"
|
||
|
continue
|
||
|
}
|
||
10 years ago
|
logURL = strings.Replace(logURL, token, "<hidden>", -1)
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
handleErr := func(err error) {
|
||
8 years ago
|
s.agent.logger.Printf("[ERR] http: Request %s %v, error: %v from=%s", req.Method, logURL, err, req.RemoteAddr)
|
||
8 years ago
|
code := http.StatusInternalServerError // 500
|
||
|
errMsg := err.Error()
|
||
|
if strings.Contains(errMsg, "Permission denied") || strings.Contains(errMsg, "ACL not found") {
|
||
|
code = http.StatusForbidden // 403
|
||
|
}
|
||
|
resp.WriteHeader(code)
|
||
|
fmt.Fprint(resp, errMsg)
|
||
|
}
|
||
|
|
||
9 years ago
|
// TODO (slackpad) We may want to consider redacting prepared
|
||
|
// query names/IDs here since they are proxies for tokens. But,
|
||
|
// knowing one only gives you read access to service listings
|
||
|
// which is pretty trivial, so it's probably not worth the code
|
||
|
// complexity and overhead of filtering them out. You can't
|
||
|
// recover the token it's a proxy for with just the query info;
|
||
|
// you'd need the actual token (or a management token) to read
|
||
|
// that back.
|
||
|
|
||
11 years ago
|
// Invoke the handler
|
||
11 years ago
|
start := time.Now()
|
||
|
defer func() {
|
||
8 years ago
|
s.agent.logger.Printf("[DEBUG] http: Request %s %v (%v) from=%s", req.Method, logURL, time.Now().Sub(start), req.RemoteAddr)
|
||
11 years ago
|
}()
|
||
11 years ago
|
obj, err := handler(resp, req)
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
handleErr(err)
|
||
|
return
|
||
|
}
|
||
|
if obj == nil {
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
buf, err := s.marshalJSON(req, obj)
|
||
|
if err != nil {
|
||
|
handleErr(err)
|
||
|
return
|
||
11 years ago
|
}
|
||
8 years ago
|
resp.Header().Set("Content-Type", "application/json")
|
||
|
resp.Write(buf)
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
|
||
9 years ago
|
// marshalJSON marshals the object into JSON, respecting the user's pretty-ness
|
||
|
// configuration.
|
||
|
func (s *HTTPServer) marshalJSON(req *http.Request, obj interface{}) ([]byte, error) {
|
||
8 years ago
|
if _, ok := req.URL.Query()["pretty"]; ok || s.agent.config.DevMode {
|
||
9 years ago
|
buf, err := json.MarshalIndent(obj, "", " ")
|
||
9 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
buf = append(buf, "\n"...)
|
||
|
return buf, nil
|
||
9 years ago
|
}
|
||
|
|
||
|
buf, err := json.Marshal(obj)
|
||
9 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
9 years ago
|
return buf, err
|
||
|
}
|
||
|
|
||
9 years ago
|
// Returns true if the UI is enabled.
|
||
|
func (s *HTTPServer) IsUIEnabled() bool {
|
||
8 years ago
|
return s.agent.config.UIDir != "" || s.agent.config.EnableUI
|
||
9 years ago
|
}
|
||
|
|
||
11 years ago
|
// Renders a simple index page
|
||
|
func (s *HTTPServer) Index(resp http.ResponseWriter, req *http.Request) {
|
||
11 years ago
|
// Check if this is a non-index path
|
||
|
if req.URL.Path != "/" {
|
||
9 years ago
|
resp.WriteHeader(http.StatusNotFound) // 404
|
||
11 years ago
|
return
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
9 years ago
|
// Give them something helpful if there's no UI so they at least know
|
||
|
// what this server is.
|
||
|
if !s.IsUIEnabled() {
|
||
8 years ago
|
fmt.Fprint(resp, "Consul Agent")
|
||
11 years ago
|
return
|
||
|
}
|
||
|
|
||
|
// Redirect to the UI endpoint
|
||
9 years ago
|
http.Redirect(resp, req, "/ui/", http.StatusMovedPermanently) // 301
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// decodeBody is used to decode a JSON request body
|
||
11 years ago
|
func decodeBody(req *http.Request, out interface{}, cb func(interface{}) error) error {
|
||
|
var raw interface{}
|
||
11 years ago
|
dec := json.NewDecoder(req.Body)
|
||
11 years ago
|
if err := dec.Decode(&raw); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Invoke the callback prior to decode
|
||
|
if cb != nil {
|
||
|
if err := cb(raw); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return mapstructure.Decode(raw, out)
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// setTranslateAddr is used to set the address translation header. This is only
|
||
|
// present if the feature is active.
|
||
|
func setTranslateAddr(resp http.ResponseWriter, active bool) {
|
||
|
if active {
|
||
|
resp.Header().Set("X-Consul-Translate-Addresses", "true")
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
// setIndex is used to set the index response header
|
||
|
func setIndex(resp http.ResponseWriter, index uint64) {
|
||
10 years ago
|
resp.Header().Set("X-Consul-Index", strconv.FormatUint(index, 10))
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// setKnownLeader is used to set the known leader header
|
||
|
func setKnownLeader(resp http.ResponseWriter, known bool) {
|
||
|
s := "true"
|
||
|
if !known {
|
||
|
s = "false"
|
||
|
}
|
||
10 years ago
|
resp.Header().Set("X-Consul-KnownLeader", s)
|
||
11 years ago
|
}
|
||
|
|
||
|
// setLastContact is used to set the last contact header
|
||
|
func setLastContact(resp http.ResponseWriter, last time.Duration) {
|
||
8 years ago
|
if last < 0 {
|
||
|
last = 0
|
||
|
}
|
||
11 years ago
|
lastMsec := uint64(last / time.Millisecond)
|
||
10 years ago
|
resp.Header().Set("X-Consul-LastContact", strconv.FormatUint(lastMsec, 10))
|
||
11 years ago
|
}
|
||
|
|
||
|
// setMeta is used to set the query response meta data
|
||
|
func setMeta(resp http.ResponseWriter, m *structs.QueryMeta) {
|
||
|
setIndex(resp, m.Index)
|
||
|
setLastContact(resp, m.LastContact)
|
||
|
setKnownLeader(resp, m.KnownLeader)
|
||
|
}
|
||
|
|
||
10 years ago
|
// setHeaders is used to set canonical response header fields
|
||
|
func setHeaders(resp http.ResponseWriter, headers map[string]string) {
|
||
|
for field, value := range headers {
|
||
|
resp.Header().Set(http.CanonicalHeaderKey(field), value)
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
// parseWait is used to parse the ?wait and ?index query params
|
||
|
// Returns true on error
|
||
11 years ago
|
func parseWait(resp http.ResponseWriter, req *http.Request, b *structs.QueryOptions) bool {
|
||
11 years ago
|
query := req.URL.Query()
|
||
|
if wait := query.Get("wait"); wait != "" {
|
||
|
dur, err := time.ParseDuration(wait)
|
||
|
if err != nil {
|
||
9 years ago
|
resp.WriteHeader(http.StatusBadRequest) // 400
|
||
8 years ago
|
fmt.Fprint(resp, "Invalid wait time")
|
||
11 years ago
|
return true
|
||
|
}
|
||
|
b.MaxQueryTime = dur
|
||
|
}
|
||
|
if idx := query.Get("index"); idx != "" {
|
||
|
index, err := strconv.ParseUint(idx, 10, 64)
|
||
|
if err != nil {
|
||
9 years ago
|
resp.WriteHeader(http.StatusBadRequest) // 400
|
||
8 years ago
|
fmt.Fprint(resp, "Invalid index")
|
||
11 years ago
|
return true
|
||
|
}
|
||
|
b.MinQueryIndex = index
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
11 years ago
|
// parseConsistency is used to parse the ?stale and ?consistent query params.
|
||
|
// Returns true on error
|
||
|
func parseConsistency(resp http.ResponseWriter, req *http.Request, b *structs.QueryOptions) bool {
|
||
|
query := req.URL.Query()
|
||
|
if _, ok := query["stale"]; ok {
|
||
|
b.AllowStale = true
|
||
|
}
|
||
|
if _, ok := query["consistent"]; ok {
|
||
|
b.RequireConsistent = true
|
||
|
}
|
||
|
if b.AllowStale && b.RequireConsistent {
|
||
9 years ago
|
resp.WriteHeader(http.StatusBadRequest) // 400
|
||
8 years ago
|
fmt.Fprint(resp, "Cannot specify ?stale with ?consistent, conflicting semantics.")
|
||
11 years ago
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
11 years ago
|
// parseDC is used to parse the ?dc query param
|
||
|
func (s *HTTPServer) parseDC(req *http.Request, dc *string) {
|
||
|
if other := req.URL.Query().Get("dc"); other != "" {
|
||
|
*dc = other
|
||
|
} else if *dc == "" {
|
||
|
*dc = s.agent.config.Datacenter
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// parseToken is used to parse the ?token query param or the X-Consul-Token header
|
||
10 years ago
|
func (s *HTTPServer) parseToken(req *http.Request, token *string) {
|
||
|
if other := req.URL.Query().Get("token"); other != "" {
|
||
|
*token = other
|
||
10 years ago
|
return
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
if other := req.Header.Get("X-Consul-Token"); other != "" {
|
||
|
*token = other
|
||
|
return
|
||
|
}
|
||
|
|
||
10 years ago
|
// Set the default ACLToken
|
||
|
*token = s.agent.config.ACLToken
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// parseSource is used to parse the ?near=<node> query parameter, used for
|
||
|
// sorting by RTT based on a source node. We set the source's DC to the target
|
||
|
// DC in the request, if given, or else the agent's DC.
|
||
|
func (s *HTTPServer) parseSource(req *http.Request, source *structs.QuerySource) {
|
||
|
s.parseDC(req, &source.Datacenter)
|
||
|
if node := req.URL.Query().Get("near"); node != "" {
|
||
9 years ago
|
if node == "_agent" {
|
||
9 years ago
|
source.Node = s.agent.config.NodeName
|
||
|
} else {
|
||
|
source.Node = node
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
// parseMetaFilter is used to parse the ?node-meta=key:value query parameter, used for
|
||
|
// filtering results to nodes with the given metadata key/value
|
||
8 years ago
|
func (s *HTTPServer) parseMetaFilter(req *http.Request) map[string]string {
|
||
|
if filterList, ok := req.URL.Query()["node-meta"]; ok {
|
||
|
filters := make(map[string]string)
|
||
|
for _, filter := range filterList {
|
||
8 years ago
|
key, value := ParseMetaPair(filter)
|
||
8 years ago
|
filters[key] = value
|
||
8 years ago
|
}
|
||
8 years ago
|
return filters
|
||
8 years ago
|
}
|
||
8 years ago
|
return nil
|
||
8 years ago
|
}
|
||
|
|
||
11 years ago
|
// parse is a convenience method for endpoints that need
|
||
|
// to use both parseWait and parseDC.
|
||
11 years ago
|
func (s *HTTPServer) parse(resp http.ResponseWriter, req *http.Request, dc *string, b *structs.QueryOptions) bool {
|
||
11 years ago
|
s.parseDC(req, dc)
|
||
10 years ago
|
s.parseToken(req, &b.Token)
|
||
11 years ago
|
if parseConsistency(resp, req, b) {
|
||
|
return true
|
||
|
}
|
||
11 years ago
|
return parseWait(resp, req, b)
|
||
|
}
|