Skip to main content

rabbitmq_http_client/blocking_api/
limits.rs

1// Copyright (C) 2023-2025 RabbitMQ Core Team (teamrabbitmq@gmail.com)
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{
16    commons::{UserLimitTarget, VirtualHostLimitTarget},
17    path,
18    requests::EnforcedLimitParams,
19    responses,
20};
21use reqwest::StatusCode;
22use serde_json::json;
23
24use super::client::{Client, Result};
25use std::fmt::Display;
26
27impl<E, U, P> Client<E, U, P>
28where
29    E: Display,
30    U: Display,
31    P: Display,
32{
33    /// Requires the `administrator` user tag.
34    pub fn set_user_limit(
35        &self,
36        username: &str,
37        limit: EnforcedLimitParams<UserLimitTarget>,
38    ) -> Result<()> {
39        let body = json!({"value": limit.value});
40        let _response = self.http_put(
41            path!("user-limits", username, limit.kind),
42            &body,
43            None,
44            None,
45        )?;
46        Ok(())
47    }
48
49    /// Requires the `administrator` user tag.
50    pub fn clear_user_limit(&self, username: &str, kind: UserLimitTarget) -> Result<()> {
51        let _response = self.http_delete(path!("user-limits", username, kind), None, None)?;
52        Ok(())
53    }
54
55    /// Requires the `administrator` user tag. Does not modify state.
56    pub fn list_all_user_limits(&self) -> Result<Vec<responses::UserLimits>> {
57        let response = self.http_get("user-limits", None, None)?;
58        let response = response.json()?;
59        Ok(response)
60    }
61
62    /// Requires the `administrator` user tag. Does not modify state.
63    pub fn list_user_limits(&self, username: &str) -> Result<Vec<responses::UserLimits>> {
64        let response = self.http_get(path!("user-limits", username), None, None)?;
65        let response = response.json()?;
66        Ok(response)
67    }
68
69    /// Sets a [virtual host limit](https://www.rabbitmq.com/docs/vhosts#limits).
70    ///
71    /// Requires the `administrator` user tag.
72    pub fn set_vhost_limit(
73        &self,
74        vhost: &str,
75        limit: EnforcedLimitParams<VirtualHostLimitTarget>,
76    ) -> Result<()> {
77        let body = json!({"value": limit.value});
78        let _response =
79            self.http_put(path!("vhost-limits", vhost, limit.kind), &body, None, None)?;
80        Ok(())
81    }
82
83    /// Clears (removes) a [virtual host limit](https://www.rabbitmq.com/docs/vhosts#limits).
84    ///
85    /// Requires the `administrator` user tag.
86    pub fn clear_vhost_limit(&self, vhost: &str, kind: VirtualHostLimitTarget) -> Result<()> {
87        let _response = self.http_delete(
88            path!("vhost-limits", vhost, kind),
89            Some(StatusCode::NOT_FOUND),
90            None,
91        )?;
92        Ok(())
93    }
94
95    /// Lists all [virtual host limits](https://www.rabbitmq.com/docs/vhosts#limits) set in the cluster.
96    ///
97    /// Requires the `administrator` user tag. Does not modify state.
98    pub fn list_all_vhost_limits(&self) -> Result<Vec<responses::VirtualHostLimits>> {
99        let response = self.http_get("vhost-limits", None, None)?;
100        let response = response.json()?;
101        Ok(response)
102    }
103
104    /// Lists the [limits of a given virtual host](https://www.rabbitmq.com/docs/vhosts#limits).
105    ///
106    /// Requires the `administrator` user tag. Does not modify state.
107    pub fn list_vhost_limits(&self, vhost: &str) -> Result<Vec<responses::VirtualHostLimits>> {
108        let response = self.http_get(path!("vhost-limits", vhost), None, None)?;
109        let response = response.json()?;
110        Ok(response)
111    }
112}