github.com/emate/nomad@v0.8.2-wo-binpacking/api/namespace.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  )
     7  
     8  // Namespaces is used to query the namespace endpoints.
     9  type Namespaces struct {
    10  	client *Client
    11  }
    12  
    13  // Namespaces returns a new handle on the namespaces.
    14  func (c *Client) Namespaces() *Namespaces {
    15  	return &Namespaces{client: c}
    16  }
    17  
    18  // List is used to dump all of the namespaces.
    19  func (n *Namespaces) List(q *QueryOptions) ([]*Namespace, *QueryMeta, error) {
    20  	var resp []*Namespace
    21  	qm, err := n.client.query("/v1/namespaces", &resp, q)
    22  	if err != nil {
    23  		return nil, nil, err
    24  	}
    25  	sort.Sort(NamespaceIndexSort(resp))
    26  	return resp, qm, nil
    27  }
    28  
    29  // PrefixList is used to do a PrefixList search over namespaces
    30  func (n *Namespaces) PrefixList(prefix string, q *QueryOptions) ([]*Namespace, *QueryMeta, error) {
    31  	if q == nil {
    32  		q = &QueryOptions{Prefix: prefix}
    33  	} else {
    34  		q.Prefix = prefix
    35  	}
    36  
    37  	return n.List(q)
    38  }
    39  
    40  // Info is used to query a single namespace by its name.
    41  func (n *Namespaces) Info(name string, q *QueryOptions) (*Namespace, *QueryMeta, error) {
    42  	var resp Namespace
    43  	qm, err := n.client.query("/v1/namespace/"+name, &resp, q)
    44  	if err != nil {
    45  		return nil, nil, err
    46  	}
    47  	return &resp, qm, nil
    48  }
    49  
    50  // Register is used to register a namespace.
    51  func (n *Namespaces) Register(namespace *Namespace, q *WriteOptions) (*WriteMeta, error) {
    52  	wm, err := n.client.write("/v1/namespace", namespace, nil, q)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return wm, nil
    57  }
    58  
    59  // Delete is used to delete a namespace
    60  func (n *Namespaces) Delete(namespace string, q *WriteOptions) (*WriteMeta, error) {
    61  	wm, err := n.client.delete(fmt.Sprintf("/v1/namespace/%s", namespace), nil, q)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	return wm, nil
    66  }
    67  
    68  // Namespace is used to serialize a namespace.
    69  type Namespace struct {
    70  	Name        string
    71  	Description string
    72  	Quota       string
    73  	CreateIndex uint64
    74  	ModifyIndex uint64
    75  }
    76  
    77  // NamespaceIndexSort is a wrapper to sort Namespaces by CreateIndex. We
    78  // reverse the test so that we get the highest index first.
    79  type NamespaceIndexSort []*Namespace
    80  
    81  func (n NamespaceIndexSort) Len() int {
    82  	return len(n)
    83  }
    84  
    85  func (n NamespaceIndexSort) Less(i, j int) bool {
    86  	return n[i].CreateIndex > n[j].CreateIndex
    87  }
    88  
    89  func (n NamespaceIndexSort) Swap(i, j int) {
    90  	n[i], n[j] = n[j], n[i]
    91  }