Skip to main content

openstack_sdk_identity/v3/user/
create.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//! Creates a user.
19//!
20//! Relationship:
21//! `https://docs.openstack.org/api/openstack-identity/3/rel/users`
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 serde_json::Value;
31use std::borrow::Cow;
32use std::collections::BTreeMap;
33
34#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
35#[builder(setter(strip_option))]
36pub struct Protocols<'a> {
37    #[serde()]
38    #[builder(setter(into))]
39    pub(crate) protocol_id: Cow<'a, str>,
40
41    #[serde()]
42    #[builder(setter(into))]
43    pub(crate) unique_id: Cow<'a, str>,
44}
45
46#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
47#[builder(setter(strip_option))]
48pub struct Federated<'a> {
49    #[serde()]
50    #[builder(setter(into))]
51    pub(crate) idp_id: Cow<'a, str>,
52
53    #[serde()]
54    #[builder(setter(into))]
55    pub(crate) protocols: Vec<Protocols<'a>>,
56}
57
58/// The resource options for the user. Available resource options are
59/// `ignore_change_password_upon_first_use`, `ignore_password_expiry`,
60/// `ignore_lockout_failure_attempts`, `lock_password`,
61/// `multi_factor_auth_enabled`, and `multi_factor_auth_rules`
62/// `ignore_user_inactivity`.
63#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
64#[builder(setter(strip_option))]
65pub struct Options<'a> {
66    #[serde(skip_serializing_if = "Option::is_none")]
67    #[builder(default, setter(into))]
68    pub(crate) ignore_change_password_upon_first_use: Option<bool>,
69
70    #[serde(skip_serializing_if = "Option::is_none")]
71    #[builder(default, setter(into))]
72    pub(crate) ignore_lockout_failure_attempts: Option<bool>,
73
74    #[serde(skip_serializing_if = "Option::is_none")]
75    #[builder(default, setter(into))]
76    pub(crate) ignore_password_expiry: Option<bool>,
77
78    #[serde(skip_serializing_if = "Option::is_none")]
79    #[builder(default, setter(into))]
80    pub(crate) ignore_user_inactivity: Option<bool>,
81
82    #[serde(skip_serializing_if = "Option::is_none")]
83    #[builder(default, setter(into))]
84    pub(crate) lock_password: Option<bool>,
85
86    #[serde(skip_serializing_if = "Option::is_none")]
87    #[builder(default, setter(into))]
88    pub(crate) multi_factor_auth_enabled: Option<bool>,
89
90    #[serde(skip_serializing_if = "Option::is_none")]
91    #[builder(default, private, setter(into, name = "_multi_factor_auth_rules"))]
92    pub(crate) multi_factor_auth_rules: Option<Vec<Vec<Cow<'a, str>>>>,
93}
94
95impl<'a> OptionsBuilder<'a> {
96    pub fn multi_factor_auth_rules<I1, I2, V>(&mut self, iter: I1) -> &mut Self
97    where
98        I1: Iterator<Item = I2>,
99        I2: IntoIterator<Item = V>,
100        V: Into<Cow<'a, str>>,
101    {
102        self.multi_factor_auth_rules
103            .get_or_insert(None)
104            .get_or_insert_with(Vec::new)
105            .extend(iter.map(|x| Vec::from_iter(x.into_iter().map(Into::into))));
106        self
107    }
108}
109
110/// A `user` object
111#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
112#[builder(setter(strip_option))]
113pub struct User<'a> {
114    /// The ID of the default project for the user. A user’s default project
115    /// must not be a domain. Setting this attribute does not grant any actual
116    /// authorization on the project, and is merely provided for convenience.
117    /// Therefore, the referenced project does not need to exist within the
118    /// user domain. (Since v3.1) If the user does not have authorization to
119    /// their default project, the default project is ignored at token
120    /// creation. (Since v3.1) Additionally, if your default project is not
121    /// valid, a token is issued without an explicit scope of authorization.
122    #[serde(skip_serializing_if = "Option::is_none")]
123    #[builder(default, setter(into))]
124    pub(crate) default_project_id: Option<Option<Cow<'a, str>>>,
125
126    /// The description of the user resource.
127    #[serde(skip_serializing_if = "Option::is_none")]
128    #[builder(default, setter(into))]
129    pub(crate) description: Option<Option<Cow<'a, str>>>,
130
131    /// The ID of the domain of the user. If the domain ID is not provided in
132    /// the request, the Identity service will attempt to pull the domain ID
133    /// from the token used in the request. Note that this requires the use of
134    /// a domain-scoped token.
135    #[serde(skip_serializing_if = "Option::is_none")]
136    #[builder(default, setter(into))]
137    pub(crate) domain_id: Option<Cow<'a, str>>,
138
139    /// If the user is enabled, this value is `true`. If the user is disabled,
140    /// this value is `false`.
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 user name. Must be unique within the owning domain.
165    #[serde()]
166    #[builder(setter(into))]
167    pub(crate) name: 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 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    #[builder(setter(name = "_headers"), default, private)]
210    _headers: Option<HeaderMap>,
211}
212impl<'a> Request<'a> {
213    /// Create a builder for the endpoint.
214    pub fn builder() -> RequestBuilder<'a> {
215        RequestBuilder::default()
216    }
217}
218
219impl<'a> RequestBuilder<'a> {
220    /// Add a single header to the User.
221    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
222    where
223        K: Into<HeaderName>,
224        V: Into<HeaderValue>,
225    {
226        self._headers
227            .get_or_insert(None)
228            .get_or_insert_with(HeaderMap::new)
229            .insert(header_name.into(), header_value.into());
230        self
231    }
232
233    /// Add multiple headers.
234    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
235    where
236        I: Iterator<Item = T>,
237        T: Into<(Option<HeaderName>, HeaderValue)>,
238    {
239        self._headers
240            .get_or_insert(None)
241            .get_or_insert_with(HeaderMap::new)
242            .extend(iter.map(Into::into));
243        self
244    }
245}
246
247impl RestEndpoint for Request<'_> {
248    fn method(&self) -> http::Method {
249        http::Method::POST
250    }
251
252    fn endpoint(&self) -> Cow<'static, str> {
253        "users".to_string().into()
254    }
255
256    fn parameters(&self) -> QueryParams<'_> {
257        QueryParams::default()
258    }
259
260    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
261        let mut params = JsonBodyParams::default();
262
263        params.push("user", serde_json::to_value(&self.user)?);
264
265        params.into_body()
266    }
267
268    fn service_type(&self) -> ServiceType {
269        ServiceType::Identity
270    }
271
272    fn response_key(&self) -> Option<Cow<'static, str>> {
273        Some("user".into())
274    }
275
276    /// Returns headers to be set into the request
277    fn request_headers(&self) -> Option<&HeaderMap> {
278        self._headers.as_ref()
279    }
280
281    /// Returns required API version
282    fn api_version(&self) -> Option<ApiVersion> {
283        Some(ApiVersion::new(3, 0))
284    }
285}
286
287#[cfg(test)]
288mod tests {
289    use super::*;
290    use http::{HeaderName, HeaderValue};
291    use httpmock::MockServer;
292    #[cfg(feature = "sync")]
293    use openstack_sdk_core::api::Query;
294    use openstack_sdk_core::test::client::FakeOpenStackClient;
295    use openstack_sdk_core::types::ServiceType;
296    use serde_json::json;
297
298    #[test]
299    fn test_service_type() {
300        assert_eq!(
301            Request::builder()
302                .user(UserBuilder::default().name("foo").build().unwrap())
303                .build()
304                .unwrap()
305                .service_type(),
306            ServiceType::Identity
307        );
308    }
309
310    #[test]
311    fn test_response_key() {
312        assert_eq!(
313            Request::builder()
314                .user(UserBuilder::default().name("foo").build().unwrap())
315                .build()
316                .unwrap()
317                .response_key()
318                .unwrap(),
319            "user"
320        );
321    }
322
323    #[cfg(feature = "sync")]
324    #[test]
325    fn endpoint() {
326        let server = MockServer::start();
327        let client = FakeOpenStackClient::new(server.base_url());
328        let mock = server.mock(|when, then| {
329            when.method(httpmock::Method::POST)
330                .path("/users".to_string());
331
332            then.status(200)
333                .header("content-type", "application/json")
334                .json_body(json!({ "user": {} }));
335        });
336
337        let endpoint = Request::builder()
338            .user(UserBuilder::default().name("foo").build().unwrap())
339            .build()
340            .unwrap();
341        let _: serde_json::Value = endpoint.query(&client).unwrap();
342        mock.assert();
343    }
344
345    #[cfg(feature = "sync")]
346    #[test]
347    fn endpoint_headers() {
348        let server = MockServer::start();
349        let client = FakeOpenStackClient::new(server.base_url());
350        let mock = server.mock(|when, then| {
351            when.method(httpmock::Method::POST)
352                .path("/users".to_string())
353                .header("foo", "bar")
354                .header("not_foo", "not_bar");
355            then.status(200)
356                .header("content-type", "application/json")
357                .json_body(json!({ "user": {} }));
358        });
359
360        let endpoint = Request::builder()
361            .user(UserBuilder::default().name("foo").build().unwrap())
362            .headers(
363                [(
364                    Some(HeaderName::from_static("foo")),
365                    HeaderValue::from_static("bar"),
366                )]
367                .into_iter(),
368            )
369            .header(
370                HeaderName::from_static("not_foo"),
371                HeaderValue::from_static("not_bar"),
372            )
373            .build()
374            .unwrap();
375        let _: serde_json::Value = endpoint.query(&client).unwrap();
376        mock.assert();
377    }
378}