Skip to main content

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