2013-12-19 20:03:57 +00:00
|
|
|
package structs
|
2013-12-11 22:04:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"github.com/ugorji/go/codec"
|
2014-02-05 18:21:31 +00:00
|
|
|
"time"
|
2013-12-11 22:04:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2013-12-19 23:08:55 +00:00
|
|
|
ErrNoLeader = fmt.Errorf("No cluster leader")
|
|
|
|
ErrNoDCPath = fmt.Errorf("No path to datacenter")
|
|
|
|
ErrNoServers = fmt.Errorf("No known Consul servers")
|
2013-12-11 22:04:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MessageType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
RegisterRequestType MessageType = iota
|
|
|
|
DeregisterRequestType
|
|
|
|
)
|
|
|
|
|
2014-01-08 19:21:29 +00:00
|
|
|
const (
|
|
|
|
HealthUnknown = "unknown"
|
|
|
|
HealthPassing = "passing"
|
|
|
|
HealthWarning = "warning"
|
|
|
|
HealthCritical = "critical"
|
|
|
|
)
|
|
|
|
|
2014-02-05 18:21:31 +00:00
|
|
|
// BlockingQuery is used to block on a query and wait for a change.
|
|
|
|
// Either both fields, or neither must be provided.
|
|
|
|
type BlockingQuery struct {
|
|
|
|
// If set, wait until query exceeds given index
|
|
|
|
MinQueryIndex uint64
|
|
|
|
|
|
|
|
// Provided with MinQueryIndex to wait for change
|
|
|
|
MaxQueryTime time.Duration
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// RegisterRequest is used for the Catalog.Register endpoint
|
|
|
|
// to register a node as providing a service. If no service
|
|
|
|
// is provided, the node is registered.
|
|
|
|
type RegisterRequest struct {
|
2014-01-08 21:39:40 +00:00
|
|
|
Datacenter string
|
|
|
|
Node string
|
|
|
|
Address string
|
|
|
|
Service *NodeService
|
|
|
|
Check *HealthCheck
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeregisterRequest is used for the Catalog.Deregister endpoint
|
|
|
|
// to deregister a node as providing a service. If no service is
|
|
|
|
// provided the entire node is deregistered.
|
|
|
|
type DeregisterRequest struct {
|
2014-01-06 22:18:38 +00:00
|
|
|
Datacenter string
|
|
|
|
Node string
|
|
|
|
ServiceID string
|
2014-01-08 21:39:40 +00:00
|
|
|
CheckID string
|
2013-12-11 22:04:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 18:21:31 +00:00
|
|
|
// DCSpecificRequest is used to query about a specific DC
|
|
|
|
type DCSpecificRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
BlockingQuery
|
|
|
|
}
|
|
|
|
|
2014-01-08 22:43:36 +00:00
|
|
|
// ServiceSpecificRequest is used to query about a specific node
|
|
|
|
type ServiceSpecificRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
ServiceName string
|
|
|
|
ServiceTag string
|
|
|
|
TagFilter bool // Controls tag filtering
|
2014-02-05 18:21:31 +00:00
|
|
|
BlockingQuery
|
2014-01-08 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeSpecificRequest is used to request the information about a single node
|
|
|
|
type NodeSpecificRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
Node string
|
2014-02-05 18:21:31 +00:00
|
|
|
BlockingQuery
|
2014-01-08 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChecksInStateRequest is used to query for nodes in a state
|
|
|
|
type ChecksInStateRequest struct {
|
|
|
|
Datacenter string
|
|
|
|
State string
|
2014-02-05 18:21:31 +00:00
|
|
|
BlockingQuery
|
2014-01-08 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 18:48:36 +00:00
|
|
|
// Used to return information about a node
|
|
|
|
type Node struct {
|
|
|
|
Node string
|
|
|
|
Address string
|
|
|
|
}
|
|
|
|
type Nodes []Node
|
|
|
|
|
2013-12-12 19:07:14 +00:00
|
|
|
// Used to return information about a provided services.
|
|
|
|
// Maps service name to available tags
|
|
|
|
type Services map[string][]string
|
|
|
|
|
2013-12-12 19:37:19 +00:00
|
|
|
// ServiceNode represents a node that is part of a service
|
|
|
|
type ServiceNode struct {
|
|
|
|
Node string
|
|
|
|
Address string
|
2014-01-06 22:18:38 +00:00
|
|
|
ServiceID string
|
2014-01-08 18:29:29 +00:00
|
|
|
ServiceName string
|
2013-12-12 19:37:19 +00:00
|
|
|
ServiceTag string
|
|
|
|
ServicePort int
|
|
|
|
}
|
|
|
|
type ServiceNodes []ServiceNode
|
|
|
|
|
2013-12-12 19:46:25 +00:00
|
|
|
// NodeService is a service provided by a node
|
|
|
|
type NodeService struct {
|
2014-01-06 22:18:38 +00:00
|
|
|
ID string
|
|
|
|
Service string
|
|
|
|
Tag string
|
|
|
|
Port int
|
2013-12-12 19:46:25 +00:00
|
|
|
}
|
2014-01-03 01:29:39 +00:00
|
|
|
type NodeServices struct {
|
2014-01-08 22:43:36 +00:00
|
|
|
Node Node
|
2014-01-08 18:29:29 +00:00
|
|
|
Services map[string]*NodeService
|
2014-01-03 01:29:39 +00:00
|
|
|
}
|
2013-12-12 19:46:25 +00:00
|
|
|
|
2014-01-08 19:21:29 +00:00
|
|
|
// HealthCheck represents a single check on a given node
|
|
|
|
type HealthCheck struct {
|
|
|
|
Node string
|
|
|
|
CheckID string // Unique per-node ID
|
|
|
|
Name string // Check name
|
|
|
|
Status string // The current check status
|
|
|
|
Notes string // Additional notes with the status
|
|
|
|
ServiceID string // optional associated service
|
|
|
|
ServiceName string // optional service name
|
|
|
|
}
|
2014-01-08 19:35:27 +00:00
|
|
|
type HealthChecks []*HealthCheck
|
2014-01-08 19:21:29 +00:00
|
|
|
|
2014-01-08 22:58:53 +00:00
|
|
|
// CheckServiceNode is used to provide the node, it's service
|
2014-01-08 22:43:36 +00:00
|
|
|
// definition, as well as a HealthCheck that is associated
|
2014-01-08 22:58:53 +00:00
|
|
|
type CheckServiceNode struct {
|
2014-01-08 22:43:36 +00:00
|
|
|
Node Node
|
|
|
|
Service NodeService
|
2014-01-08 22:58:53 +00:00
|
|
|
Checks HealthChecks
|
2014-01-08 21:52:09 +00:00
|
|
|
}
|
2014-01-08 22:58:53 +00:00
|
|
|
type CheckServiceNodes []CheckServiceNode
|
2014-01-08 21:52:09 +00:00
|
|
|
|
2014-02-05 18:44:28 +00:00
|
|
|
type IndexedNodes struct {
|
|
|
|
Index uint64
|
|
|
|
Nodes Nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedServices struct {
|
|
|
|
Index uint64
|
|
|
|
Services map[string][]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedServiceNodes struct {
|
|
|
|
Index uint64
|
|
|
|
ServiceNodes ServiceNodes
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedNodeServices struct {
|
|
|
|
Index uint64
|
|
|
|
NodeServices *NodeServices
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexedHealthChecks struct {
|
|
|
|
Index uint64
|
|
|
|
HealthChecks HealthChecks
|
|
|
|
}
|
|
|
|
|
2014-02-05 21:30:18 +00:00
|
|
|
type IndexedCheckServiceNodes struct {
|
|
|
|
Index uint64
|
|
|
|
Nodes CheckServiceNodes
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:04:44 +00:00
|
|
|
// Decode is used to decode a MsgPack encoded object
|
|
|
|
func Decode(buf []byte, out interface{}) error {
|
|
|
|
var handle codec.MsgpackHandle
|
|
|
|
return codec.NewDecoder(bytes.NewReader(buf), &handle).Decode(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode is used to encode a MsgPack object with type prefix
|
|
|
|
func Encode(t MessageType, msg interface{}) ([]byte, error) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
buf.WriteByte(uint8(t))
|
|
|
|
|
|
|
|
handle := codec.MsgpackHandle{}
|
|
|
|
encoder := codec.NewEncoder(buf, &handle)
|
|
|
|
err := encoder.Encode(msg)
|
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|