Skip to main content

nomad_rs_api/
namespace.rs

1use crate::option::{QueryOptions, WriteOptions};
2use crate::{ClientError, Nomad};
3use reqwest::Method;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Serialize, Deserialize)]
7#[serde(rename_all = "PascalCase")]
8pub struct Namespace {
9    pub name: String,
10    pub description: Option<String>,
11    pub quota: Option<String>,
12    pub capabilities: Option<NamespaceCapabilities>,
13    pub node_pool_configuration: Option<NamespaceNodePoolConfiguration>,
14    pub vault_configuration: Option<NamespaceVaultConfiguration>,
15    pub consul_configuration: Option<NamespaceConsulConfiguration>,
16    pub meta: Option<std::collections::HashMap<String, String>>,
17    pub create_index: Option<u64>,
18    pub modify_index: Option<u64>,
19}
20
21impl Namespace {
22    pub fn new(name: String) -> Self {
23        Namespace {
24            name,
25            description: None,
26            quota: None,
27            capabilities: None,
28            node_pool_configuration: None,
29            vault_configuration: None,
30            consul_configuration: None,
31            meta: None,
32            create_index: None,
33            modify_index: None,
34        }
35    }
36}
37
38#[derive(Serialize, Deserialize, Debug)]
39#[serde(rename_all = "PascalCase")]
40pub struct NamespaceCapabilities {
41    pub enabled_task_drivers: Option<Vec<String>>,
42    pub disabled_task_drivers: Option<Vec<String>>,
43    pub enabled_network_modes: Option<Vec<String>>,
44    pub disabled_network_modes: Option<Vec<String>>,
45}
46
47#[derive(Serialize, Deserialize, Debug)]
48#[serde(rename_all = "PascalCase")]
49pub struct NamespaceNodePoolConfiguration {
50    pub default: Option<String>,
51    pub allowed: Option<Vec<String>>,
52    pub denied: Option<Vec<String>>,
53}
54
55#[derive(Serialize, Deserialize, Debug)]
56#[serde(rename_all = "PascalCase")]
57pub struct NamespaceVaultConfiguration {
58    pub default: String,
59    pub allowed: Option<Vec<String>>,
60    pub denied: Option<Vec<String>>,
61}
62
63#[derive(Serialize, Deserialize, Debug)]
64#[serde(rename_all = "PascalCase")]
65pub struct NamespaceConsulConfiguration {
66    pub default: String,
67    pub allowed: Option<Vec<String>>,
68    pub denied: Option<Vec<String>>,
69}
70
71pub struct Endpoint<'a> {
72    client: &'a Nomad,
73}
74
75impl<'a> Endpoint<'a> {
76    /// Create a new `Endpoint` with the given `Nomad` client to interact with
77    /// the namespace endpoints.
78    pub fn new(client: &'a Nomad) -> Self {
79        Self { client }
80    }
81
82    /// Create a new namespace in the Nomad cluster. This can also be used to
83    /// update an existing namespace by providing the same name.
84    ///
85    /// # Arguments
86    /// * `namespace` - The `Namespace` object containing the details of the
87    ///   namespace to create.
88    /// * `opts` - Optional write options to use for the request.
89    ///
90    /// # Returns
91    /// A `Result` containing the created namespace or an error if the request
92    /// fails.
93    pub async fn create(
94        &self,
95        namespace: &Namespace,
96        opts: Option<WriteOptions>,
97    ) -> Result<(), ClientError> {
98        let req = self
99            .client
100            .set_request_write_options(
101                self.client.build_request(Method::PUT, "/v1/namespace"),
102                &opts.unwrap_or_default(),
103            )
104            .json(&namespace);
105        self.client.send_without_response(req).await
106    }
107
108    /// Delete a namespace from the Nomad cluster.
109    ///
110    /// # Arguments
111    /// * `name` - The name of the namespace to delete.
112    /// * `opts` - Optional write options to use for the request.
113    ///
114    /// # Returns
115    /// A `Result` indicating success or failure of the deletion operation.
116    pub async fn delete(&self, name: &str, opts: Option<WriteOptions>) -> Result<(), ClientError> {
117        let req = self.client.set_request_write_options(
118            self.client
119                .build_request(Method::DELETE, &format!("/v1/namespace/{}", name)),
120            &opts.unwrap_or_default(),
121        );
122        self.client.send_without_response(req).await
123    }
124
125    /// Get details of a specific namespace by name.
126    ///
127    /// # Arguments
128    /// * `name` - The name of the namespace to retrieve.
129    /// * `opts` - Optional query options for the request.
130    ///
131    /// # Returns
132    /// A `Result` containing the namespace object or an error if the request
133    /// fails.
134    pub async fn get(
135        &self,
136        name: &str,
137        opts: Option<QueryOptions>,
138    ) -> Result<Namespace, ClientError> {
139        let req = self.client.set_request_query_options(
140            self.client
141                .build_request(Method::GET, &format!("/v1/namespace/{}", name)),
142            &opts.unwrap_or_default(),
143        );
144        self.client.send_with_response::<Namespace>(req).await
145    }
146
147    /// Get the list of namespaces in the Nomad cluster.
148    ///
149    /// # Arguments
150    /// * `opts` - Optional query options for the request.
151    ///
152    /// # Returns
153    /// A `Result` containing a vector of namespaces or an error if the request
154    /// fails.
155    pub async fn list(&self, opts: Option<QueryOptions>) -> Result<Vec<Namespace>, ClientError> {
156        let req = self.client.set_request_query_options(
157            self.client.build_request(Method::GET, "/v1/namespaces"),
158            &opts.unwrap_or_default(),
159        );
160        self.client.send_with_response::<Vec<Namespace>>(req).await
161    }
162}