2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Namespaces is used to query the namespace endpoints.
|
|
|
|
type Namespaces struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Namespaces returns a new handle on the namespaces.
|
|
|
|
func (c *Client) Namespaces() *Namespaces {
|
|
|
|
return &Namespaces{client: c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// List is used to dump all of the namespaces.
|
|
|
|
func (n *Namespaces) List(q *QueryOptions) ([]*Namespace, *QueryMeta, error) {
|
|
|
|
var resp []*Namespace
|
|
|
|
qm, err := n.client.query("/v1/namespaces", &resp, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
sort.Sort(NamespaceIndexSort(resp))
|
|
|
|
return resp, qm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrefixList is used to do a PrefixList search over namespaces
|
|
|
|
func (n *Namespaces) PrefixList(prefix string, q *QueryOptions) ([]*Namespace, *QueryMeta, error) {
|
|
|
|
if q == nil {
|
|
|
|
q = &QueryOptions{Prefix: prefix}
|
|
|
|
} else {
|
|
|
|
q.Prefix = prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.List(q)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info is used to query a single namespace by its name.
|
|
|
|
func (n *Namespaces) Info(name string, q *QueryOptions) (*Namespace, *QueryMeta, error) {
|
|
|
|
var resp Namespace
|
|
|
|
qm, err := n.client.query("/v1/namespace/"+name, &resp, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &resp, qm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register is used to register a namespace.
|
|
|
|
func (n *Namespaces) Register(namespace *Namespace, q *WriteOptions) (*WriteMeta, error) {
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := n.client.put("/v1/namespace", namespace, nil, q)
|
2017-09-07 23:56:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return wm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is used to delete a namespace
|
|
|
|
func (n *Namespaces) Delete(namespace string, q *WriteOptions) (*WriteMeta, error) {
|
2022-07-06 14:30:11 +00:00
|
|
|
wm, err := n.client.delete(fmt.Sprintf("/v1/namespace/%s", namespace), nil, nil, q)
|
2017-09-07 23:56:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return wm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace is used to serialize a namespace.
|
|
|
|
type Namespace struct {
|
2023-06-16 20:30:22 +00:00
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
Quota string
|
|
|
|
Capabilities *NamespaceCapabilities `hcl:"capabilities,block"`
|
|
|
|
NodePoolConfiguration *NamespaceNodePoolConfiguration `hcl:"node_pool_config,block"`
|
|
|
|
Meta map[string]string
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
2022-02-24 14:27:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-16 20:30:22 +00:00
|
|
|
// NamespaceCapabilities represents a set of capabilities allowed for this
|
|
|
|
// namespace, to be checked at job submission time.
|
2022-02-24 14:27:32 +00:00
|
|
|
type NamespaceCapabilities struct {
|
|
|
|
EnabledTaskDrivers []string `hcl:"enabled_task_drivers"`
|
|
|
|
DisabledTaskDrivers []string `hcl:"disabled_task_drivers"`
|
2017-09-07 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-06-16 20:30:22 +00:00
|
|
|
// NamespaceNodePoolConfiguration stores configuration about node pools for a
|
|
|
|
// namespace.
|
|
|
|
type NamespaceNodePoolConfiguration struct {
|
|
|
|
Default string
|
|
|
|
Allowed []string
|
|
|
|
Denied []string
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
// NamespaceIndexSort is a wrapper to sort Namespaces by CreateIndex. We
|
|
|
|
// reverse the test so that we get the highest index first.
|
|
|
|
type NamespaceIndexSort []*Namespace
|
|
|
|
|
|
|
|
func (n NamespaceIndexSort) Len() int {
|
|
|
|
return len(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n NamespaceIndexSort) Less(i, j int) bool {
|
|
|
|
return n[i].CreateIndex > n[j].CreateIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n NamespaceIndexSort) Swap(i, j int) {
|
|
|
|
n[i], n[j] = n[j], n[i]
|
|
|
|
}
|