Skip to main content

openstack_sdk_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 openstack_sdk_core::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<'a> RequestBuilder<'a> {
70    /// Add a single header to the Limit.
71    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
72    where
73        K: Into<HeaderName>,
74        V: Into<HeaderValue>,
75    {
76        self._headers
77            .get_or_insert(None)
78            .get_or_insert_with(HeaderMap::new)
79            .insert(header_name.into(), header_value.into());
80        self
81    }
82
83    /// Add multiple headers.
84    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
85    where
86        I: Iterator<Item = T>,
87        T: Into<(Option<HeaderName>, HeaderValue)>,
88    {
89        self._headers
90            .get_or_insert(None)
91            .get_or_insert_with(HeaderMap::new)
92            .extend(iter.map(Into::into));
93        self
94    }
95}
96
97impl RestEndpoint for Request<'_> {
98    fn method(&self) -> http::Method {
99        http::Method::PATCH
100    }
101
102    fn endpoint(&self) -> Cow<'static, str> {
103        format!("limits/{id}", id = self.id.as_ref(),).into()
104    }
105
106    fn parameters(&self) -> QueryParams<'_> {
107        QueryParams::default()
108    }
109
110    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
111        let mut params = JsonBodyParams::default();
112
113        params.push("limit", serde_json::to_value(&self.limit)?);
114
115        params.into_body()
116    }
117
118    fn service_type(&self) -> ServiceType {
119        ServiceType::Identity
120    }
121
122    fn response_key(&self) -> Option<Cow<'static, str>> {
123        Some("limit".into())
124    }
125
126    /// Returns headers to be set into the request
127    fn request_headers(&self) -> Option<&HeaderMap> {
128        self._headers.as_ref()
129    }
130
131    /// Returns required API version
132    fn api_version(&self) -> Option<ApiVersion> {
133        Some(ApiVersion::new(3, 0))
134    }
135}
136
137#[cfg(test)]
138mod tests {
139    use super::*;
140    use http::{HeaderName, HeaderValue};
141    use httpmock::MockServer;
142    #[cfg(feature = "sync")]
143    use openstack_sdk_core::api::Query;
144    use openstack_sdk_core::test::client::FakeOpenStackClient;
145    use openstack_sdk_core::types::ServiceType;
146    use serde_json::json;
147
148    #[test]
149    fn test_service_type() {
150        assert_eq!(
151            Request::builder()
152                .limit(LimitBuilder::default().build().unwrap())
153                .build()
154                .unwrap()
155                .service_type(),
156            ServiceType::Identity
157        );
158    }
159
160    #[test]
161    fn test_response_key() {
162        assert_eq!(
163            Request::builder()
164                .limit(LimitBuilder::default().build().unwrap())
165                .build()
166                .unwrap()
167                .response_key()
168                .unwrap(),
169            "limit"
170        );
171    }
172
173    #[cfg(feature = "sync")]
174    #[test]
175    fn endpoint() {
176        let server = MockServer::start();
177        let client = FakeOpenStackClient::new(server.base_url());
178        let mock = server.mock(|when, then| {
179            when.method(httpmock::Method::PATCH)
180                .path(format!("/limits/{id}", id = "id",));
181
182            then.status(200)
183                .header("content-type", "application/json")
184                .json_body(json!({ "limit": {} }));
185        });
186
187        let endpoint = Request::builder()
188            .id("id")
189            .limit(LimitBuilder::default().build().unwrap())
190            .build()
191            .unwrap();
192        let _: serde_json::Value = endpoint.query(&client).unwrap();
193        mock.assert();
194    }
195
196    #[cfg(feature = "sync")]
197    #[test]
198    fn endpoint_headers() {
199        let server = MockServer::start();
200        let client = FakeOpenStackClient::new(server.base_url());
201        let mock = server.mock(|when, then| {
202            when.method(httpmock::Method::PATCH)
203                .path(format!("/limits/{id}", id = "id",))
204                .header("foo", "bar")
205                .header("not_foo", "not_bar");
206            then.status(200)
207                .header("content-type", "application/json")
208                .json_body(json!({ "limit": {} }));
209        });
210
211        let endpoint = Request::builder()
212            .id("id")
213            .limit(LimitBuilder::default().build().unwrap())
214            .headers(
215                [(
216                    Some(HeaderName::from_static("foo")),
217                    HeaderValue::from_static("bar"),
218                )]
219                .into_iter(),
220            )
221            .header(
222                HeaderName::from_static("not_foo"),
223                HeaderValue::from_static("not_bar"),
224            )
225            .build()
226            .unwrap();
227        let _: serde_json::Value = endpoint.query(&client).unwrap();
228        mock.assert();
229    }
230}