Skip to main content

nomad_rs_api/
acl_token.rs

1use crate::option::{QueryOptions, WriteOptions};
2use crate::{ClientError, Nomad};
3use reqwest::Method;
4use serde::{Deserialize, Serialize};
5use time;
6
7#[derive(Debug, Serialize, Deserialize)]
8#[serde(rename_all = "PascalCase")]
9pub struct ACLTokenBootstrapRequest {
10    pub bootstrap_secret: String,
11}
12
13impl ACLTokenBootstrapRequest {
14    pub fn new(bootstrap_secret: String) -> Self {
15        ACLTokenBootstrapRequest { bootstrap_secret }
16    }
17}
18
19#[derive(Debug, Serialize)]
20#[serde(rename_all = "PascalCase")]
21pub struct ACLTokenCreateRequest {
22    pub name: Option<String>,
23    #[serde(rename = "Type")]
24    pub token_type: String,
25    pub global: bool,
26    pub policies: Option<Vec<String>>,
27    pub roles: Option<Vec<ACLTokenRoleLink>>,
28    pub expiration_time: Option<String>,
29    pub expiration_ttl: Option<i64>,
30}
31
32impl ACLTokenCreateRequest {
33    pub fn new(token_type: String, global: bool) -> Self {
34        ACLTokenCreateRequest {
35            token_type,
36            global,
37            name: None,
38            policies: None,
39            roles: None,
40            expiration_time: None,
41            expiration_ttl: None,
42        }
43    }
44}
45
46#[derive(Debug, Serialize, Deserialize)]
47#[serde(rename_all = "PascalCase")]
48pub struct ACLToken {
49    #[serde(rename = "AccessorID")]
50    pub accessor_id: String,
51    #[serde(rename = "SecretID")]
52    pub secret_id: String,
53    pub name: Option<String>,
54    #[serde(rename = "Type")]
55    pub token_type: String,
56    pub policies: Option<Vec<String>>,
57    pub roles: Option<Vec<ACLTokenRoleLink>>,
58    pub global: bool,
59    #[serde(with = "time::serde::rfc3339")]
60    pub create_time: time::OffsetDateTime,
61    #[serde(with = "time::serde::rfc3339::option")]
62    pub expiration_time: Option<time::OffsetDateTime>,
63    pub expiration_ttl: Option<time::Duration>,
64    pub create_index: Option<u64>,
65    pub modify_index: Option<u64>,
66}
67
68#[derive(Debug, Serialize, Deserialize)]
69#[serde(rename_all = "PascalCase")]
70pub struct ACLTokenStub {
71    #[serde(rename = "AccessorID")]
72    pub accessor_id: String,
73    pub name: String,
74    #[serde(rename = "Type")]
75    pub token_type: String,
76    pub policies: Option<Vec<String>>,
77    pub roles: Option<Vec<ACLTokenRoleLink>>,
78    pub global: bool,
79    #[serde(with = "time::serde::rfc3339")]
80    pub create_time: time::OffsetDateTime,
81    #[serde(with = "time::serde::rfc3339::option")]
82    pub expiration_time: Option<time::OffsetDateTime>,
83    pub hash: String,
84    pub create_index: Option<u64>,
85    pub modify_index: Option<u64>,
86}
87
88#[derive(Debug, Serialize, Deserialize)]
89#[serde(rename_all = "PascalCase")]
90pub struct ACLTokenRoleLink {
91    #[serde(rename = "ID")]
92    pub id: String,
93    pub name: String,
94}
95
96pub struct Endpoint<'a> {
97    client: &'a Nomad,
98}
99
100impl<'a> Endpoint<'a> {
101    /// Create a new `Endpoint` with the given `Nomad` client to interact with
102    /// the ACL token endpoints.
103    pub fn new(client: &'a Nomad) -> Self {
104        Self { client }
105    }
106
107    /// Bootstrap the ACL subsystem.
108    ///
109    /// # Arguments
110    /// * `acl_token_bootstrap_request` - Optional bootstrapping parameters
111    ///   which allows specifying the secret ID of the bootstrap token.
112    /// * `opts` - Optional write options to use for the request.
113    ///
114    /// # Returns
115    /// A `Result` containing the created ACL bootstrap token object or an error
116    /// if the request fails.
117    pub async fn bootstrap(
118        &self,
119        acl_token_bootstrap_request: Option<&ACLTokenBootstrapRequest>,
120        opts: Option<WriteOptions>,
121    ) -> Result<ACLToken, ClientError> {
122        let req = self
123            .client
124            .set_request_write_options(
125                self.client.build_request(Method::POST, "/v1/acl/bootstrap"),
126                &opts.unwrap_or_default(),
127            )
128            .json(&acl_token_bootstrap_request);
129        self.client.send_with_response(req).await
130    }
131
132    /// Create an ACL token.
133    ///
134    /// # Arguments
135    /// * `acl_token_create_request` - The request object that defines the ACL
136    ///   token that should be created.
137    /// * `opts` - Optional write options to use for the request.
138    ///
139    /// # Returns
140    /// A `Result` containing the created ACL token object or an error if the
141    /// request fails.
142    pub async fn create(
143        &self,
144        acl_token_create_request: &ACLTokenCreateRequest,
145        opts: Option<WriteOptions>,
146    ) -> Result<ACLToken, ClientError> {
147        let req = self
148            .client
149            .set_request_write_options(
150                self.client.build_request(Method::POST, "/v1/acl/token"),
151                &opts.unwrap_or_default(),
152            )
153            .json(&acl_token_create_request);
154        self.client.send_with_response(req).await
155    }
156
157    /// Delete an ACL token by its accessor ID.
158    ///
159    /// # Arguments
160    /// * `accessor_id` - The accessor ID of the ACL token to delete.
161    /// * `opts` - Optional write options for the request.
162    ///
163    /// # Returns
164    /// A `Result` indicating success or failure of the operation.
165    pub async fn delete(
166        &self,
167        accessor_id: &str,
168        opts: Option<WriteOptions>,
169    ) -> Result<(), ClientError> {
170        let req = self.client.set_request_write_options(
171            self.client
172                .build_request(Method::DELETE, &format!("/v1/acl/token/{}", accessor_id)),
173            &opts.unwrap_or_default(),
174        );
175        self.client.send_without_response(req).await
176    }
177
178    /// Get an ACL token by its accessor ID.
179    ///
180    /// # Arguments
181    /// * `accessor_id` - The accessor ID of the ACL token to retrieve.
182    /// * `opts` - Optional query options for the request.
183    ///
184    /// # Returns
185    /// A `Result` containing the ACL token object or an error if the request
186    /// fails.
187    pub async fn get(
188        &self,
189        accessor_id: &str,
190        opts: Option<QueryOptions>,
191    ) -> Result<ACLToken, ClientError> {
192        let req = self.client.set_request_query_options(
193            self.client
194                .build_request(Method::GET, &format!("/v1/acl/token/{}", accessor_id)),
195            &opts.unwrap_or_default(),
196        );
197        self.client.send_with_response::<ACLToken>(req).await
198    }
199
200    /// Get an ACL token for the token used to authenticate the request.
201    ///
202    /// # Arguments
203    /// * `opts` - Optional query options for the request.
204    ///
205    /// # Returns
206    /// A `Result` containing the ACL token object or an error if the request
207    /// fails.
208    pub async fn get_self(&self, opts: Option<QueryOptions>) -> Result<ACLToken, ClientError> {
209        let req = self.client.set_request_query_options(
210            self.client.build_request(Method::GET, "/v1/acl/token/self"),
211            &opts.unwrap_or_default(),
212        );
213        self.client.send_with_response::<ACLToken>(req).await
214    }
215
216    /// Get the list of ACL tokens in the Nomad cluster.
217    ///
218    /// # Arguments
219    /// * `opts` - Optional query options to filter the results.
220    ///
221    /// # Returns
222    /// A `Result` containing a vector of `ACLTokenStub` objects or an error if
223    /// the request fails.
224    pub async fn list(&self, opts: Option<QueryOptions>) -> Result<Vec<ACLTokenStub>, ClientError> {
225        let req = self.client.set_request_query_options(
226            self.client.build_request(Method::GET, "/v1/acl/tokens"),
227            &opts.unwrap_or_default(),
228        );
229        self.client
230            .send_with_response::<Vec<ACLTokenStub>>(req)
231            .await
232    }
233}