agent: Remove wrapQuery, call setMeta directly
This commit is contained in:
parent
4c039aa4d5
commit
53d3449947
|
@ -57,39 +57,41 @@ func (s *HTTPServer) CatalogDatacenters(resp http.ResponseWriter, req *http.Requ
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HTTPServer) CatalogNodes(resp http.ResponseWriter, req *http.Request) (uint64, interface{}, error) {
|
func (s *HTTPServer) CatalogNodes(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
// Setup the request
|
// Setup the request
|
||||||
args := structs.DCSpecificRequest{}
|
args := structs.DCSpecificRequest{}
|
||||||
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var out structs.IndexedNodes
|
var out structs.IndexedNodes
|
||||||
|
defer setMeta(resp, &out.QueryMeta)
|
||||||
if err := s.agent.RPC("Catalog.ListNodes", &args, &out); err != nil {
|
if err := s.agent.RPC("Catalog.ListNodes", &args, &out); err != nil {
|
||||||
return 0, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return out.Index, out.Nodes, nil
|
return out.Nodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HTTPServer) CatalogServices(resp http.ResponseWriter, req *http.Request) (uint64, interface{}, error) {
|
func (s *HTTPServer) CatalogServices(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
// Set default DC
|
// Set default DC
|
||||||
args := structs.DCSpecificRequest{}
|
args := structs.DCSpecificRequest{}
|
||||||
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var out structs.IndexedServices
|
var out structs.IndexedServices
|
||||||
|
defer setMeta(resp, &out.QueryMeta)
|
||||||
if err := s.agent.RPC("Catalog.ListServices", &args, &out); err != nil {
|
if err := s.agent.RPC("Catalog.ListServices", &args, &out); err != nil {
|
||||||
return 0, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return out.Index, out.Services, nil
|
return out.Services, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HTTPServer) CatalogServiceNodes(resp http.ResponseWriter, req *http.Request) (uint64, interface{}, error) {
|
func (s *HTTPServer) CatalogServiceNodes(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
// Set default DC
|
// Set default DC
|
||||||
args := structs.ServiceSpecificRequest{}
|
args := structs.ServiceSpecificRequest{}
|
||||||
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for a tag
|
// Check for a tag
|
||||||
|
@ -104,22 +106,23 @@ func (s *HTTPServer) CatalogServiceNodes(resp http.ResponseWriter, req *http.Req
|
||||||
if args.ServiceName == "" {
|
if args.ServiceName == "" {
|
||||||
resp.WriteHeader(400)
|
resp.WriteHeader(400)
|
||||||
resp.Write([]byte("Missing service name"))
|
resp.Write([]byte("Missing service name"))
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the RPC request
|
// Make the RPC request
|
||||||
var out structs.IndexedServiceNodes
|
var out structs.IndexedServiceNodes
|
||||||
|
defer setMeta(resp, &out.QueryMeta)
|
||||||
if err := s.agent.RPC("Catalog.ServiceNodes", &args, &out); err != nil {
|
if err := s.agent.RPC("Catalog.ServiceNodes", &args, &out); err != nil {
|
||||||
return 0, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return out.Index, out.ServiceNodes, nil
|
return out.ServiceNodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HTTPServer) CatalogNodeServices(resp http.ResponseWriter, req *http.Request) (uint64, interface{}, error) {
|
func (s *HTTPServer) CatalogNodeServices(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
// Set default Datacenter
|
// Set default Datacenter
|
||||||
args := structs.NodeSpecificRequest{}
|
args := structs.NodeSpecificRequest{}
|
||||||
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull out the node name
|
// Pull out the node name
|
||||||
|
@ -127,13 +130,14 @@ func (s *HTTPServer) CatalogNodeServices(resp http.ResponseWriter, req *http.Req
|
||||||
if args.Node == "" {
|
if args.Node == "" {
|
||||||
resp.WriteHeader(400)
|
resp.WriteHeader(400)
|
||||||
resp.Write([]byte("Missing node name"))
|
resp.Write([]byte("Missing node name"))
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the RPC request
|
// Make the RPC request
|
||||||
var out structs.IndexedNodeServices
|
var out structs.IndexedNodeServices
|
||||||
|
defer setMeta(resp, &out.QueryMeta)
|
||||||
if err := s.agent.RPC("Catalog.NodeServices", &args, &out); err != nil {
|
if err := s.agent.RPC("Catalog.NodeServices", &args, &out); err != nil {
|
||||||
return 0, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return out.Index, out.NodeServices, nil
|
return out.NodeServices, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,11 +6,11 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *HTTPServer) HealthChecksInState(resp http.ResponseWriter, req *http.Request) (uint64, interface{}, error) {
|
func (s *HTTPServer) HealthChecksInState(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
// Set default DC
|
// Set default DC
|
||||||
args := structs.ChecksInStateRequest{}
|
args := structs.ChecksInStateRequest{}
|
||||||
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull out the service name
|
// Pull out the service name
|
||||||
|
@ -18,22 +18,23 @@ func (s *HTTPServer) HealthChecksInState(resp http.ResponseWriter, req *http.Req
|
||||||
if args.State == "" {
|
if args.State == "" {
|
||||||
resp.WriteHeader(400)
|
resp.WriteHeader(400)
|
||||||
resp.Write([]byte("Missing check state"))
|
resp.Write([]byte("Missing check state"))
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the RPC request
|
// Make the RPC request
|
||||||
var out structs.IndexedHealthChecks
|
var out structs.IndexedHealthChecks
|
||||||
|
defer setMeta(resp, &out.QueryMeta)
|
||||||
if err := s.agent.RPC("Health.ChecksInState", &args, &out); err != nil {
|
if err := s.agent.RPC("Health.ChecksInState", &args, &out); err != nil {
|
||||||
return 0, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return out.Index, out.HealthChecks, nil
|
return out.HealthChecks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HTTPServer) HealthNodeChecks(resp http.ResponseWriter, req *http.Request) (uint64, interface{}, error) {
|
func (s *HTTPServer) HealthNodeChecks(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
// Set default DC
|
// Set default DC
|
||||||
args := structs.NodeSpecificRequest{}
|
args := structs.NodeSpecificRequest{}
|
||||||
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull out the service name
|
// Pull out the service name
|
||||||
|
@ -41,22 +42,23 @@ func (s *HTTPServer) HealthNodeChecks(resp http.ResponseWriter, req *http.Reques
|
||||||
if args.Node == "" {
|
if args.Node == "" {
|
||||||
resp.WriteHeader(400)
|
resp.WriteHeader(400)
|
||||||
resp.Write([]byte("Missing node name"))
|
resp.Write([]byte("Missing node name"))
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the RPC request
|
// Make the RPC request
|
||||||
var out structs.IndexedHealthChecks
|
var out structs.IndexedHealthChecks
|
||||||
|
defer setMeta(resp, &out.QueryMeta)
|
||||||
if err := s.agent.RPC("Health.NodeChecks", &args, &out); err != nil {
|
if err := s.agent.RPC("Health.NodeChecks", &args, &out); err != nil {
|
||||||
return 0, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return out.Index, out.HealthChecks, nil
|
return out.HealthChecks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HTTPServer) HealthServiceChecks(resp http.ResponseWriter, req *http.Request) (uint64, interface{}, error) {
|
func (s *HTTPServer) HealthServiceChecks(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
// Set default DC
|
// Set default DC
|
||||||
args := structs.ServiceSpecificRequest{}
|
args := structs.ServiceSpecificRequest{}
|
||||||
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull out the service name
|
// Pull out the service name
|
||||||
|
@ -64,22 +66,23 @@ func (s *HTTPServer) HealthServiceChecks(resp http.ResponseWriter, req *http.Req
|
||||||
if args.ServiceName == "" {
|
if args.ServiceName == "" {
|
||||||
resp.WriteHeader(400)
|
resp.WriteHeader(400)
|
||||||
resp.Write([]byte("Missing service name"))
|
resp.Write([]byte("Missing service name"))
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the RPC request
|
// Make the RPC request
|
||||||
var out structs.IndexedHealthChecks
|
var out structs.IndexedHealthChecks
|
||||||
|
defer setMeta(resp, &out.QueryMeta)
|
||||||
if err := s.agent.RPC("Health.ServiceChecks", &args, &out); err != nil {
|
if err := s.agent.RPC("Health.ServiceChecks", &args, &out); err != nil {
|
||||||
return 0, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return out.Index, out.HealthChecks, nil
|
return out.HealthChecks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Request) (uint64, interface{}, error) {
|
func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
// Set default DC
|
// Set default DC
|
||||||
args := structs.ServiceSpecificRequest{}
|
args := structs.ServiceSpecificRequest{}
|
||||||
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for a tag
|
// Check for a tag
|
||||||
|
@ -94,13 +97,14 @@ func (s *HTTPServer) HealthServiceNodes(resp http.ResponseWriter, req *http.Requ
|
||||||
if args.ServiceName == "" {
|
if args.ServiceName == "" {
|
||||||
resp.WriteHeader(400)
|
resp.WriteHeader(400)
|
||||||
resp.Write([]byte("Missing service name"))
|
resp.Write([]byte("Missing service name"))
|
||||||
return 0, nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the RPC request
|
// Make the RPC request
|
||||||
var out structs.IndexedCheckServiceNodes
|
var out structs.IndexedCheckServiceNodes
|
||||||
|
defer setMeta(resp, &out.QueryMeta)
|
||||||
if err := s.agent.RPC("Health.ServiceNodes", &args, &out); err != nil {
|
if err := s.agent.RPC("Health.ServiceNodes", &args, &out); err != nil {
|
||||||
return 0, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return out.Index, out.Nodes, nil
|
return out.Nodes, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,15 +63,15 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) {
|
||||||
s.mux.HandleFunc("/v1/catalog/register", s.wrap(s.CatalogRegister))
|
s.mux.HandleFunc("/v1/catalog/register", s.wrap(s.CatalogRegister))
|
||||||
s.mux.HandleFunc("/v1/catalog/deregister", s.wrap(s.CatalogDeregister))
|
s.mux.HandleFunc("/v1/catalog/deregister", s.wrap(s.CatalogDeregister))
|
||||||
s.mux.HandleFunc("/v1/catalog/datacenters", s.wrap(s.CatalogDatacenters))
|
s.mux.HandleFunc("/v1/catalog/datacenters", s.wrap(s.CatalogDatacenters))
|
||||||
s.mux.HandleFunc("/v1/catalog/nodes", s.wrapQuery(s.CatalogNodes))
|
s.mux.HandleFunc("/v1/catalog/nodes", s.wrap(s.CatalogNodes))
|
||||||
s.mux.HandleFunc("/v1/catalog/services", s.wrapQuery(s.CatalogServices))
|
s.mux.HandleFunc("/v1/catalog/services", s.wrap(s.CatalogServices))
|
||||||
s.mux.HandleFunc("/v1/catalog/service/", s.wrapQuery(s.CatalogServiceNodes))
|
s.mux.HandleFunc("/v1/catalog/service/", s.wrap(s.CatalogServiceNodes))
|
||||||
s.mux.HandleFunc("/v1/catalog/node/", s.wrapQuery(s.CatalogNodeServices))
|
s.mux.HandleFunc("/v1/catalog/node/", s.wrap(s.CatalogNodeServices))
|
||||||
|
|
||||||
s.mux.HandleFunc("/v1/health/node/", s.wrapQuery(s.HealthNodeChecks))
|
s.mux.HandleFunc("/v1/health/node/", s.wrap(s.HealthNodeChecks))
|
||||||
s.mux.HandleFunc("/v1/health/checks/", s.wrapQuery(s.HealthServiceChecks))
|
s.mux.HandleFunc("/v1/health/checks/", s.wrap(s.HealthServiceChecks))
|
||||||
s.mux.HandleFunc("/v1/health/state/", s.wrapQuery(s.HealthChecksInState))
|
s.mux.HandleFunc("/v1/health/state/", s.wrap(s.HealthChecksInState))
|
||||||
s.mux.HandleFunc("/v1/health/service/", s.wrapQuery(s.HealthServiceNodes))
|
s.mux.HandleFunc("/v1/health/service/", s.wrap(s.HealthServiceNodes))
|
||||||
|
|
||||||
s.mux.HandleFunc("/v1/agent/services", s.wrap(s.AgentServices))
|
s.mux.HandleFunc("/v1/agent/services", s.wrap(s.AgentServices))
|
||||||
s.mux.HandleFunc("/v1/agent/checks", s.wrap(s.AgentChecks))
|
s.mux.HandleFunc("/v1/agent/checks", s.wrap(s.AgentChecks))
|
||||||
|
@ -131,16 +131,6 @@ func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Reque
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
// wrapQuery is used to wrap query functions to make them more convenient
|
|
||||||
func (s *HTTPServer) wrapQuery(handler func(resp http.ResponseWriter, req *http.Request) (uint64, interface{}, error)) func(resp http.ResponseWriter, req *http.Request) {
|
|
||||||
f := func(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
||||||
idx, obj, err := handler(resp, req)
|
|
||||||
setIndex(resp, idx)
|
|
||||||
return obj, err
|
|
||||||
}
|
|
||||||
return s.wrap(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Renders a simple index page
|
// Renders a simple index page
|
||||||
func (s *HTTPServer) Index(resp http.ResponseWriter, req *http.Request) {
|
func (s *HTTPServer) Index(resp http.ResponseWriter, req *http.Request) {
|
||||||
if req.URL.Path == "/" {
|
if req.URL.Path == "/" {
|
||||||
|
@ -161,6 +151,28 @@ func setIndex(resp http.ResponseWriter, index uint64) {
|
||||||
resp.Header().Add("X-Consul-Index", strconv.FormatUint(index, 10))
|
resp.Header().Add("X-Consul-Index", strconv.FormatUint(index, 10))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setKnownLeader is used to set the known leader header
|
||||||
|
func setKnownLeader(resp http.ResponseWriter, known bool) {
|
||||||
|
s := "true"
|
||||||
|
if !known {
|
||||||
|
s = "false"
|
||||||
|
}
|
||||||
|
resp.Header().Add("X-Consul-KnownLeader", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// setLastContact is used to set the last contact header
|
||||||
|
func setLastContact(resp http.ResponseWriter, last time.Duration) {
|
||||||
|
lastMsec := uint64(last / time.Millisecond)
|
||||||
|
resp.Header().Add("X-Consul-LastContact", strconv.FormatUint(lastMsec, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
// parseWait is used to parse the ?wait and ?index query params
|
// parseWait is used to parse the ?wait and ?index query params
|
||||||
// Returns true on error
|
// Returns true on error
|
||||||
func parseWait(resp http.ResponseWriter, req *http.Request, b *structs.QueryOptions) bool {
|
func parseWait(resp http.ResponseWriter, req *http.Request, b *structs.QueryOptions) bool {
|
||||||
|
|
|
@ -47,10 +47,10 @@ func (s *HTTPServer) KVSGet(resp http.ResponseWriter, req *http.Request, args *s
|
||||||
|
|
||||||
// Make the RPC
|
// Make the RPC
|
||||||
var out structs.IndexedDirEntries
|
var out structs.IndexedDirEntries
|
||||||
|
defer setMeta(resp, &out.QueryMeta)
|
||||||
if err := s.agent.RPC(method, &args, &out); err != nil {
|
if err := s.agent.RPC(method, &args, &out); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
setIndex(resp, out.Index)
|
|
||||||
|
|
||||||
// Check if we get a not found
|
// Check if we get a not found
|
||||||
if len(out.Entries) == 0 {
|
if len(out.Entries) == 0 {
|
||||||
|
|
Loading…
Reference in a new issue