openstack_sdk/api/identity/v3/auth/domain/
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//! New in version 3.3
19//!
20//! This call returns the list of domains that are available to be scoped to
21//! based on the X-Auth-Token provided in the request.
22//!
23//! The structure is the same as listing domains.
24//!
25//! Relationship:
26//! `https://docs.openstack.org/api/openstack-identity/3/rel/auth_domains`
27//!
28use derive_builder::Builder;
29use http::{HeaderMap, HeaderName, HeaderValue};
30
31use crate::api::rest_endpoint_prelude::*;
32
33#[derive(Builder, Debug, Clone)]
34#[builder(setter(strip_option))]
35pub struct Request {
36    #[builder(setter(name = "_headers"), default, private)]
37    _headers: Option<HeaderMap>,
38}
39impl Request {
40    /// Create a builder for the endpoint.
41    pub fn builder() -> RequestBuilder {
42        RequestBuilder::default()
43    }
44}
45
46impl RequestBuilder {
47    /// Add a single header to the Domain.
48    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
49    where
50        K: Into<HeaderName>,
51        V: Into<HeaderValue>,
52    {
53        self._headers
54            .get_or_insert(None)
55            .get_or_insert_with(HeaderMap::new)
56            .insert(header_name.into(), header_value.into());
57        self
58    }
59
60    /// Add multiple headers.
61    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
62    where
63        I: Iterator<Item = T>,
64        T: Into<(Option<HeaderName>, HeaderValue)>,
65    {
66        self._headers
67            .get_or_insert(None)
68            .get_or_insert_with(HeaderMap::new)
69            .extend(iter.map(Into::into));
70        self
71    }
72}
73
74impl RestEndpoint for Request {
75    fn method(&self) -> http::Method {
76        http::Method::GET
77    }
78
79    fn endpoint(&self) -> Cow<'static, str> {
80        "auth/domains".to_string().into()
81    }
82
83    fn parameters(&self) -> QueryParams<'_> {
84        QueryParams::default()
85    }
86
87    fn service_type(&self) -> ServiceType {
88        ServiceType::Identity
89    }
90
91    fn response_key(&self) -> Option<Cow<'static, str>> {
92        Some("domains".into())
93    }
94
95    /// Returns headers to be set into the request
96    fn request_headers(&self) -> Option<&HeaderMap> {
97        self._headers.as_ref()
98    }
99
100    /// Returns required API version
101    fn api_version(&self) -> Option<ApiVersion> {
102        Some(ApiVersion::new(3, 0))
103    }
104}
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109    #[cfg(feature = "sync")]
110    use crate::api::Query;
111    use crate::test::client::FakeOpenStackClient;
112    use crate::types::ServiceType;
113    use http::{HeaderName, HeaderValue};
114    use httpmock::MockServer;
115    use serde_json::json;
116
117    #[test]
118    fn test_service_type() {
119        assert_eq!(
120            Request::builder().build().unwrap().service_type(),
121            ServiceType::Identity
122        );
123    }
124
125    #[test]
126    fn test_response_key() {
127        assert_eq!(
128            Request::builder().build().unwrap().response_key().unwrap(),
129            "domains"
130        );
131    }
132
133    #[cfg(feature = "sync")]
134    #[test]
135    fn endpoint() {
136        let server = MockServer::start();
137        let client = FakeOpenStackClient::new(server.base_url());
138        let mock = server.mock(|when, then| {
139            when.method(httpmock::Method::GET)
140                .path("/auth/domains".to_string());
141
142            then.status(200)
143                .header("content-type", "application/json")
144                .json_body(json!({ "domains": {} }));
145        });
146
147        let endpoint = Request::builder().build().unwrap();
148        let _: serde_json::Value = endpoint.query(&client).unwrap();
149        mock.assert();
150    }
151
152    #[cfg(feature = "sync")]
153    #[test]
154    fn endpoint_headers() {
155        let server = MockServer::start();
156        let client = FakeOpenStackClient::new(server.base_url());
157        let mock = server.mock(|when, then| {
158            when.method(httpmock::Method::GET)
159                .path("/auth/domains".to_string())
160                .header("foo", "bar")
161                .header("not_foo", "not_bar");
162            then.status(200)
163                .header("content-type", "application/json")
164                .json_body(json!({ "domains": {} }));
165        });
166
167        let endpoint = Request::builder()
168            .headers(
169                [(
170                    Some(HeaderName::from_static("foo")),
171                    HeaderValue::from_static("bar"),
172                )]
173                .into_iter(),
174            )
175            .header(
176                HeaderName::from_static("not_foo"),
177                HeaderValue::from_static("not_bar"),
178            )
179            .build()
180            .unwrap();
181        let _: serde_json::Value = endpoint.query(&client).unwrap();
182        mock.assert();
183    }
184}