openstack_sdk/api/identity/v3/limit/
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 the specified limit. It only supports to update `resource_limit` or
19//! `description` for the limit.
20//!
21//! Relationship:
22//! `https://docs.openstack.org/api/openstack-identity/3/rel/limit`
23//!
24use derive_builder::Builder;
25use http::{HeaderMap, HeaderName, HeaderValue};
26
27use crate::api::rest_endpoint_prelude::*;
28
29use serde::Deserialize;
30use serde::Serialize;
31use std::borrow::Cow;
32
33/// A `limit` object
34#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
35#[builder(setter(strip_option))]
36pub struct Limit<'a> {
37    /// The limit description.
38    #[serde(skip_serializing_if = "Option::is_none")]
39    #[builder(default, setter(into))]
40    pub(crate) description: Option<Option<Cow<'a, str>>>,
41
42    /// The override limit.
43    #[serde(skip_serializing_if = "Option::is_none")]
44    #[builder(default, setter(into))]
45    pub(crate) resource_limit: Option<i32>,
46}
47
48#[derive(Builder, Debug, Clone)]
49#[builder(setter(strip_option))]
50pub struct Request<'a> {
51    /// A `limit` object
52    #[builder(setter(into))]
53    pub(crate) limit: Limit<'a>,
54
55    /// limit_id parameter for /v3/limits/{limit_id} API
56    #[builder(default, setter(into))]
57    id: Cow<'a, str>,
58
59    #[builder(setter(name = "_headers"), default, private)]
60    _headers: Option<HeaderMap>,
61}
62impl<'a> Request<'a> {
63    /// Create a builder for the endpoint.
64    pub fn builder() -> RequestBuilder<'a> {
65        RequestBuilder::default()
66    }
67}
68
69impl RequestBuilder<'_> {
70    /// Add a single header to the Limit.
71    pub fn header(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
72where {
73        self._headers
74            .get_or_insert(None)
75            .get_or_insert_with(HeaderMap::new)
76            .insert(header_name, HeaderValue::from_static(header_value));
77        self
78    }
79
80    /// Add multiple headers.
81    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
82    where
83        I: Iterator<Item = T>,
84        T: Into<(Option<HeaderName>, HeaderValue)>,
85    {
86        self._headers
87            .get_or_insert(None)
88            .get_or_insert_with(HeaderMap::new)
89            .extend(iter.map(Into::into));
90        self
91    }
92}
93
94impl RestEndpoint for Request<'_> {
95    fn method(&self) -> http::Method {
96        http::Method::PATCH
97    }
98
99    fn endpoint(&self) -> Cow<'static, str> {
100        format!("limits/{id}", id = self.id.as_ref(),).into()
101    }
102
103    fn parameters(&self) -> QueryParams {
104        QueryParams::default()
105    }
106
107    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
108        let mut params = JsonBodyParams::default();
109
110        params.push("limit", serde_json::to_value(&self.limit)?);
111
112        params.into_body()
113    }
114
115    fn service_type(&self) -> ServiceType {
116        ServiceType::Identity
117    }
118
119    fn response_key(&self) -> Option<Cow<'static, str>> {
120        Some("limit".into())
121    }
122
123    /// Returns headers to be set into the request
124    fn request_headers(&self) -> Option<&HeaderMap> {
125        self._headers.as_ref()
126    }
127
128    /// Returns required API version
129    fn api_version(&self) -> Option<ApiVersion> {
130        Some(ApiVersion::new(3, 0))
131    }
132}
133
134#[cfg(test)]
135mod tests {
136    use super::*;
137    #[cfg(feature = "sync")]
138    use crate::api::Query;
139    use crate::test::client::FakeOpenStackClient;
140    use crate::types::ServiceType;
141    use http::{HeaderName, HeaderValue};
142    use httpmock::MockServer;
143    use serde_json::json;
144
145    #[test]
146    fn test_service_type() {
147        assert_eq!(
148            Request::builder()
149                .limit(LimitBuilder::default().build().unwrap())
150                .build()
151                .unwrap()
152                .service_type(),
153            ServiceType::Identity
154        );
155    }
156
157    #[test]
158    fn test_response_key() {
159        assert_eq!(
160            Request::builder()
161                .limit(LimitBuilder::default().build().unwrap())
162                .build()
163                .unwrap()
164                .response_key()
165                .unwrap(),
166            "limit"
167        );
168    }
169
170    #[cfg(feature = "sync")]
171    #[test]
172    fn endpoint() {
173        let server = MockServer::start();
174        let client = FakeOpenStackClient::new(server.base_url());
175        let mock = server.mock(|when, then| {
176            when.method(httpmock::Method::PATCH)
177                .path(format!("/limits/{id}", id = "id",));
178
179            then.status(200)
180                .header("content-type", "application/json")
181                .json_body(json!({ "limit": {} }));
182        });
183
184        let endpoint = Request::builder()
185            .id("id")
186            .limit(LimitBuilder::default().build().unwrap())
187            .build()
188            .unwrap();
189        let _: serde_json::Value = endpoint.query(&client).unwrap();
190        mock.assert();
191    }
192
193    #[cfg(feature = "sync")]
194    #[test]
195    fn endpoint_headers() {
196        let server = MockServer::start();
197        let client = FakeOpenStackClient::new(server.base_url());
198        let mock = server.mock(|when, then| {
199            when.method(httpmock::Method::PATCH)
200                .path(format!("/limits/{id}", id = "id",))
201                .header("foo", "bar")
202                .header("not_foo", "not_bar");
203            then.status(200)
204                .header("content-type", "application/json")
205                .json_body(json!({ "limit": {} }));
206        });
207
208        let endpoint = Request::builder()
209            .id("id")
210            .limit(LimitBuilder::default().build().unwrap())
211            .headers(
212                [(
213                    Some(HeaderName::from_static("foo")),
214                    HeaderValue::from_static("bar"),
215                )]
216                .into_iter(),
217            )
218            .header("not_foo", "not_bar")
219            .build()
220            .unwrap();
221        let _: serde_json::Value = endpoint.query(&client).unwrap();
222        mock.assert();
223    }
224}