Skip to main content

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