github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/api/nodes.go (about) 1 package api 2 3 import ( 4 "sort" 5 "strconv" 6 ) 7 8 // Nodes is used to query node-related API endpoints 9 type Nodes struct { 10 client *Client 11 } 12 13 // Nodes returns a handle on the node endpoints. 14 func (c *Client) Nodes() *Nodes { 15 return &Nodes{client: c} 16 } 17 18 // List is used to list out all of the nodes 19 func (n *Nodes) List(q *QueryOptions) ([]*NodeListStub, *QueryMeta, error) { 20 var resp NodeIndexSort 21 qm, err := n.client.query("/v1/nodes", &resp, q) 22 if err != nil { 23 return nil, nil, err 24 } 25 sort.Sort(NodeIndexSort(resp)) 26 return resp, qm, nil 27 } 28 29 // Info is used to query a specific node by its ID. 30 func (n *Nodes) Info(nodeID string, q *QueryOptions) (*Node, *QueryMeta, error) { 31 var resp Node 32 qm, err := n.client.query("/v1/node/"+nodeID, &resp, q) 33 if err != nil { 34 return nil, nil, err 35 } 36 return &resp, qm, nil 37 } 38 39 // ToggleDrain is used to toggle drain mode on/off for a given node. 40 func (n *Nodes) ToggleDrain(nodeID string, drain bool, q *WriteOptions) (*WriteMeta, error) { 41 drainArg := strconv.FormatBool(drain) 42 wm, err := n.client.write("/v1/node/"+nodeID+"/drain?enable="+drainArg, nil, nil, q) 43 if err != nil { 44 return nil, err 45 } 46 return wm, nil 47 } 48 49 // Allocations is used to return the allocations associated with a node. 50 func (n *Nodes) Allocations(nodeID string, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) { 51 var resp []*AllocationListStub 52 qm, err := n.client.query("/v1/node/"+nodeID+"/allocations", &resp, q) 53 if err != nil { 54 return nil, nil, err 55 } 56 sort.Sort(AllocIndexSort(resp)) 57 return resp, qm, nil 58 } 59 60 // ForceEvaluate is used to force-evaluate an existing node. 61 func (n *Nodes) ForceEvaluate(nodeID string, q *WriteOptions) (string, *WriteMeta, error) { 62 var resp nodeEvalResponse 63 wm, err := n.client.write("/v1/node/"+nodeID+"/evaluate", nil, &resp, q) 64 if err != nil { 65 return "", nil, err 66 } 67 return resp.EvalID, wm, nil 68 } 69 70 // Node is used to deserialize a node entry. 71 type Node struct { 72 ID string 73 Datacenter string 74 Name string 75 Attributes map[string]string 76 Resources *Resources 77 Reserved *Resources 78 Links map[string]string 79 NodeClass string 80 Drain bool 81 Status string 82 StatusDescription string 83 CreateIndex uint64 84 ModifyIndex uint64 85 } 86 87 // NodeListStub is a subset of information returned during 88 // node list operations. 89 type NodeListStub struct { 90 ID string 91 Datacenter string 92 Name string 93 NodeClass string 94 Drain bool 95 Status string 96 StatusDescription string 97 CreateIndex uint64 98 ModifyIndex uint64 99 } 100 101 // NodeIndexSort reverse sorts nodes by CreateIndex 102 type NodeIndexSort []*NodeListStub 103 104 func (n NodeIndexSort) Len() int { 105 return len(n) 106 } 107 108 func (n NodeIndexSort) Less(i, j int) bool { 109 return n[i].CreateIndex > n[j].CreateIndex 110 } 111 112 func (n NodeIndexSort) Swap(i, j int) { 113 n[i], n[j] = n[j], n[i] 114 } 115 116 // nodeEvalResponse is used to decode a force-eval. 117 type nodeEvalResponse struct { 118 EvalID string 119 }