2013-12-23 22:26:34 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2013-12-24 00:20:51 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2013-12-23 22:26:34 +00:00
|
|
|
"net/http"
|
2013-12-24 19:55:14 +00:00
|
|
|
"strings"
|
2013-12-23 22:26:34 +00:00
|
|
|
)
|
|
|
|
|
2013-12-24 00:20:51 +00:00
|
|
|
func (s *HTTPServer) CatalogRegister(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
var args structs.RegisterRequest
|
2014-04-21 22:02:36 +00:00
|
|
|
if err := decodeBody(req, &args, nil); err != nil {
|
2013-12-24 00:20:51 +00:00
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err)))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the default DC if not provided
|
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward to the servers
|
|
|
|
var out struct{}
|
|
|
|
if err := s.agent.RPC("Catalog.Register", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
func (s *HTTPServer) CatalogDeregister(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
var args structs.DeregisterRequest
|
2014-04-21 22:02:36 +00:00
|
|
|
if err := decodeBody(req, &args, nil); err != nil {
|
2013-12-24 19:55:14 +00:00
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err)))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the default DC if not provided
|
|
|
|
if args.Datacenter == "" {
|
|
|
|
args.Datacenter = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward to the servers
|
|
|
|
var out struct{}
|
|
|
|
if err := s.agent.RPC("Catalog.Deregister", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2013-12-24 00:20:51 +00:00
|
|
|
func (s *HTTPServer) CatalogDatacenters(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2013-12-23 22:26:34 +00:00
|
|
|
var out []string
|
|
|
|
if err := s.agent.RPC("Catalog.ListDatacenters", struct{}{}, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2013-12-24 00:20:51 +00:00
|
|
|
|
2014-04-21 19:40:11 +00:00
|
|
|
func (s *HTTPServer) CatalogNodes(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2014-02-05 22:36:13 +00:00
|
|
|
// Setup the request
|
|
|
|
args := structs.DCSpecificRequest{}
|
2015-06-30 21:25:40 +00:00
|
|
|
s.parseSource(req, &args.Source)
|
2014-04-21 19:25:36 +00:00
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
2014-04-21 19:40:11 +00:00
|
|
|
return nil, nil
|
2013-12-24 00:20:51 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 22:36:13 +00:00
|
|
|
var out structs.IndexedNodes
|
2014-04-21 19:40:11 +00:00
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
2014-02-05 22:36:13 +00:00
|
|
|
if err := s.agent.RPC("Catalog.ListNodes", &args, &out); err != nil {
|
2014-04-21 19:40:11 +00:00
|
|
|
return nil, err
|
2013-12-24 00:20:51 +00:00
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
|
|
|
|
// Use empty list instead of nil
|
|
|
|
if out.Nodes == nil {
|
|
|
|
out.Nodes = make(structs.Nodes, 0)
|
|
|
|
}
|
2014-04-21 19:40:11 +00:00
|
|
|
return out.Nodes, nil
|
2013-12-24 00:20:51 +00:00
|
|
|
}
|
2013-12-24 19:55:14 +00:00
|
|
|
|
2014-04-21 19:40:11 +00:00
|
|
|
func (s *HTTPServer) CatalogServices(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2013-12-24 19:55:14 +00:00
|
|
|
// Set default DC
|
2014-02-05 22:36:13 +00:00
|
|
|
args := structs.DCSpecificRequest{}
|
2014-04-21 19:25:36 +00:00
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
2014-04-21 19:40:11 +00:00
|
|
|
return nil, nil
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 22:36:13 +00:00
|
|
|
var out structs.IndexedServices
|
2014-04-21 19:40:11 +00:00
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
2014-02-05 22:36:13 +00:00
|
|
|
if err := s.agent.RPC("Catalog.ListServices", &args, &out); err != nil {
|
2014-04-21 19:40:11 +00:00
|
|
|
return nil, err
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
2014-04-21 19:40:11 +00:00
|
|
|
return out.Services, nil
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 19:40:11 +00:00
|
|
|
func (s *HTTPServer) CatalogServiceNodes(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2013-12-24 19:55:14 +00:00
|
|
|
// Set default DC
|
2014-02-05 22:36:13 +00:00
|
|
|
args := structs.ServiceSpecificRequest{}
|
2015-06-30 21:25:40 +00:00
|
|
|
s.parseSource(req, &args.Source)
|
2014-04-21 19:25:36 +00:00
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
2014-04-21 19:40:11 +00:00
|
|
|
return nil, nil
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a tag
|
2014-02-05 22:36:13 +00:00
|
|
|
params := req.URL.Query()
|
2013-12-24 19:55:14 +00:00
|
|
|
if _, ok := params["tag"]; ok {
|
|
|
|
args.ServiceTag = params.Get("tag")
|
|
|
|
args.TagFilter = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull out the service name
|
|
|
|
args.ServiceName = strings.TrimPrefix(req.URL.Path, "/v1/catalog/service/")
|
|
|
|
if args.ServiceName == "" {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Missing service name"))
|
2014-04-21 19:40:11 +00:00
|
|
|
return nil, nil
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the RPC request
|
2014-02-05 22:36:13 +00:00
|
|
|
var out structs.IndexedServiceNodes
|
2014-04-21 19:40:11 +00:00
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
2013-12-24 19:55:14 +00:00
|
|
|
if err := s.agent.RPC("Catalog.ServiceNodes", &args, &out); err != nil {
|
2014-04-21 19:40:11 +00:00
|
|
|
return nil, err
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
|
|
|
|
// Use empty list instead of nil
|
|
|
|
if out.ServiceNodes == nil {
|
|
|
|
out.ServiceNodes = make(structs.ServiceNodes, 0)
|
|
|
|
}
|
2014-04-21 19:40:11 +00:00
|
|
|
return out.ServiceNodes, nil
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 19:40:11 +00:00
|
|
|
func (s *HTTPServer) CatalogNodeServices(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2013-12-24 19:55:14 +00:00
|
|
|
// Set default Datacenter
|
2014-02-05 22:36:13 +00:00
|
|
|
args := structs.NodeSpecificRequest{}
|
2014-04-21 19:25:36 +00:00
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
2014-04-21 19:40:11 +00:00
|
|
|
return nil, nil
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pull out the node name
|
|
|
|
args.Node = strings.TrimPrefix(req.URL.Path, "/v1/catalog/node/")
|
|
|
|
if args.Node == "" {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Missing node name"))
|
2014-04-21 19:40:11 +00:00
|
|
|
return nil, nil
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the RPC request
|
2014-02-05 22:36:13 +00:00
|
|
|
var out structs.IndexedNodeServices
|
2014-04-21 19:40:11 +00:00
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
2014-02-05 22:36:13 +00:00
|
|
|
if err := s.agent.RPC("Catalog.NodeServices", &args, &out); err != nil {
|
2014-04-21 19:40:11 +00:00
|
|
|
return nil, err
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
2014-04-21 19:40:11 +00:00
|
|
|
return out.NodeServices, nil
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|