Skip to main content

openstack_sdk_identity/v3/user/
list.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//! Lists users.
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 std::borrow::Cow;
29
30use openstack_sdk_core::api::Pageable;
31#[derive(Builder, Debug, Clone)]
32#[builder(setter(strip_option))]
33pub struct Request<'a> {
34    /// The ID of the domain.
35    #[builder(default, setter(into))]
36    domain_id: Option<Cow<'a, str>>,
37
38    /// Whether the identity provider is enabled or not
39    #[builder(default)]
40    enabled: Option<bool>,
41
42    /// Filters the response by an identity provider ID.
43    #[builder(default, setter(into))]
44    idp_id: Option<Cow<'a, str>>,
45
46    #[builder(default)]
47    limit: Option<u32>,
48
49    /// ID of the last fetched entry
50    #[builder(default, setter(into))]
51    marker: Option<Cow<'a, str>>,
52
53    /// The resource name.
54    #[builder(default, setter(into))]
55    name: Option<Cow<'a, str>>,
56
57    /// Filter results based on which user passwords have expired. The query
58    /// should include an operator and a timestamp with a colon (:) separating
59    /// the two, for example: `password_expires_at={operator}:{timestamp}`
60    /// Valid operators are: lt, lte, gt, gte, eq, and neq
61    ///
62    /// - lt: expiration time lower than the timestamp
63    /// - lte: expiration time lower than or equal to the timestamp
64    /// - gt: expiration time higher than the timestamp
65    /// - gte: expiration time higher than or equal to the timestamp
66    /// - eq: expiration time equal to the timestamp
67    /// - neq: expiration time not equal to the timestamp
68    ///
69    /// Valid timestamps are of the form: `YYYY-MM-DDTHH:mm:ssZ`.For
70    /// example:`/v3/users?password_expires_at=lt:2016-12-08T22:02:00Z` The
71    /// example would return a list of users whose password expired before the
72    /// timestamp `(2016-12-08T22:02:00Z).`
73    #[builder(default, setter(into))]
74    password_expires_at: Option<Cow<'a, str>>,
75
76    /// Filters the response by a protocol ID.
77    #[builder(default, setter(into))]
78    protocol_id: Option<Cow<'a, str>>,
79
80    /// Sort direction. A valid value is asc (ascending) or desc (descending).
81    #[builder(default, setter(into))]
82    sort_dir: Option<Cow<'a, str>>,
83
84    /// Sorts resources by attribute.
85    #[builder(default, setter(into))]
86    sort_key: Option<Cow<'a, str>>,
87
88    /// Filters the response by a unique ID.
89    #[builder(default, setter(into))]
90    unique_id: Option<Cow<'a, str>>,
91
92    #[builder(setter(name = "_headers"), default, private)]
93    _headers: Option<HeaderMap>,
94}
95impl<'a> Request<'a> {
96    /// Create a builder for the endpoint.
97    pub fn builder() -> RequestBuilder<'a> {
98        RequestBuilder::default()
99    }
100}
101
102impl<'a> RequestBuilder<'a> {
103    /// Add a single header to the User.
104    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
105    where
106        K: Into<HeaderName>,
107        V: Into<HeaderValue>,
108    {
109        self._headers
110            .get_or_insert(None)
111            .get_or_insert_with(HeaderMap::new)
112            .insert(header_name.into(), header_value.into());
113        self
114    }
115
116    /// Add multiple headers.
117    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
118    where
119        I: Iterator<Item = T>,
120        T: Into<(Option<HeaderName>, HeaderValue)>,
121    {
122        self._headers
123            .get_or_insert(None)
124            .get_or_insert_with(HeaderMap::new)
125            .extend(iter.map(Into::into));
126        self
127    }
128}
129
130impl RestEndpoint for Request<'_> {
131    fn method(&self) -> http::Method {
132        http::Method::GET
133    }
134
135    fn endpoint(&self) -> Cow<'static, str> {
136        "users".to_string().into()
137    }
138
139    fn parameters(&self) -> QueryParams<'_> {
140        let mut params = QueryParams::default();
141        params.push_opt("domain_id", self.domain_id.as_ref());
142        params.push_opt("enabled", self.enabled);
143        params.push_opt("idp_id", self.idp_id.as_ref());
144        params.push_opt("limit", self.limit);
145        params.push_opt("marker", self.marker.as_ref());
146        params.push_opt("name", self.name.as_ref());
147        params.push_opt("password_expires_at", self.password_expires_at.as_ref());
148        params.push_opt("protocol_id", self.protocol_id.as_ref());
149        params.push_opt("sort_dir", self.sort_dir.as_ref());
150        params.push_opt("sort_key", self.sort_key.as_ref());
151        params.push_opt("unique_id", self.unique_id.as_ref());
152
153        params
154    }
155
156    fn service_type(&self) -> ServiceType {
157        ServiceType::Identity
158    }
159
160    fn response_key(&self) -> Option<Cow<'static, str>> {
161        Some("users".into())
162    }
163
164    /// Returns headers to be set into the request
165    fn request_headers(&self) -> Option<&HeaderMap> {
166        self._headers.as_ref()
167    }
168
169    /// Returns required API version
170    fn api_version(&self) -> Option<ApiVersion> {
171        Some(ApiVersion::new(3, 0))
172    }
173}
174impl Pageable for Request<'_> {}
175
176#[cfg(test)]
177mod tests {
178    use super::*;
179    use http::{HeaderName, HeaderValue};
180    use httpmock::MockServer;
181    #[cfg(feature = "sync")]
182    use openstack_sdk_core::api::Query;
183    use openstack_sdk_core::test::client::FakeOpenStackClient;
184    use openstack_sdk_core::types::ServiceType;
185    use serde_json::json;
186
187    #[test]
188    fn test_service_type() {
189        assert_eq!(
190            Request::builder().build().unwrap().service_type(),
191            ServiceType::Identity
192        );
193    }
194
195    #[test]
196    fn test_response_key() {
197        assert_eq!(
198            Request::builder().build().unwrap().response_key().unwrap(),
199            "users"
200        );
201    }
202
203    #[cfg(feature = "sync")]
204    #[test]
205    fn endpoint() {
206        let server = MockServer::start();
207        let client = FakeOpenStackClient::new(server.base_url());
208        let mock = server.mock(|when, then| {
209            when.method(httpmock::Method::GET)
210                .path("/users".to_string());
211
212            then.status(200)
213                .header("content-type", "application/json")
214                .json_body(json!({ "users": {} }));
215        });
216
217        let endpoint = Request::builder().build().unwrap();
218        let _: serde_json::Value = endpoint.query(&client).unwrap();
219        mock.assert();
220    }
221
222    #[cfg(feature = "sync")]
223    #[test]
224    fn endpoint_headers() {
225        let server = MockServer::start();
226        let client = FakeOpenStackClient::new(server.base_url());
227        let mock = server.mock(|when, then| {
228            when.method(httpmock::Method::GET)
229                .path("/users".to_string())
230                .header("foo", "bar")
231                .header("not_foo", "not_bar");
232            then.status(200)
233                .header("content-type", "application/json")
234                .json_body(json!({ "users": {} }));
235        });
236
237        let endpoint = Request::builder()
238            .headers(
239                [(
240                    Some(HeaderName::from_static("foo")),
241                    HeaderValue::from_static("bar"),
242                )]
243                .into_iter(),
244            )
245            .header(
246                HeaderName::from_static("not_foo"),
247                HeaderValue::from_static("not_bar"),
248            )
249            .build()
250            .unwrap();
251        let _: serde_json::Value = endpoint.query(&client).unwrap();
252        mock.assert();
253    }
254}