Skip to main content

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