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
//! `GET /api/policies`, `GET /api/operator-policies`, and related
//! policy endpoints.
use crate::api::encode_segment;
use crate::types::policy::Policy;
use crate::{Client, Result};
/// Request body for `PUT /api/policies/{vhost}/{name}` and
/// `PUT /api/operator-policies/{vhost}/{name}`. Name and vhost come
/// from the path, so the body carries only the policy attributes.
#[derive(serde::Serialize)]
struct PolicySet {
pattern: String,
definition: serde_json::Value,
priority: i32,
#[serde(rename = "apply-to")]
apply_to: String,
}
impl Client {
/// `GET /api/policies` — lists all policies across all vhosts.
pub async fn list_policies(&self) -> Result<Vec<Policy>> {
self.get("policies", None).await
}
/// `GET /api/policies/{vhost}` — lists all policies within a single
/// vhost. The vhost is percent-encoded.
pub async fn list_policies_in_vhost(&self, vhost: &str) -> Result<Vec<Policy>> {
self.get(&format!("policies/{}", encode_segment(vhost)), None)
.await
}
/// `GET /api/policies/{vhost}/{name}` — returns a single policy.
/// The vhost and name are percent-encoded.
pub async fn get_policy(&self, vhost: &str, name: &str) -> Result<Policy> {
self.get_ctx(
&format!(
"policies/{}/{}",
encode_segment(vhost),
encode_segment(name)
),
None,
&format!("policy '{name}' in vhost '{vhost}'"),
)
.await
}
/// `PUT /api/policies/{vhost}/{name}` — creates or updates a policy.
/// The vhost and name are percent-encoded.
pub async fn set_policy(
&self,
vhost: &str,
name: &str,
pattern: &str,
definition: serde_json::Value,
priority: i32,
apply_to: &str,
) -> Result<()> {
self.put(
&format!(
"policies/{}/{}",
encode_segment(vhost),
encode_segment(name)
),
&PolicySet {
pattern: pattern.to_string(),
definition,
priority,
apply_to: apply_to.to_string(),
},
)
.await
}
/// `DELETE /api/policies/{vhost}/{name}` — deletes a policy. The
/// vhost and name are percent-encoded.
pub async fn delete_policy(&self, vhost: &str, name: &str) -> Result<()> {
self.delete_ctx(
&format!(
"policies/{}/{}",
encode_segment(vhost),
encode_segment(name)
),
&format!("policy '{name}' in vhost '{vhost}'"),
)
.await
}
/// `GET /api/operator-policies` — lists all operator policies
/// across all vhosts.
pub async fn list_operator_policies(&self) -> Result<Vec<Policy>> {
self.get("operator-policies", None).await
}
/// `GET /api/operator-policies/{vhost}` — lists all operator
/// policies within a single vhost. The vhost is percent-encoded.
pub async fn list_operator_policies_in_vhost(&self, vhost: &str) -> Result<Vec<Policy>> {
self.get(
&format!("operator-policies/{}", encode_segment(vhost)),
None,
)
.await
}
/// `GET /api/operator-policies/{vhost}/{name}` — returns a single
/// operator policy. The vhost and name are percent-encoded.
pub async fn get_operator_policy(&self, vhost: &str, name: &str) -> Result<Policy> {
self.get_ctx(
&format!(
"operator-policies/{}/{}",
encode_segment(vhost),
encode_segment(name)
),
None,
&format!("operator policy '{name}' in vhost '{vhost}'"),
)
.await
}
/// `PUT /api/operator-policies/{vhost}/{name}` — creates or updates
/// an operator policy. The vhost and name are percent-encoded.
pub async fn set_operator_policy(
&self,
vhost: &str,
name: &str,
pattern: &str,
definition: serde_json::Value,
priority: i32,
apply_to: &str,
) -> Result<()> {
self.put(
&format!(
"operator-policies/{}/{}",
encode_segment(vhost),
encode_segment(name)
),
&PolicySet {
pattern: pattern.to_string(),
definition,
priority,
apply_to: apply_to.to_string(),
},
)
.await
}
/// `DELETE /api/operator-policies/{vhost}/{name}` — deletes an
/// operator policy. The vhost and name are percent-encoded.
pub async fn delete_operator_policy(&self, vhost: &str, name: &str) -> Result<()> {
self.delete_ctx(
&format!(
"operator-policies/{}/{}",
encode_segment(vhost),
encode_segment(name)
),
&format!("operator policy '{name}' in vhost '{vhost}'"),
)
.await
}
}