github.com/hashicorp/nomad/api@v0.0.0-20240306165712-3193ac204f65/namespace.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package api
     5  
     6  import (
     7  	"fmt"
     8  	"sort"
     9  )
    10  
    11  // Namespaces is used to query the namespace endpoints.
    12  type Namespaces struct {
    13  	client *Client
    14  }
    15  
    16  // Namespaces returns a new handle on the namespaces.
    17  func (c *Client) Namespaces() *Namespaces {
    18  	return &Namespaces{client: c}
    19  }
    20  
    21  // List is used to dump all of the namespaces.
    22  func (n *Namespaces) List(q *QueryOptions) ([]*Namespace, *QueryMeta, error) {
    23  	var resp []*Namespace
    24  	qm, err := n.client.query("/v1/namespaces", &resp, q)
    25  	if err != nil {
    26  		return nil, nil, err
    27  	}
    28  	sort.Sort(NamespaceIndexSort(resp))
    29  	return resp, qm, nil
    30  }
    31  
    32  // PrefixList is used to do a PrefixList search over namespaces
    33  func (n *Namespaces) PrefixList(prefix string, q *QueryOptions) ([]*Namespace, *QueryMeta, error) {
    34  	if q == nil {
    35  		q = &QueryOptions{Prefix: prefix}
    36  	} else {
    37  		q.Prefix = prefix
    38  	}
    39  
    40  	return n.List(q)
    41  }
    42  
    43  // Info is used to query a single namespace by its name.
    44  func (n *Namespaces) Info(name string, q *QueryOptions) (*Namespace, *QueryMeta, error) {
    45  	var resp Namespace
    46  	qm, err := n.client.query("/v1/namespace/"+name, &resp, q)
    47  	if err != nil {
    48  		return nil, nil, err
    49  	}
    50  	return &resp, qm, nil
    51  }
    52  
    53  // Register is used to register a namespace.
    54  func (n *Namespaces) Register(namespace *Namespace, q *WriteOptions) (*WriteMeta, error) {
    55  	wm, err := n.client.put("/v1/namespace", namespace, nil, q)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return wm, nil
    60  }
    61  
    62  // Delete is used to delete a namespace
    63  func (n *Namespaces) Delete(namespace string, q *WriteOptions) (*WriteMeta, error) {
    64  	wm, err := n.client.delete(fmt.Sprintf("/v1/namespace/%s", namespace), nil, nil, q)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	return wm, nil
    69  }
    70  
    71  // Namespace is used to serialize a namespace.
    72  type Namespace struct {
    73  	Name                  string
    74  	Description           string
    75  	Quota                 string
    76  	Capabilities          *NamespaceCapabilities          `hcl:"capabilities,block"`
    77  	NodePoolConfiguration *NamespaceNodePoolConfiguration `hcl:"node_pool_config,block"`
    78  	VaultConfiguration    *NamespaceVaultConfiguration    `hcl:"vault,block"`
    79  	ConsulConfiguration   *NamespaceConsulConfiguration   `hcl:"consul,block"`
    80  	Meta                  map[string]string
    81  	CreateIndex           uint64
    82  	ModifyIndex           uint64
    83  }
    84  
    85  // NamespaceCapabilities represents a set of capabilities allowed for this
    86  // namespace, to be checked at job submission time.
    87  type NamespaceCapabilities struct {
    88  	EnabledTaskDrivers  []string `hcl:"enabled_task_drivers"`
    89  	DisabledTaskDrivers []string `hcl:"disabled_task_drivers"`
    90  }
    91  
    92  // NamespaceNodePoolConfiguration stores configuration about node pools for a
    93  // namespace.
    94  type NamespaceNodePoolConfiguration struct {
    95  	Default string
    96  	Allowed []string
    97  	Denied  []string
    98  }
    99  
   100  // NamespaceVaultConfiguration stores configuration about permissions to Vault
   101  // clusters for a namespace, for use with Nomad Enterprise.
   102  type NamespaceVaultConfiguration struct {
   103  	// Default is the Vault cluster used by jobs in this namespace that don't
   104  	// specify a cluster of their own.
   105  	Default string
   106  
   107  	// Allowed specifies the Vault clusters that are allowed to be used by jobs
   108  	// in this namespace. By default, all clusters are allowed. If an empty list
   109  	// is provided only the namespace's default cluster is allowed. This field
   110  	// supports wildcard globbing through the use of `*` for multi-character
   111  	// matching. This field cannot be used with Denied.
   112  	Allowed []string
   113  
   114  	// Denied specifies the Vault clusters that are not allowed to be used by
   115  	// jobs in this namespace. This field supports wildcard globbing through the
   116  	// use of `*` for multi-character matching. If specified, any cluster is
   117  	// allowed to be used, except for those that match any of these patterns.
   118  	// This field cannot be used with Allowed.
   119  	Denied []string
   120  }
   121  
   122  // NamespaceConsulConfiguration stores configuration about permissions to Consul
   123  // clusters for a namespace, for use with Nomad Enterprise.
   124  type NamespaceConsulConfiguration struct {
   125  	// Default is the Consul cluster used by jobs in this namespace that don't
   126  	// specify a cluster of their own.
   127  	Default string
   128  
   129  	// Allowed specifies the Consul clusters that are allowed to be used by jobs
   130  	// in this namespace. By default, all clusters are allowed. If an empty list
   131  	// is provided only the namespace's default cluster is allowed. This field
   132  	// supports wildcard globbing through the use of `*` for multi-character
   133  	// matching. This field cannot be used with Denied.
   134  	Allowed []string
   135  
   136  	// Denied specifies the Consul clusters that are not allowed to be used by
   137  	// jobs in this namespace. This field supports wildcard globbing through the
   138  	// use of `*` for multi-character matching. If specified, any cluster is
   139  	// allowed to be used, except for those that match any of these patterns.
   140  	// This field cannot be used with Allowed.
   141  	Denied []string
   142  }
   143  
   144  // NamespaceIndexSort is a wrapper to sort Namespaces by CreateIndex. We
   145  // reverse the test so that we get the highest index first.
   146  type NamespaceIndexSort []*Namespace
   147  
   148  func (n NamespaceIndexSort) Len() int {
   149  	return len(n)
   150  }
   151  
   152  func (n NamespaceIndexSort) Less(i, j int) bool {
   153  	return n[i].CreateIndex > n[j].CreateIndex
   154  }
   155  
   156  func (n NamespaceIndexSort) Swap(i, j int) {
   157  	n[i], n[j] = n[j], n[i]
   158  }