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
// Copyright (C) 2023-2025 RabbitMQ Core Team (teamrabbitmq@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{
commons::{UserLimitTarget, VirtualHostLimitTarget},
path,
requests::EnforcedLimitParams,
responses,
};
use reqwest::StatusCode;
use serde_json::json;
use super::client::{Client, Result};
use std::fmt::Display;
impl<E, U, P> Client<E, U, P>
where
E: Display,
U: Display,
P: Display,
{
/// Sets a user limit (max-connections or max-channels).
///
/// Requires the `administrator` user tag.
pub async fn set_user_limit(
&self,
username: &str,
limit: EnforcedLimitParams<UserLimitTarget>,
) -> Result<()> {
let body = json!({"value": limit.value});
let _response = self
.http_put(
path!("user-limits", username, limit.kind),
&body,
None,
None,
)
.await?;
Ok(())
}
/// Clears a user limit.
///
/// Requires the `administrator` user tag.
pub async fn clear_user_limit(&self, username: &str, kind: UserLimitTarget) -> Result<()> {
let _response = self
.http_delete(path!("user-limits", username, kind), None, None)
.await?;
Ok(())
}
/// Lists all user limits in the cluster.
///
/// Requires the `administrator` user tag.
pub async fn list_all_user_limits(&self) -> Result<Vec<responses::UserLimits>> {
let response = self.http_get("user-limits", None, None).await?;
let response = response.json().await?;
Ok(response)
}
/// Lists limits for a specific user.
///
/// Requires the `administrator` user tag.
pub async fn list_user_limits(&self, username: &str) -> Result<Vec<responses::UserLimits>> {
let response = self
.http_get(path!("user-limits", username), None, None)
.await?;
let response = response.json().await?;
Ok(response)
}
/// Sets a [virtual host limit](https://www.rabbitmq.com/docs/vhosts#limits).
///
/// Requires the `administrator` user tag.
pub async fn set_vhost_limit(
&self,
vhost: &str,
limit: EnforcedLimitParams<VirtualHostLimitTarget>,
) -> Result<()> {
let body = json!({"value": limit.value});
let _response = self
.http_put(path!("vhost-limits", vhost, limit.kind), &body, None, None)
.await?;
Ok(())
}
/// Clears (removes) a [virtual host limit](https://www.rabbitmq.com/docs/vhosts#limits).
///
/// Requires the `administrator` user tag.
pub async fn clear_vhost_limit(&self, vhost: &str, kind: VirtualHostLimitTarget) -> Result<()> {
let _response = self
.http_delete(
path!("vhost-limits", vhost, kind),
Some(StatusCode::NOT_FOUND),
None,
)
.await?;
Ok(())
}
/// Lists all [virtual host limits](https://www.rabbitmq.com/docs/vhosts#limits) set in the cluster.
///
/// Requires the `administrator` user tag.
pub async fn list_all_vhost_limits(&self) -> Result<Vec<responses::VirtualHostLimits>> {
let response = self.http_get("vhost-limits", None, None).await?;
let response = response.json().await?;
Ok(response)
}
/// Lists the [limits of a given virtual host](https://www.rabbitmq.com/docs/vhosts#limits).
///
/// Requires the `administrator` user tag.
pub async fn list_vhost_limits(
&self,
vhost: &str,
) -> Result<Vec<responses::VirtualHostLimits>> {
let response = self
.http_get(path!("vhost-limits", vhost), None, None)
.await?;
let response = response.json().await?;
Ok(response)
}
}