Skip to main content

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