2013-12-11 22:04:44 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2013-12-24 20:43:34 +00:00
|
|
|
"fmt"
|
2013-12-19 20:03:57 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2013-12-11 22:04:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Catalog endpoint is used to manipulate the service catalog
|
|
|
|
type Catalog struct {
|
2013-12-11 22:57:40 +00:00
|
|
|
srv *Server
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register is used register that a node is providing a given service.
|
2013-12-19 20:03:57 +00:00
|
|
|
func (c *Catalog) Register(args *structs.RegisterRequest, reply *struct{}) error {
|
2013-12-11 22:57:40 +00:00
|
|
|
if done, err := c.srv.forward("Catalog.Register", args.Datacenter, args, reply); done {
|
2013-12-11 22:04:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:43:34 +00:00
|
|
|
// Verify the args
|
|
|
|
if args.Node == "" || args.Address == "" {
|
|
|
|
return fmt.Errorf("Must provide node and address")
|
|
|
|
}
|
|
|
|
|
2014-01-06 22:18:38 +00:00
|
|
|
// If no service id, but service name, use default
|
|
|
|
if args.ServiceID == "" && args.ServiceName != "" {
|
|
|
|
args.ServiceID = args.ServiceName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify ServiceName provided if ID
|
|
|
|
if args.ServiceID != "" && args.ServiceName == "" {
|
|
|
|
return fmt.Errorf("Must provide service name with ID")
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
_, err := c.srv.raftApply(structs.RegisterRequestType, args)
|
2013-12-11 22:04:44 +00:00
|
|
|
if err != nil {
|
2013-12-11 22:57:40 +00:00
|
|
|
c.srv.logger.Printf("[ERR] Register failed: %v", err)
|
2013-12-11 22:04:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deregister is used to remove a service registration for a given node.
|
2013-12-19 20:03:57 +00:00
|
|
|
func (c *Catalog) Deregister(args *structs.DeregisterRequest, reply *struct{}) error {
|
2013-12-11 23:34:10 +00:00
|
|
|
if done, err := c.srv.forward("Catalog.Deregister", args.Datacenter, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:43:34 +00:00
|
|
|
// Verify the args
|
|
|
|
if args.Node == "" {
|
|
|
|
return fmt.Errorf("Must provide node")
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
_, err := c.srv.raftApply(structs.DeregisterRequestType, args)
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
c.srv.logger.Printf("[ERR] Deregister failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2013-12-11 22:04:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
2013-12-12 18:35:50 +00:00
|
|
|
|
|
|
|
// ListDatacenters is used to query for the list of known datacenters
|
|
|
|
func (c *Catalog) ListDatacenters(args *struct{}, reply *[]string) error {
|
|
|
|
c.srv.remoteLock.RLock()
|
|
|
|
defer c.srv.remoteLock.RUnlock()
|
|
|
|
|
|
|
|
// Read the known DCs
|
|
|
|
var dcs []string
|
|
|
|
for dc := range c.srv.remoteConsuls {
|
|
|
|
dcs = append(dcs, dc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return
|
|
|
|
*reply = dcs
|
|
|
|
return nil
|
|
|
|
}
|
2013-12-12 18:48:36 +00:00
|
|
|
|
|
|
|
// ListNodes is used to query the nodes in a DC
|
2013-12-19 20:03:57 +00:00
|
|
|
func (c *Catalog) ListNodes(dc string, reply *structs.Nodes) error {
|
2013-12-12 18:48:36 +00:00
|
|
|
if done, err := c.srv.forward("Catalog.ListNodes", dc, dc, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current nodes
|
|
|
|
state := c.srv.fsm.State()
|
2014-01-08 18:31:42 +00:00
|
|
|
nodes := state.Nodes()
|
2013-12-12 18:48:36 +00:00
|
|
|
|
|
|
|
*reply = nodes
|
|
|
|
return nil
|
|
|
|
}
|
2013-12-12 19:07:14 +00:00
|
|
|
|
|
|
|
// ListServices is used to query the services in a DC
|
2013-12-19 20:03:57 +00:00
|
|
|
func (c *Catalog) ListServices(dc string, reply *structs.Services) error {
|
2013-12-12 19:07:14 +00:00
|
|
|
if done, err := c.srv.forward("Catalog.ListServices", dc, dc, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current nodes
|
|
|
|
state := c.srv.fsm.State()
|
|
|
|
services := state.Services()
|
|
|
|
|
|
|
|
*reply = services
|
|
|
|
return nil
|
|
|
|
}
|
2013-12-12 19:37:19 +00:00
|
|
|
|
|
|
|
// ServiceNodes returns all the nodes registered as part of a service
|
2013-12-19 20:03:57 +00:00
|
|
|
func (c *Catalog) ServiceNodes(args *structs.ServiceNodesRequest, reply *structs.ServiceNodes) error {
|
2013-12-12 19:37:19 +00:00
|
|
|
if done, err := c.srv.forward("Catalog.ServiceNodes", args.Datacenter, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:43:34 +00:00
|
|
|
// Verify the arguments
|
|
|
|
if args.ServiceName == "" {
|
|
|
|
return fmt.Errorf("Must provide service name")
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:37:19 +00:00
|
|
|
// Get the nodes
|
|
|
|
state := c.srv.fsm.State()
|
2013-12-19 20:03:57 +00:00
|
|
|
var nodes structs.ServiceNodes
|
2013-12-12 19:37:19 +00:00
|
|
|
if args.TagFilter {
|
|
|
|
nodes = state.ServiceTagNodes(args.ServiceName, args.ServiceTag)
|
|
|
|
} else {
|
|
|
|
nodes = state.ServiceNodes(args.ServiceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = nodes
|
|
|
|
return nil
|
|
|
|
}
|
2013-12-12 19:46:25 +00:00
|
|
|
|
|
|
|
// NodeServices returns all the services registered as part of a node
|
2013-12-19 20:03:57 +00:00
|
|
|
func (c *Catalog) NodeServices(args *structs.NodeServicesRequest, reply *structs.NodeServices) error {
|
2013-12-12 19:46:25 +00:00
|
|
|
if done, err := c.srv.forward("Catalog.NodeServices", args.Datacenter, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:43:34 +00:00
|
|
|
// Verify the arguments
|
|
|
|
if args.Node == "" {
|
|
|
|
return fmt.Errorf("Must provide node")
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:46:25 +00:00
|
|
|
// Get the node services
|
|
|
|
state := c.srv.fsm.State()
|
|
|
|
services := state.NodeServices(args.Node)
|
|
|
|
|
2014-01-03 01:29:39 +00:00
|
|
|
*reply = *services
|
2013-12-12 19:46:25 +00:00
|
|
|
return nil
|
|
|
|
}
|