Skip to main content

openstack_sdk_identity/v3/group/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 the users that belong to a group.
19//!
20//! Relationship:
21//! `https://docs.openstack.org/api/openstack-identity/3/rel/group_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
30#[derive(Builder, Debug, Clone)]
31#[builder(setter(strip_option))]
32pub struct Request<'a> {
33    /// group_id parameter for /v3/groups/{group_id}/users/{user_id} API
34    #[builder(default, setter(into))]
35    group_id: Cow<'a, str>,
36
37    /// Filter results based on which user passwords have expired. The query
38    /// should include an operator and a timestamp with a colon (:) separating
39    /// the two, for example: `password_expires_at={operator}:{timestamp}`.
40    /// Valid operators are: `lt`, `lte`, `gt`, `gte`, `eq`, and `neq`. Valid
41    /// timestamps are of the form: YYYY-MM-DDTHH:mm:ssZ.
42    #[builder(default, setter(into))]
43    password_expires_at: Option<Cow<'a, str>>,
44
45    #[builder(setter(name = "_headers"), default, private)]
46    _headers: Option<HeaderMap>,
47}
48impl<'a> Request<'a> {
49    /// Create a builder for the endpoint.
50    pub fn builder() -> RequestBuilder<'a> {
51        RequestBuilder::default()
52    }
53}
54
55impl<'a> RequestBuilder<'a> {
56    /// Add a single header to the User.
57    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
58    where
59        K: Into<HeaderName>,
60        V: Into<HeaderValue>,
61    {
62        self._headers
63            .get_or_insert(None)
64            .get_or_insert_with(HeaderMap::new)
65            .insert(header_name.into(), header_value.into());
66        self
67    }
68
69    /// Add multiple headers.
70    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
71    where
72        I: Iterator<Item = T>,
73        T: Into<(Option<HeaderName>, HeaderValue)>,
74    {
75        self._headers
76            .get_or_insert(None)
77            .get_or_insert_with(HeaderMap::new)
78            .extend(iter.map(Into::into));
79        self
80    }
81}
82
83impl RestEndpoint for Request<'_> {
84    fn method(&self) -> http::Method {
85        http::Method::GET
86    }
87
88    fn endpoint(&self) -> Cow<'static, str> {
89        format!("groups/{group_id}/users", group_id = self.group_id.as_ref(),).into()
90    }
91
92    fn parameters(&self) -> QueryParams<'_> {
93        let mut params = QueryParams::default();
94        params.push_opt("password_expires_at", self.password_expires_at.as_ref());
95
96        params
97    }
98
99    fn service_type(&self) -> ServiceType {
100        ServiceType::Identity
101    }
102
103    fn response_key(&self) -> Option<Cow<'static, str>> {
104        Some("users".into())
105    }
106
107    /// Returns headers to be set into the request
108    fn request_headers(&self) -> Option<&HeaderMap> {
109        self._headers.as_ref()
110    }
111
112    /// Returns required API version
113    fn api_version(&self) -> Option<ApiVersion> {
114        Some(ApiVersion::new(3, 0))
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121    use http::{HeaderName, HeaderValue};
122    use httpmock::MockServer;
123    #[cfg(feature = "sync")]
124    use openstack_sdk_core::api::Query;
125    use openstack_sdk_core::test::client::FakeOpenStackClient;
126    use openstack_sdk_core::types::ServiceType;
127    use serde_json::json;
128
129    #[test]
130    fn test_service_type() {
131        assert_eq!(
132            Request::builder().build().unwrap().service_type(),
133            ServiceType::Identity
134        );
135    }
136
137    #[test]
138    fn test_response_key() {
139        assert_eq!(
140            Request::builder().build().unwrap().response_key().unwrap(),
141            "users"
142        );
143    }
144
145    #[cfg(feature = "sync")]
146    #[test]
147    fn endpoint() {
148        let server = MockServer::start();
149        let client = FakeOpenStackClient::new(server.base_url());
150        let mock = server.mock(|when, then| {
151            when.method(httpmock::Method::GET)
152                .path(format!("/groups/{group_id}/users", group_id = "group_id",));
153
154            then.status(200)
155                .header("content-type", "application/json")
156                .json_body(json!({ "users": {} }));
157        });
158
159        let endpoint = Request::builder().group_id("group_id").build().unwrap();
160        let _: serde_json::Value = endpoint.query(&client).unwrap();
161        mock.assert();
162    }
163
164    #[cfg(feature = "sync")]
165    #[test]
166    fn endpoint_headers() {
167        let server = MockServer::start();
168        let client = FakeOpenStackClient::new(server.base_url());
169        let mock = server.mock(|when, then| {
170            when.method(httpmock::Method::GET)
171                .path(format!("/groups/{group_id}/users", group_id = "group_id",))
172                .header("foo", "bar")
173                .header("not_foo", "not_bar");
174            then.status(200)
175                .header("content-type", "application/json")
176                .json_body(json!({ "users": {} }));
177        });
178
179        let endpoint = Request::builder()
180            .group_id("group_id")
181            .headers(
182                [(
183                    Some(HeaderName::from_static("foo")),
184                    HeaderValue::from_static("bar"),
185                )]
186                .into_iter(),
187            )
188            .header(
189                HeaderName::from_static("not_foo"),
190                HeaderValue::from_static("not_bar"),
191            )
192            .build()
193            .unwrap();
194        let _: serde_json::Value = endpoint.query(&client).unwrap();
195        mock.assert();
196    }
197}