Skip to main content

openstack_sdk_identity/v4/federation/mapping/
set.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//! TODO: describe domain_id update rules
19//!
20use derive_builder::Builder;
21use http::{HeaderMap, HeaderName, HeaderValue};
22
23use openstack_sdk_core::api::rest_endpoint_prelude::*;
24
25use serde::Deserialize;
26use serde::Serialize;
27use serde_json::Value;
28use std::borrow::Cow;
29use std::collections::BTreeMap;
30
31#[derive(Debug, Deserialize, Clone, Serialize)]
32pub enum Type {
33    #[serde(rename = "jwt")]
34    Jwt,
35    #[serde(rename = "oidc")]
36    Oidc,
37}
38
39/// OIDC/JWT attribute mapping update data.
40#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
41#[builder(setter(strip_option))]
42pub struct Mapping<'a> {
43    /// List of allowed redirect urls (only for `oidc` type).
44    #[serde(skip_serializing_if = "Option::is_none")]
45    #[builder(default, setter(into))]
46    pub(crate) allowed_redirect_uris: Option<Vec<Cow<'a, str>>>,
47
48    /// List of audiences that must be present in the token.
49    #[serde(skip_serializing_if = "Option::is_none")]
50    #[builder(default, setter(into))]
51    pub(crate) bound_audiences: Option<Vec<Cow<'a, str>>>,
52
53    /// Additional claims that must be present in the token.
54    #[serde(skip_serializing_if = "Option::is_none")]
55    #[builder(default, private, setter(into, name = "_bound_claims"))]
56    pub(crate) bound_claims: Option<BTreeMap<Cow<'a, str>, Value>>,
57
58    /// Token subject value that must be set in the token.
59    #[serde(skip_serializing_if = "Option::is_none")]
60    #[builder(default, setter(into))]
61    pub(crate) bound_subject: Option<Option<Cow<'a, str>>>,
62
63    /// `domain_id` owning the attribute mapping.
64    ///
65    /// Unset `domain_id` means the attribute mapping is shared and can be used
66    /// by different domains. This requires `domain_id_claim` to be present.
67    /// Attribute mapping can be only shared when the referred identity
68    /// provider is also shared (does not set the `domain_id` attribute).
69    #[serde(skip_serializing_if = "Option::is_none")]
70    #[builder(default, setter(into))]
71    pub(crate) domain_id: Option<Option<Cow<'a, str>>>,
72
73    #[serde(skip_serializing_if = "Option::is_none")]
74    #[builder(default, setter(into))]
75    pub(crate) domain_id_claim: Option<Option<Cow<'a, str>>>,
76
77    /// Mapping enabled property. Inactive mappings can not be used for login.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    #[builder(default, setter(into))]
80    pub(crate) enabled: Option<bool>,
81
82    /// `groups` claim name.
83    #[serde(skip_serializing_if = "Option::is_none")]
84    #[builder(default, setter(into))]
85    pub(crate) groups_claim: Option<Option<Cow<'a, str>>>,
86
87    /// ID of the federated identity provider for which this attribute mapping
88    /// can be used.
89    #[serde(skip_serializing_if = "Option::is_none")]
90    #[builder(default, setter(into))]
91    pub(crate) idp_id: Option<Option<Cow<'a, str>>>,
92
93    /// Attribute mapping name for federated logins.
94    #[serde(skip_serializing_if = "Option::is_none")]
95    #[builder(default, setter(into))]
96    pub(crate) name: Option<Option<Cow<'a, str>>>,
97
98    /// List of OIDC scopes.
99    #[serde(skip_serializing_if = "Option::is_none")]
100    #[builder(default, setter(into))]
101    pub(crate) oidc_scopes: Option<Vec<Cow<'a, str>>>,
102
103    /// Fixed project_id for the token.
104    #[serde(skip_serializing_if = "Option::is_none")]
105    #[builder(default, setter(into))]
106    pub(crate) token_project_id: Option<Option<Cow<'a, str>>>,
107
108    /// Token restrictions to be applied to the granted token.
109    #[serde(skip_serializing_if = "Option::is_none")]
110    #[builder(default, setter(into))]
111    pub(crate) token_restriction_id: Option<Option<Cow<'a, str>>>,
112
113    /// Attribute mapping type.
114    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
115    #[builder(default)]
116    pub(crate) _type: Option<Type>,
117
118    /// `user_id` claim name.
119    #[serde(skip_serializing_if = "Option::is_none")]
120    #[builder(default, setter(into))]
121    pub(crate) user_id_claim: Option<Option<Cow<'a, str>>>,
122
123    /// `user_name` claim name.
124    #[serde(skip_serializing_if = "Option::is_none")]
125    #[builder(default, setter(into))]
126    pub(crate) user_name_claim: Option<Option<Cow<'a, str>>>,
127}
128
129impl<'a> MappingBuilder<'a> {
130    /// Additional claims that must be present in the token.
131    pub fn bound_claims<I, K, V>(&mut self, iter: I) -> &mut Self
132    where
133        I: Iterator<Item = (K, V)>,
134        K: Into<Cow<'a, str>>,
135        V: Into<Value>,
136    {
137        self.bound_claims
138            .get_or_insert(None)
139            .get_or_insert_with(BTreeMap::new)
140            .extend(iter.map(|(k, v)| (k.into(), v.into())));
141        self
142    }
143}
144
145#[derive(Builder, Debug, Clone)]
146#[builder(setter(strip_option))]
147pub struct Request<'a> {
148    /// OIDC/JWT attribute mapping update data.
149    #[builder(setter(into))]
150    pub(crate) mapping: Mapping<'a>,
151
152    /// The ID of the attribute mapping.
153    #[builder(default, setter(into))]
154    id: Cow<'a, str>,
155
156    #[builder(setter(name = "_headers"), default, private)]
157    _headers: Option<HeaderMap>,
158}
159impl<'a> Request<'a> {
160    /// Create a builder for the endpoint.
161    pub fn builder() -> RequestBuilder<'a> {
162        RequestBuilder::default()
163    }
164}
165
166impl<'a> RequestBuilder<'a> {
167    /// Add a single header to the Mapping.
168    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
169    where
170        K: Into<HeaderName>,
171        V: Into<HeaderValue>,
172    {
173        self._headers
174            .get_or_insert(None)
175            .get_or_insert_with(HeaderMap::new)
176            .insert(header_name.into(), header_value.into());
177        self
178    }
179
180    /// Add multiple headers.
181    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
182    where
183        I: Iterator<Item = T>,
184        T: Into<(Option<HeaderName>, HeaderValue)>,
185    {
186        self._headers
187            .get_or_insert(None)
188            .get_or_insert_with(HeaderMap::new)
189            .extend(iter.map(Into::into));
190        self
191    }
192}
193
194impl RestEndpoint for Request<'_> {
195    fn method(&self) -> http::Method {
196        http::Method::PUT
197    }
198
199    fn endpoint(&self) -> Cow<'static, str> {
200        format!("federation/mappings/{id}", id = self.id.as_ref(),).into()
201    }
202
203    fn parameters(&self) -> QueryParams<'_> {
204        QueryParams::default()
205    }
206
207    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
208        let mut params = JsonBodyParams::default();
209
210        params.push("mapping", serde_json::to_value(&self.mapping)?);
211
212        params.into_body()
213    }
214
215    fn service_type(&self) -> ServiceType {
216        ServiceType::Identity
217    }
218
219    fn response_key(&self) -> Option<Cow<'static, str>> {
220        Some("mapping".into())
221    }
222
223    /// Returns headers to be set into the request
224    fn request_headers(&self) -> Option<&HeaderMap> {
225        self._headers.as_ref()
226    }
227
228    /// Returns required API version
229    fn api_version(&self) -> Option<ApiVersion> {
230        Some(ApiVersion::new(4, 0))
231    }
232}
233
234#[cfg(test)]
235mod tests {
236    use super::*;
237    use http::{HeaderName, HeaderValue};
238    use httpmock::MockServer;
239    #[cfg(feature = "sync")]
240    use openstack_sdk_core::api::Query;
241    use openstack_sdk_core::test::client::FakeOpenStackClient;
242    use openstack_sdk_core::types::ServiceType;
243    use serde_json::json;
244
245    #[test]
246    fn test_service_type() {
247        assert_eq!(
248            Request::builder()
249                .mapping(MappingBuilder::default().build().unwrap())
250                .build()
251                .unwrap()
252                .service_type(),
253            ServiceType::Identity
254        );
255    }
256
257    #[test]
258    fn test_response_key() {
259        assert_eq!(
260            Request::builder()
261                .mapping(MappingBuilder::default().build().unwrap())
262                .build()
263                .unwrap()
264                .response_key()
265                .unwrap(),
266            "mapping"
267        );
268    }
269
270    #[cfg(feature = "sync")]
271    #[test]
272    fn endpoint() {
273        let server = MockServer::start();
274        let client = FakeOpenStackClient::new(server.base_url());
275        let mock = server.mock(|when, then| {
276            when.method(httpmock::Method::PUT)
277                .path(format!("/federation/mappings/{id}", id = "id",));
278
279            then.status(200)
280                .header("content-type", "application/json")
281                .json_body(json!({ "mapping": {} }));
282        });
283
284        let endpoint = Request::builder()
285            .id("id")
286            .mapping(MappingBuilder::default().build().unwrap())
287            .build()
288            .unwrap();
289        let _: serde_json::Value = endpoint.query(&client).unwrap();
290        mock.assert();
291    }
292
293    #[cfg(feature = "sync")]
294    #[test]
295    fn endpoint_headers() {
296        let server = MockServer::start();
297        let client = FakeOpenStackClient::new(server.base_url());
298        let mock = server.mock(|when, then| {
299            when.method(httpmock::Method::PUT)
300                .path(format!("/federation/mappings/{id}", id = "id",))
301                .header("foo", "bar")
302                .header("not_foo", "not_bar");
303            then.status(200)
304                .header("content-type", "application/json")
305                .json_body(json!({ "mapping": {} }));
306        });
307
308        let endpoint = Request::builder()
309            .id("id")
310            .mapping(MappingBuilder::default().build().unwrap())
311            .headers(
312                [(
313                    Some(HeaderName::from_static("foo")),
314                    HeaderValue::from_static("bar"),
315                )]
316                .into_iter(),
317            )
318            .header(
319                HeaderName::from_static("not_foo"),
320                HeaderValue::from_static("not_bar"),
321            )
322            .build()
323            .unwrap();
324        let _: serde_json::Value = endpoint.query(&client).unwrap();
325        mock.assert();
326    }
327}