Skip to main content

openstack_sdk_identity/v3/domain/config/
replace.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//! Creates a domain configuration.
19//!
20//! Relationship:
21//! `https://docs.openstack.org/api/openstack-identity/3/rel/domain_config`
22//!
23use derive_builder::Builder;
24use http::{HeaderMap, HeaderName, HeaderValue};
25
26use openstack_sdk_core::api::rest_endpoint_prelude::*;
27
28use serde_json::Value;
29use std::borrow::Cow;
30use std::collections::BTreeMap;
31
32#[derive(Builder, Debug, Clone)]
33#[builder(setter(strip_option))]
34pub struct Request<'a> {
35    /// A `config` object.
36    #[builder(private, setter(into, name = "_config"))]
37    pub(crate) config: BTreeMap<Cow<'a, str>, BTreeMap<Cow<'a, str>, Value>>,
38
39    /// domain_id parameter for /v3/domains/{domain_id}/config/{group}/{option}
40    /// API
41    #[builder(default, setter(into))]
42    domain_id: Cow<'a, str>,
43
44    #[builder(setter(name = "_headers"), default, private)]
45    _headers: Option<HeaderMap>,
46}
47impl<'a> Request<'a> {
48    /// Create a builder for the endpoint.
49    pub fn builder() -> RequestBuilder<'a> {
50        RequestBuilder::default()
51    }
52}
53
54impl<'a> RequestBuilder<'a> {
55    /// A `config` object.
56    pub fn config<I, K, V, K1, V1>(&mut self, iter: I) -> &mut Self
57    where
58        I: Iterator<Item = (K, V)>,
59        K: Into<Cow<'a, str>>,
60        V: Iterator<Item = (K1, V1)>,
61        K1: Into<Cow<'a, str>>,
62        V1: Into<Value>,
63    {
64        self.config
65            .get_or_insert_with(BTreeMap::new)
66            .extend(iter.map(|(k, v)| {
67                (
68                    k.into(),
69                    BTreeMap::from_iter(v.into_iter().map(|(k1, v1)| (k1.into(), v1.into()))),
70                )
71            }));
72        self
73    }
74
75    /// Add a single header to the Config.
76    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
77    where
78        K: Into<HeaderName>,
79        V: Into<HeaderValue>,
80    {
81        self._headers
82            .get_or_insert(None)
83            .get_or_insert_with(HeaderMap::new)
84            .insert(header_name.into(), header_value.into());
85        self
86    }
87
88    /// Add multiple headers.
89    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
90    where
91        I: Iterator<Item = T>,
92        T: Into<(Option<HeaderName>, HeaderValue)>,
93    {
94        self._headers
95            .get_or_insert(None)
96            .get_or_insert_with(HeaderMap::new)
97            .extend(iter.map(Into::into));
98        self
99    }
100}
101
102impl RestEndpoint for Request<'_> {
103    fn method(&self) -> http::Method {
104        http::Method::PUT
105    }
106
107    fn endpoint(&self) -> Cow<'static, str> {
108        format!(
109            "domains/{domain_id}/config",
110            domain_id = self.domain_id.as_ref(),
111        )
112        .into()
113    }
114
115    fn parameters(&self) -> QueryParams<'_> {
116        QueryParams::default()
117    }
118
119    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
120        let mut params = JsonBodyParams::default();
121
122        params.push("config", serde_json::to_value(&self.config)?);
123
124        params.into_body()
125    }
126
127    fn service_type(&self) -> ServiceType {
128        ServiceType::Identity
129    }
130
131    fn response_key(&self) -> Option<Cow<'static, str>> {
132        Some("config".into())
133    }
134
135    /// Returns headers to be set into the request
136    fn request_headers(&self) -> Option<&HeaderMap> {
137        self._headers.as_ref()
138    }
139
140    /// Returns required API version
141    fn api_version(&self) -> Option<ApiVersion> {
142        Some(ApiVersion::new(3, 0))
143    }
144}
145
146#[cfg(test)]
147mod tests {
148    use super::*;
149    use http::{HeaderName, HeaderValue};
150    use httpmock::MockServer;
151    #[cfg(feature = "sync")]
152    use openstack_sdk_core::api::Query;
153    use openstack_sdk_core::test::client::FakeOpenStackClient;
154    use openstack_sdk_core::types::ServiceType;
155    use serde_json::json;
156
157    #[test]
158    fn test_service_type() {
159        assert_eq!(
160            Request::builder()
161                .config(
162                    BTreeMap::<String, BTreeMap<String, Value>>::new()
163                        .into_iter()
164                        .map(|(k, v)| (k, v.into_iter()))
165                )
166                .build()
167                .unwrap()
168                .service_type(),
169            ServiceType::Identity
170        );
171    }
172
173    #[test]
174    fn test_response_key() {
175        assert_eq!(
176            Request::builder()
177                .config(
178                    BTreeMap::<String, BTreeMap<String, Value>>::new()
179                        .into_iter()
180                        .map(|(k, v)| (k, v.into_iter()))
181                )
182                .build()
183                .unwrap()
184                .response_key()
185                .unwrap(),
186            "config"
187        );
188    }
189
190    #[cfg(feature = "sync")]
191    #[test]
192    fn endpoint() {
193        let server = MockServer::start();
194        let client = FakeOpenStackClient::new(server.base_url());
195        let mock = server.mock(|when, then| {
196            when.method(httpmock::Method::PUT).path(format!(
197                "/domains/{domain_id}/config",
198                domain_id = "domain_id",
199            ));
200
201            then.status(200)
202                .header("content-type", "application/json")
203                .json_body(json!({ "config": {} }));
204        });
205
206        let endpoint = Request::builder()
207            .domain_id("domain_id")
208            .config(
209                BTreeMap::<String, BTreeMap<String, Value>>::new()
210                    .into_iter()
211                    .map(|(k, v)| (k, v.into_iter())),
212            )
213            .build()
214            .unwrap();
215        let _: serde_json::Value = endpoint.query(&client).unwrap();
216        mock.assert();
217    }
218
219    #[cfg(feature = "sync")]
220    #[test]
221    fn endpoint_headers() {
222        let server = MockServer::start();
223        let client = FakeOpenStackClient::new(server.base_url());
224        let mock = server.mock(|when, then| {
225            when.method(httpmock::Method::PUT)
226                .path(format!(
227                    "/domains/{domain_id}/config",
228                    domain_id = "domain_id",
229                ))
230                .header("foo", "bar")
231                .header("not_foo", "not_bar");
232            then.status(200)
233                .header("content-type", "application/json")
234                .json_body(json!({ "config": {} }));
235        });
236
237        let endpoint = Request::builder()
238            .domain_id("domain_id")
239            .config(
240                BTreeMap::<String, BTreeMap<String, Value>>::new()
241                    .into_iter()
242                    .map(|(k, v)| (k, v.into_iter())),
243            )
244            .headers(
245                [(
246                    Some(HeaderName::from_static("foo")),
247                    HeaderValue::from_static("bar"),
248                )]
249                .into_iter(),
250            )
251            .header(
252                HeaderName::from_static("not_foo"),
253                HeaderValue::from_static("not_bar"),
254            )
255            .build()
256            .unwrap();
257        let _: serde_json::Value = endpoint.query(&client).unwrap();
258        mock.assert();
259    }
260}