open-nomad/api/nodes.go

120 lines
3.0 KiB
Go
Raw Normal View History

2015-09-11 23:51:18 +00:00
package api
2015-09-12 00:20:33 +00:00
import (
2015-09-17 19:40:51 +00:00
"sort"
2015-09-12 00:20:33 +00:00
"strconv"
)
2015-09-11 23:51:18 +00:00
// Nodes is used to query node-related API endpoints
type Nodes struct {
client *Client
}
// Nodes returns a handle on the node endpoints.
func (c *Client) Nodes() *Nodes {
return &Nodes{client: c}
}
// List is used to list out all of the nodes
2015-09-14 02:55:47 +00:00
func (n *Nodes) List(q *QueryOptions) ([]*NodeListStub, *QueryMeta, error) {
2015-09-17 19:40:51 +00:00
var resp NodeIndexSort
2015-09-11 23:51:18 +00:00
qm, err := n.client.query("/v1/nodes", &resp, q)
if err != nil {
return nil, nil, err
}
2015-09-17 19:40:51 +00:00
sort.Sort(NodeIndexSort(resp))
2015-09-11 23:51:18 +00:00
return resp, qm, nil
}
// Info is used to query a specific node by its ID.
func (n *Nodes) Info(nodeID string, q *QueryOptions) (*Node, *QueryMeta, error) {
var resp Node
qm, err := n.client.query("/v1/node/"+nodeID, &resp, q)
if err != nil {
return nil, nil, err
}
return &resp, qm, nil
}
// ToggleDrain is used to toggle drain mode on/off for a given node.
func (n *Nodes) ToggleDrain(nodeID string, drain bool, q *WriteOptions) (*WriteMeta, error) {
2015-09-12 00:20:33 +00:00
drainArg := strconv.FormatBool(drain)
2015-09-11 23:51:18 +00:00
wm, err := n.client.write("/v1/node/"+nodeID+"/drain?enable="+drainArg, nil, nil, q)
if err != nil {
return nil, err
}
return wm, nil
}
// Allocations is used to return the allocations associated with a node.
2015-09-14 02:55:47 +00:00
func (n *Nodes) Allocations(nodeID string, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) {
var resp []*AllocationListStub
2015-09-11 23:51:18 +00:00
qm, err := n.client.query("/v1/node/"+nodeID+"/allocations", &resp, q)
if err != nil {
return nil, nil, err
}
2015-09-17 19:40:51 +00:00
sort.Sort(AllocIndexSort(resp))
2015-09-11 23:51:18 +00:00
return resp, qm, nil
}
// ForceEvaluate is used to force-evaluate an existing node.
func (n *Nodes) ForceEvaluate(nodeID string, q *WriteOptions) (string, *WriteMeta, error) {
var resp nodeEvalResponse
wm, err := n.client.write("/v1/node/"+nodeID+"/evaluate", nil, &resp, q)
if err != nil {
return "", nil, err
}
return resp.EvalID, wm, nil
}
// Node is used to deserialize a node entry.
type Node struct {
2015-09-14 02:55:47 +00:00
ID string
Datacenter string
Name string
Attributes map[string]string
Resources *Resources
Reserved *Resources
Links map[string]string
NodeClass string
Drain bool
Status string
StatusDescription string
CreateIndex uint64
ModifyIndex uint64
}
// NodeListStub is a subset of information returned during
// node list operations.
type NodeListStub struct {
2015-09-11 23:51:18 +00:00
ID string
Datacenter string
Name string
NodeClass string
Drain bool
Status string
StatusDescription string
CreateIndex uint64
ModifyIndex uint64
}
2015-09-17 19:40:51 +00:00
// NodeIndexSort reverse sorts nodes by CreateIndex
type NodeIndexSort []*NodeListStub
func (n NodeIndexSort) Len() int {
return len(n)
}
func (n NodeIndexSort) Less(i, j int) bool {
return n[i].CreateIndex > n[j].CreateIndex
}
func (n NodeIndexSort) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}
2015-09-11 23:51:18 +00:00
// nodeEvalResponse is used to decode a force-eval.
type nodeEvalResponse struct {
EvalID string
}