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