Skip to main content

openstack_sdk_identity/v3/group/
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 groups.
19//!
20//! Relationship:
21//! `https://docs.openstack.org/api/openstack-identity/3/rel/groups`
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    #[builder(default)]
39    limit: Option<u32>,
40
41    /// ID of the last fetched entry
42    #[builder(default, setter(into))]
43    marker: Option<Cow<'a, str>>,
44
45    /// The resource name.
46    #[builder(default, setter(into))]
47    name: Option<Cow<'a, str>>,
48
49    /// Sort direction. A valid value is asc (ascending) or desc (descending).
50    #[builder(default, setter(into))]
51    sort_dir: Option<Cow<'a, str>>,
52
53    /// Sorts resources by attribute.
54    #[builder(default, setter(into))]
55    sort_key: Option<Cow<'a, str>>,
56
57    #[builder(setter(name = "_headers"), default, private)]
58    _headers: Option<HeaderMap>,
59}
60impl<'a> Request<'a> {
61    /// Create a builder for the endpoint.
62    pub fn builder() -> RequestBuilder<'a> {
63        RequestBuilder::default()
64    }
65}
66
67impl<'a> RequestBuilder<'a> {
68    /// Add a single header to the Group.
69    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
70    where
71        K: Into<HeaderName>,
72        V: Into<HeaderValue>,
73    {
74        self._headers
75            .get_or_insert(None)
76            .get_or_insert_with(HeaderMap::new)
77            .insert(header_name.into(), header_value.into());
78        self
79    }
80
81    /// Add multiple headers.
82    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
83    where
84        I: Iterator<Item = T>,
85        T: Into<(Option<HeaderName>, HeaderValue)>,
86    {
87        self._headers
88            .get_or_insert(None)
89            .get_or_insert_with(HeaderMap::new)
90            .extend(iter.map(Into::into));
91        self
92    }
93}
94
95impl RestEndpoint for Request<'_> {
96    fn method(&self) -> http::Method {
97        http::Method::GET
98    }
99
100    fn endpoint(&self) -> Cow<'static, str> {
101        "groups".to_string().into()
102    }
103
104    fn parameters(&self) -> QueryParams<'_> {
105        let mut params = QueryParams::default();
106        params.push_opt("domain_id", self.domain_id.as_ref());
107        params.push_opt("limit", self.limit);
108        params.push_opt("marker", self.marker.as_ref());
109        params.push_opt("name", self.name.as_ref());
110        params.push_opt("sort_dir", self.sort_dir.as_ref());
111        params.push_opt("sort_key", self.sort_key.as_ref());
112
113        params
114    }
115
116    fn service_type(&self) -> ServiceType {
117        ServiceType::Identity
118    }
119
120    fn response_key(&self) -> Option<Cow<'static, str>> {
121        Some("groups".into())
122    }
123
124    /// Returns headers to be set into the request
125    fn request_headers(&self) -> Option<&HeaderMap> {
126        self._headers.as_ref()
127    }
128
129    /// Returns required API version
130    fn api_version(&self) -> Option<ApiVersion> {
131        Some(ApiVersion::new(3, 0))
132    }
133}
134impl Pageable for Request<'_> {}
135
136#[cfg(test)]
137mod tests {
138    use super::*;
139    use http::{HeaderName, HeaderValue};
140    use httpmock::MockServer;
141    #[cfg(feature = "sync")]
142    use openstack_sdk_core::api::Query;
143    use openstack_sdk_core::test::client::FakeOpenStackClient;
144    use openstack_sdk_core::types::ServiceType;
145    use serde_json::json;
146
147    #[test]
148    fn test_service_type() {
149        assert_eq!(
150            Request::builder().build().unwrap().service_type(),
151            ServiceType::Identity
152        );
153    }
154
155    #[test]
156    fn test_response_key() {
157        assert_eq!(
158            Request::builder().build().unwrap().response_key().unwrap(),
159            "groups"
160        );
161    }
162
163    #[cfg(feature = "sync")]
164    #[test]
165    fn endpoint() {
166        let server = MockServer::start();
167        let client = FakeOpenStackClient::new(server.base_url());
168        let mock = server.mock(|when, then| {
169            when.method(httpmock::Method::GET)
170                .path("/groups".to_string());
171
172            then.status(200)
173                .header("content-type", "application/json")
174                .json_body(json!({ "groups": {} }));
175        });
176
177        let endpoint = Request::builder().build().unwrap();
178        let _: serde_json::Value = endpoint.query(&client).unwrap();
179        mock.assert();
180    }
181
182    #[cfg(feature = "sync")]
183    #[test]
184    fn endpoint_headers() {
185        let server = MockServer::start();
186        let client = FakeOpenStackClient::new(server.base_url());
187        let mock = server.mock(|when, then| {
188            when.method(httpmock::Method::GET)
189                .path("/groups".to_string())
190                .header("foo", "bar")
191                .header("not_foo", "not_bar");
192            then.status(200)
193                .header("content-type", "application/json")
194                .json_body(json!({ "groups": {} }));
195        });
196
197        let endpoint = Request::builder()
198            .headers(
199                [(
200                    Some(HeaderName::from_static("foo")),
201                    HeaderValue::from_static("bar"),
202                )]
203                .into_iter(),
204            )
205            .header(
206                HeaderName::from_static("not_foo"),
207                HeaderValue::from_static("not_bar"),
208            )
209            .build()
210            .unwrap();
211        let _: serde_json::Value = endpoint.query(&client).unwrap();
212        mock.assert();
213    }
214}