github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/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  func (n *Nodes) PrefixList(prefix string) ([]*NodeListStub, *QueryMeta, error) {
    30  	return n.List(&QueryOptions{Prefix: prefix})
    31  }
    32  
    33  // Info is used to query a specific node by its ID.
    34  func (n *Nodes) Info(nodeID string, q *QueryOptions) (*Node, *QueryMeta, error) {
    35  	var resp Node
    36  	qm, err := n.client.query("/v1/node/"+nodeID, &resp, q)
    37  	if err != nil {
    38  		return nil, nil, err
    39  	}
    40  	return &resp, qm, nil
    41  }
    42  
    43  // ToggleDrain is used to toggle drain mode on/off for a given node.
    44  func (n *Nodes) ToggleDrain(nodeID string, drain bool, q *WriteOptions) (*WriteMeta, error) {
    45  	drainArg := strconv.FormatBool(drain)
    46  	wm, err := n.client.write("/v1/node/"+nodeID+"/drain?enable="+drainArg, nil, nil, q)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	return wm, nil
    51  }
    52  
    53  // Allocations is used to return the allocations associated with a node.
    54  func (n *Nodes) Allocations(nodeID string, q *QueryOptions) ([]*Allocation, *QueryMeta, error) {
    55  	var resp []*Allocation
    56  	qm, err := n.client.query("/v1/node/"+nodeID+"/allocations", &resp, q)
    57  	if err != nil {
    58  		return nil, nil, err
    59  	}
    60  	sort.Sort(AllocationSort(resp))
    61  	return resp, qm, nil
    62  }
    63  
    64  // ForceEvaluate is used to force-evaluate an existing node.
    65  func (n *Nodes) ForceEvaluate(nodeID string, q *WriteOptions) (string, *WriteMeta, error) {
    66  	var resp nodeEvalResponse
    67  	wm, err := n.client.write("/v1/node/"+nodeID+"/evaluate", nil, &resp, q)
    68  	if err != nil {
    69  		return "", nil, err
    70  	}
    71  	return resp.EvalID, wm, nil
    72  }
    73  
    74  // Node is used to deserialize a node entry.
    75  type Node struct {
    76  	ID                string
    77  	Datacenter        string
    78  	Name              string
    79  	HTTPAddr          string
    80  	Attributes        map[string]string
    81  	Resources         *Resources
    82  	Reserved          *Resources
    83  	Links             map[string]string
    84  	Meta              map[string]string
    85  	NodeClass         string
    86  	Drain             bool
    87  	Status            string
    88  	StatusDescription string
    89  	CreateIndex       uint64
    90  	ModifyIndex       uint64
    91  }
    92  
    93  // NodeListStub is a subset of information returned during
    94  // node list operations.
    95  type NodeListStub struct {
    96  	ID                string
    97  	Datacenter        string
    98  	Name              string
    99  	NodeClass         string
   100  	Drain             bool
   101  	Status            string
   102  	StatusDescription string
   103  	CreateIndex       uint64
   104  	ModifyIndex       uint64
   105  }
   106  
   107  // NodeIndexSort reverse sorts nodes by CreateIndex
   108  type NodeIndexSort []*NodeListStub
   109  
   110  func (n NodeIndexSort) Len() int {
   111  	return len(n)
   112  }
   113  
   114  func (n NodeIndexSort) Less(i, j int) bool {
   115  	return n[i].CreateIndex > n[j].CreateIndex
   116  }
   117  
   118  func (n NodeIndexSort) Swap(i, j int) {
   119  	n[i], n[j] = n[j], n[i]
   120  }
   121  
   122  // nodeEvalResponse is used to decode a force-eval.
   123  type nodeEvalResponse struct {
   124  	EvalID string
   125  }
   126  
   127  // AllocationSort reverse sorts allocs by CreateIndex.
   128  type AllocationSort []*Allocation
   129  
   130  func (a AllocationSort) Len() int {
   131  	return len(a)
   132  }
   133  
   134  func (a AllocationSort) Less(i, j int) bool {
   135  	return a[i].CreateIndex > a[j].CreateIndex
   136  }
   137  
   138  func (a AllocationSort) Swap(i, j int) {
   139  	a[i], a[j] = a[j], a[i]
   140  }