open-consul/consul/catalog_endpoint.go

86 lines
2.2 KiB
Go
Raw Normal View History

package consul
import (
"github.com/hashicorp/consul/rpc"
)
// Catalog endpoint is used to manipulate the service catalog
type Catalog struct {
2013-12-11 22:57:40 +00:00
srv *Server
}
/*
* Register : Registers that a node provides a given service
* Deregister : Deregisters that a node provides a given service
* ListDatacenters: List the known datacenters
* ListServices : Lists the available services
* ListNodes : Lists the available nodes
* ServiceNodes: Returns the nodes that are part of a service
* NodeServices: Returns the services that a node is registered for
*/
// Register is used register that a node is providing a given service.
2013-12-11 22:38:18 +00:00
func (c *Catalog) Register(args *rpc.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 {
return err
}
2013-12-11 22:57:40 +00:00
_, err := c.srv.raftApply(rpc.RegisterRequestType, args)
if err != nil {
2013-12-11 22:57:40 +00:00
c.srv.logger.Printf("[ERR] Register failed: %v", err)
return err
}
return nil
}
// Deregister is used to remove a service registration for a given node.
2013-12-11 22:38:18 +00:00
func (c *Catalog) Deregister(args *rpc.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
}
_, err := c.srv.raftApply(rpc.DeregisterRequestType, args)
if err != nil {
c.srv.logger.Printf("[ERR] Deregister failed: %v", err)
return err
}
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
func (c *Catalog) ListNodes(dc string, reply *rpc.Nodes) error {
if done, err := c.srv.forward("Catalog.ListNodes", dc, dc, reply); done {
return err
}
// Get the current nodes
state := c.srv.fsm.State()
rawNodes := state.Nodes()
// Format the response
nodes := rpc.Nodes(make([]rpc.Node, len(rawNodes)/2))
for i := 0; i < len(rawNodes); i += 2 {
nodes[i] = rpc.Node{rawNodes[i], rawNodes[i+1]}
}
*reply = nodes
return nil
}