Skip to main content

openstack_sdk_load_balancer/v2/quota/
set.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14//
15// WARNING: This file is automatically generated from OpenAPI schema using
16// `openstack-codegenerator`.
17
18//! Updates a quota for a project.
19//!
20//! If the request is valid, the service returns the `Accepted (202)` response
21//! code.
22//!
23//! This operation returns the updated quota object.
24//!
25//! If the quota is specified as `null` the quota will use the deployment
26//! default quota settings.
27//!
28//! Specifying a quota of `-1` means the quota is unlimited.
29//!
30//! Specifying a quota of `0` means the project cannot create any of the
31//! resource.
32//!
33use derive_builder::Builder;
34use http::{HeaderMap, HeaderName, HeaderValue};
35
36use openstack_sdk_core::api::rest_endpoint_prelude::*;
37
38use serde::Deserialize;
39use serde::Serialize;
40use std::borrow::Cow;
41
42/// Base type for complex types
43#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
44#[builder(setter(strip_option))]
45pub struct Quota {
46    #[serde(skip_serializing_if = "Option::is_none")]
47    #[builder(default, setter(into))]
48    pub(crate) health_monitor: Option<i32>,
49
50    /// The configured health monitor quota limit. A setting of `null` means it
51    /// is using the deployment default quota. A setting of `-1` means
52    /// unlimited.
53    #[serde(skip_serializing_if = "Option::is_none")]
54    #[builder(default, setter(into))]
55    pub(crate) healthmonitor: Option<i32>,
56
57    /// The configured l7policy quota limit. A setting of `null` means it is
58    /// using the deployment default quota. A setting of `-1` means unlimited.
59    #[serde(skip_serializing_if = "Option::is_none")]
60    #[builder(default, setter(into))]
61    pub(crate) l7policy: Option<i32>,
62
63    /// The configured l7rule quota limit. A setting of `null` means it is
64    /// using the deployment default quota. A setting of `-1` means unlimited.
65    #[serde(skip_serializing_if = "Option::is_none")]
66    #[builder(default, setter(into))]
67    pub(crate) l7rule: Option<i32>,
68
69    /// The configured listener quota limit. A setting of `null` means it is
70    /// using the deployment default quota. A setting of `-1` means unlimited.
71    #[serde(skip_serializing_if = "Option::is_none")]
72    #[builder(default, setter(into))]
73    pub(crate) listener: Option<i32>,
74
75    #[serde(skip_serializing_if = "Option::is_none")]
76    #[builder(default, setter(into))]
77    pub(crate) load_balancer: Option<i32>,
78
79    /// The configured load balancer quota limit. A setting of `null` means it
80    /// is using the deployment default quota. A setting of `-1` means
81    /// unlimited.
82    #[serde(skip_serializing_if = "Option::is_none")]
83    #[builder(default, setter(into))]
84    pub(crate) loadbalancer: Option<i32>,
85
86    /// The configured member quota limit. A setting of `null` means it is
87    /// using the deployment default quota. A setting of `-1` means unlimited.
88    #[serde(skip_serializing_if = "Option::is_none")]
89    #[builder(default, setter(into))]
90    pub(crate) member: Option<i32>,
91
92    /// The configured pool quota limit. A setting of `null` means it is using
93    /// the deployment default quota. A setting of `-1` means unlimited.
94    #[serde(skip_serializing_if = "Option::is_none")]
95    #[builder(default, setter(into))]
96    pub(crate) pool: Option<i32>,
97}
98
99#[derive(Builder, Debug, Clone)]
100#[builder(setter(strip_option))]
101pub struct Request<'a> {
102    /// Base type for complex types
103    #[builder(setter(into))]
104    pub(crate) quota: Quota,
105
106    /// project_id parameter for /v2/lbaas/quotas/{project_id} API
107    #[builder(default, setter(into))]
108    project_id: Cow<'a, str>,
109
110    #[builder(setter(name = "_headers"), default, private)]
111    _headers: Option<HeaderMap>,
112}
113impl<'a> Request<'a> {
114    /// Create a builder for the endpoint.
115    pub fn builder() -> RequestBuilder<'a> {
116        RequestBuilder::default()
117    }
118}
119
120impl<'a> RequestBuilder<'a> {
121    /// Add a single header to the Quota.
122    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
123    where
124        K: Into<HeaderName>,
125        V: Into<HeaderValue>,
126    {
127        self._headers
128            .get_or_insert(None)
129            .get_or_insert_with(HeaderMap::new)
130            .insert(header_name.into(), header_value.into());
131        self
132    }
133
134    /// Add multiple headers.
135    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
136    where
137        I: Iterator<Item = T>,
138        T: Into<(Option<HeaderName>, HeaderValue)>,
139    {
140        self._headers
141            .get_or_insert(None)
142            .get_or_insert_with(HeaderMap::new)
143            .extend(iter.map(Into::into));
144        self
145    }
146}
147
148impl RestEndpoint for Request<'_> {
149    fn method(&self) -> http::Method {
150        http::Method::PUT
151    }
152
153    fn endpoint(&self) -> Cow<'static, str> {
154        format!(
155            "lbaas/quotas/{project_id}",
156            project_id = self.project_id.as_ref(),
157        )
158        .into()
159    }
160
161    fn parameters(&self) -> QueryParams<'_> {
162        QueryParams::default()
163    }
164
165    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
166        let mut params = JsonBodyParams::default();
167
168        params.push("quota", serde_json::to_value(&self.quota)?);
169
170        params.into_body()
171    }
172
173    fn service_type(&self) -> ServiceType {
174        ServiceType::LoadBalancer
175    }
176
177    fn response_key(&self) -> Option<Cow<'static, str>> {
178        Some("quota".into())
179    }
180
181    /// Returns headers to be set into the request
182    fn request_headers(&self) -> Option<&HeaderMap> {
183        self._headers.as_ref()
184    }
185
186    /// Returns required API version
187    fn api_version(&self) -> Option<ApiVersion> {
188        Some(ApiVersion::new(2, 0))
189    }
190}
191
192#[cfg(test)]
193mod tests {
194    use super::*;
195    use http::{HeaderName, HeaderValue};
196    use httpmock::MockServer;
197    #[cfg(feature = "sync")]
198    use openstack_sdk_core::api::Query;
199    use openstack_sdk_core::test::client::FakeOpenStackClient;
200    use openstack_sdk_core::types::ServiceType;
201    use serde_json::json;
202
203    #[test]
204    fn test_service_type() {
205        assert_eq!(
206            Request::builder()
207                .quota(QuotaBuilder::default().build().unwrap())
208                .build()
209                .unwrap()
210                .service_type(),
211            ServiceType::LoadBalancer
212        );
213    }
214
215    #[test]
216    fn test_response_key() {
217        assert_eq!(
218            Request::builder()
219                .quota(QuotaBuilder::default().build().unwrap())
220                .build()
221                .unwrap()
222                .response_key()
223                .unwrap(),
224            "quota"
225        );
226    }
227
228    #[cfg(feature = "sync")]
229    #[test]
230    fn endpoint() {
231        let server = MockServer::start();
232        let client = FakeOpenStackClient::new(server.base_url());
233        let mock = server.mock(|when, then| {
234            when.method(httpmock::Method::PUT).path(format!(
235                "/lbaas/quotas/{project_id}",
236                project_id = "project_id",
237            ));
238
239            then.status(200)
240                .header("content-type", "application/json")
241                .json_body(json!({ "quota": {} }));
242        });
243
244        let endpoint = Request::builder()
245            .project_id("project_id")
246            .quota(QuotaBuilder::default().build().unwrap())
247            .build()
248            .unwrap();
249        let _: serde_json::Value = endpoint.query(&client).unwrap();
250        mock.assert();
251    }
252
253    #[cfg(feature = "sync")]
254    #[test]
255    fn endpoint_headers() {
256        let server = MockServer::start();
257        let client = FakeOpenStackClient::new(server.base_url());
258        let mock = server.mock(|when, then| {
259            when.method(httpmock::Method::PUT)
260                .path(format!(
261                    "/lbaas/quotas/{project_id}",
262                    project_id = "project_id",
263                ))
264                .header("foo", "bar")
265                .header("not_foo", "not_bar");
266            then.status(200)
267                .header("content-type", "application/json")
268                .json_body(json!({ "quota": {} }));
269        });
270
271        let endpoint = Request::builder()
272            .project_id("project_id")
273            .quota(QuotaBuilder::default().build().unwrap())
274            .headers(
275                [(
276                    Some(HeaderName::from_static("foo")),
277                    HeaderValue::from_static("bar"),
278                )]
279                .into_iter(),
280            )
281            .header(
282                HeaderName::from_static("not_foo"),
283                HeaderValue::from_static("not_bar"),
284            )
285            .build()
286            .unwrap();
287        let _: serde_json::Value = endpoint.query(&client).unwrap();
288        mock.assert();
289    }
290}