Skip to main content

openstack_sdk_identity/v3/user/
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 user.
19//!
20//! If the back-end driver does not support this functionality, this call might
21//! return the HTTP `Not Implemented (501)` response code.
22//!
23//! Relationship:
24//! `https://docs.openstack.org/api/openstack-identity/3/rel/user`
25//!
26use derive_builder::Builder;
27use http::{HeaderMap, HeaderName, HeaderValue};
28
29use openstack_sdk_core::api::rest_endpoint_prelude::*;
30
31use serde::Deserialize;
32use serde::Serialize;
33use serde_json::Value;
34use std::borrow::Cow;
35use std::collections::BTreeMap;
36
37#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
38#[builder(setter(strip_option))]
39pub struct Protocols<'a> {
40    #[serde()]
41    #[builder(setter(into))]
42    pub(crate) protocol_id: Cow<'a, str>,
43
44    #[serde()]
45    #[builder(setter(into))]
46    pub(crate) unique_id: Cow<'a, str>,
47}
48
49#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
50#[builder(setter(strip_option))]
51pub struct Federated<'a> {
52    #[serde()]
53    #[builder(setter(into))]
54    pub(crate) idp_id: Cow<'a, str>,
55
56    #[serde()]
57    #[builder(setter(into))]
58    pub(crate) protocols: Vec<Protocols<'a>>,
59}
60
61/// The resource options for the user. Available resource options are
62/// `ignore_change_password_upon_first_use`, `ignore_password_expiry`,
63/// `ignore_lockout_failure_attempts`, `lock_password`,
64/// `multi_factor_auth_enabled`, and `multi_factor_auth_rules`
65/// `ignore_user_inactivity`.
66#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
67#[builder(setter(strip_option))]
68pub struct Options<'a> {
69    #[serde(skip_serializing_if = "Option::is_none")]
70    #[builder(default, setter(into))]
71    pub(crate) ignore_change_password_upon_first_use: Option<bool>,
72
73    #[serde(skip_serializing_if = "Option::is_none")]
74    #[builder(default, setter(into))]
75    pub(crate) ignore_lockout_failure_attempts: Option<bool>,
76
77    #[serde(skip_serializing_if = "Option::is_none")]
78    #[builder(default, setter(into))]
79    pub(crate) ignore_password_expiry: Option<bool>,
80
81    #[serde(skip_serializing_if = "Option::is_none")]
82    #[builder(default, setter(into))]
83    pub(crate) ignore_user_inactivity: Option<bool>,
84
85    #[serde(skip_serializing_if = "Option::is_none")]
86    #[builder(default, setter(into))]
87    pub(crate) lock_password: Option<bool>,
88
89    #[serde(skip_serializing_if = "Option::is_none")]
90    #[builder(default, setter(into))]
91    pub(crate) multi_factor_auth_enabled: Option<bool>,
92
93    #[serde(skip_serializing_if = "Option::is_none")]
94    #[builder(default, private, setter(into, name = "_multi_factor_auth_rules"))]
95    pub(crate) multi_factor_auth_rules: Option<Vec<Vec<Cow<'a, str>>>>,
96}
97
98impl<'a> OptionsBuilder<'a> {
99    pub fn multi_factor_auth_rules<I1, I2, V>(&mut self, iter: I1) -> &mut Self
100    where
101        I1: Iterator<Item = I2>,
102        I2: IntoIterator<Item = V>,
103        V: Into<Cow<'a, str>>,
104    {
105        self.multi_factor_auth_rules
106            .get_or_insert(None)
107            .get_or_insert_with(Vec::new)
108            .extend(iter.map(|x| Vec::from_iter(x.into_iter().map(Into::into))));
109        self
110    }
111}
112
113/// A `user` object
114#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
115#[builder(setter(strip_option))]
116pub struct User<'a> {
117    /// The new ID of the default project for the user.
118    #[serde(skip_serializing_if = "Option::is_none")]
119    #[builder(default, setter(into))]
120    pub(crate) default_project_id: Option<Option<Cow<'a, str>>>,
121
122    /// The description of the user resource.
123    #[serde(skip_serializing_if = "Option::is_none")]
124    #[builder(default, setter(into))]
125    pub(crate) description: Option<Option<Cow<'a, str>>>,
126
127    /// The ID of the new domain for the user. The ability to change the domain
128    /// of a user is now deprecated, and will be removed in subsequent release.
129    /// It is already disabled by default in most Identity service
130    /// implementations.
131    #[serde(skip_serializing_if = "Option::is_none")]
132    #[builder(default, setter(into))]
133    pub(crate) domain_id: Option<Cow<'a, str>>,
134
135    /// Enables or disables the user. An enabled user can authenticate and
136    /// receive authorization. A disabled user cannot authenticate or receive
137    /// authorization. Additionally, all tokens that the user holds become no
138    /// longer valid. If you reenable this user, pre-existing tokens do not
139    /// become valid. To enable the user, set to `true`. To disable the user,
140    /// set to `false`. Default is `true`.
141    #[serde(skip_serializing_if = "Option::is_none")]
142    #[builder(default, setter(into))]
143    pub(crate) enabled: Option<bool>,
144
145    /// List of federated objects associated with a user. Each object in the
146    /// list contains the `idp_id` and `protocols`. `protocols` is a list of
147    /// objects, each of which contains `protocol_id` and `unique_id` of the
148    /// protocol and user respectively. For example:
149    ///
150    /// ```text
151    /// "federated": [
152    ///   {
153    ///     "idp_id": "efbab5a6acad4d108fec6c63d9609d83",
154    ///     "protocols": [
155    ///       {"protocol_id": mapped, "unique_id": "test@example.com"}
156    ///     ]
157    ///   }
158    /// ]
159    /// ```
160    #[serde(skip_serializing_if = "Option::is_none")]
161    #[builder(default, setter(into))]
162    pub(crate) federated: Option<Vec<Federated<'a>>>,
163
164    /// The new name for the user. Must be unique within the owning domain.
165    #[serde(skip_serializing_if = "Option::is_none")]
166    #[builder(default, setter(into))]
167    pub(crate) name: Option<Cow<'a, str>>,
168
169    /// The resource options for the user. Available resource options are
170    /// `ignore_change_password_upon_first_use`, `ignore_password_expiry`,
171    /// `ignore_lockout_failure_attempts`, `lock_password`,
172    /// `multi_factor_auth_enabled`, and `multi_factor_auth_rules`
173    /// `ignore_user_inactivity`.
174    #[serde(skip_serializing_if = "Option::is_none")]
175    #[builder(default, setter(into))]
176    pub(crate) options: Option<Options<'a>>,
177
178    /// The new password for the user.
179    #[serde(skip_serializing_if = "Option::is_none")]
180    #[builder(default, setter(into))]
181    pub(crate) password: Option<Option<Cow<'a, str>>>,
182
183    #[builder(setter(name = "_properties"), default, private)]
184    #[serde(flatten)]
185    _properties: BTreeMap<Cow<'a, str>, Value>,
186}
187
188impl<'a> UserBuilder<'a> {
189    pub fn properties<I, K, V>(&mut self, iter: I) -> &mut Self
190    where
191        I: Iterator<Item = (K, V)>,
192        K: Into<Cow<'a, str>>,
193        V: Into<Value>,
194    {
195        self._properties
196            .get_or_insert_with(BTreeMap::new)
197            .extend(iter.map(|(k, v)| (k.into(), v.into())));
198        self
199    }
200}
201
202#[derive(Builder, Debug, Clone)]
203#[builder(setter(strip_option))]
204pub struct Request<'a> {
205    /// A `user` object
206    #[builder(setter(into))]
207    pub(crate) user: User<'a>,
208
209    /// user_id parameter for /v3/users/{user_id} API
210    #[builder(default, setter(into))]
211    id: Cow<'a, str>,
212
213    #[builder(setter(name = "_headers"), default, private)]
214    _headers: Option<HeaderMap>,
215}
216impl<'a> Request<'a> {
217    /// Create a builder for the endpoint.
218    pub fn builder() -> RequestBuilder<'a> {
219        RequestBuilder::default()
220    }
221}
222
223impl<'a> RequestBuilder<'a> {
224    /// Add a single header to the User.
225    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
226    where
227        K: Into<HeaderName>,
228        V: Into<HeaderValue>,
229    {
230        self._headers
231            .get_or_insert(None)
232            .get_or_insert_with(HeaderMap::new)
233            .insert(header_name.into(), header_value.into());
234        self
235    }
236
237    /// Add multiple headers.
238    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
239    where
240        I: Iterator<Item = T>,
241        T: Into<(Option<HeaderName>, HeaderValue)>,
242    {
243        self._headers
244            .get_or_insert(None)
245            .get_or_insert_with(HeaderMap::new)
246            .extend(iter.map(Into::into));
247        self
248    }
249}
250
251impl RestEndpoint for Request<'_> {
252    fn method(&self) -> http::Method {
253        http::Method::PATCH
254    }
255
256    fn endpoint(&self) -> Cow<'static, str> {
257        format!("users/{id}", id = self.id.as_ref(),).into()
258    }
259
260    fn parameters(&self) -> QueryParams<'_> {
261        QueryParams::default()
262    }
263
264    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
265        let mut params = JsonBodyParams::default();
266
267        params.push("user", serde_json::to_value(&self.user)?);
268
269        params.into_body()
270    }
271
272    fn service_type(&self) -> ServiceType {
273        ServiceType::Identity
274    }
275
276    fn response_key(&self) -> Option<Cow<'static, str>> {
277        Some("user".into())
278    }
279
280    /// Returns headers to be set into the request
281    fn request_headers(&self) -> Option<&HeaderMap> {
282        self._headers.as_ref()
283    }
284
285    /// Returns required API version
286    fn api_version(&self) -> Option<ApiVersion> {
287        Some(ApiVersion::new(3, 0))
288    }
289}
290
291#[cfg(test)]
292mod tests {
293    use super::*;
294    use http::{HeaderName, HeaderValue};
295    use httpmock::MockServer;
296    #[cfg(feature = "sync")]
297    use openstack_sdk_core::api::Query;
298    use openstack_sdk_core::test::client::FakeOpenStackClient;
299    use openstack_sdk_core::types::ServiceType;
300    use serde_json::json;
301
302    #[test]
303    fn test_service_type() {
304        assert_eq!(
305            Request::builder()
306                .user(UserBuilder::default().build().unwrap())
307                .build()
308                .unwrap()
309                .service_type(),
310            ServiceType::Identity
311        );
312    }
313
314    #[test]
315    fn test_response_key() {
316        assert_eq!(
317            Request::builder()
318                .user(UserBuilder::default().build().unwrap())
319                .build()
320                .unwrap()
321                .response_key()
322                .unwrap(),
323            "user"
324        );
325    }
326
327    #[cfg(feature = "sync")]
328    #[test]
329    fn endpoint() {
330        let server = MockServer::start();
331        let client = FakeOpenStackClient::new(server.base_url());
332        let mock = server.mock(|when, then| {
333            when.method(httpmock::Method::PATCH)
334                .path(format!("/users/{id}", id = "id",));
335
336            then.status(200)
337                .header("content-type", "application/json")
338                .json_body(json!({ "user": {} }));
339        });
340
341        let endpoint = Request::builder()
342            .id("id")
343            .user(UserBuilder::default().build().unwrap())
344            .build()
345            .unwrap();
346        let _: serde_json::Value = endpoint.query(&client).unwrap();
347        mock.assert();
348    }
349
350    #[cfg(feature = "sync")]
351    #[test]
352    fn endpoint_headers() {
353        let server = MockServer::start();
354        let client = FakeOpenStackClient::new(server.base_url());
355        let mock = server.mock(|when, then| {
356            when.method(httpmock::Method::PATCH)
357                .path(format!("/users/{id}", id = "id",))
358                .header("foo", "bar")
359                .header("not_foo", "not_bar");
360            then.status(200)
361                .header("content-type", "application/json")
362                .json_body(json!({ "user": {} }));
363        });
364
365        let endpoint = Request::builder()
366            .id("id")
367            .user(UserBuilder::default().build().unwrap())
368            .headers(
369                [(
370                    Some(HeaderName::from_static("foo")),
371                    HeaderValue::from_static("bar"),
372                )]
373                .into_iter(),
374            )
375            .header(
376                HeaderName::from_static("not_foo"),
377                HeaderValue::from_static("not_bar"),
378            )
379            .build()
380            .unwrap();
381        let _: serde_json::Value = endpoint.query(&client).unwrap();
382        mock.assert();
383    }
384}