openstack_sdk/api/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 crate::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(&mut self, header_name: &'static str, header_value: &'static str) -> &mut Self
77where {
78        self._headers
79            .get_or_insert(None)
80            .get_or_insert_with(HeaderMap::new)
81            .insert(header_name, HeaderValue::from_static(header_value));
82        self
83    }
84
85    /// Add multiple headers.
86    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
87    where
88        I: Iterator<Item = T>,
89        T: Into<(Option<HeaderName>, HeaderValue)>,
90    {
91        self._headers
92            .get_or_insert(None)
93            .get_or_insert_with(HeaderMap::new)
94            .extend(iter.map(Into::into));
95        self
96    }
97}
98
99impl RestEndpoint for Request<'_> {
100    fn method(&self) -> http::Method {
101        http::Method::PUT
102    }
103
104    fn endpoint(&self) -> Cow<'static, str> {
105        format!(
106            "domains/{domain_id}/config",
107            domain_id = self.domain_id.as_ref(),
108        )
109        .into()
110    }
111
112    fn parameters(&self) -> QueryParams {
113        QueryParams::default()
114    }
115
116    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
117        let mut params = JsonBodyParams::default();
118
119        params.push("config", serde_json::to_value(&self.config)?);
120
121        params.into_body()
122    }
123
124    fn service_type(&self) -> ServiceType {
125        ServiceType::Identity
126    }
127
128    fn response_key(&self) -> Option<Cow<'static, str>> {
129        Some("config".into())
130    }
131
132    /// Returns headers to be set into the request
133    fn request_headers(&self) -> Option<&HeaderMap> {
134        self._headers.as_ref()
135    }
136
137    /// Returns required API version
138    fn api_version(&self) -> Option<ApiVersion> {
139        Some(ApiVersion::new(3, 0))
140    }
141}
142
143#[cfg(test)]
144mod tests {
145    use super::*;
146    #[cfg(feature = "sync")]
147    use crate::api::Query;
148    use crate::test::client::FakeOpenStackClient;
149    use crate::types::ServiceType;
150    use http::{HeaderName, HeaderValue};
151    use httpmock::MockServer;
152    use serde_json::json;
153
154    #[test]
155    fn test_service_type() {
156        assert_eq!(
157            Request::builder()
158                .config(
159                    BTreeMap::<String, BTreeMap<String, Value>>::new()
160                        .into_iter()
161                        .map(|(k, v)| (k, v.into_iter()))
162                )
163                .build()
164                .unwrap()
165                .service_type(),
166            ServiceType::Identity
167        );
168    }
169
170    #[test]
171    fn test_response_key() {
172        assert_eq!(
173            Request::builder()
174                .config(
175                    BTreeMap::<String, BTreeMap<String, Value>>::new()
176                        .into_iter()
177                        .map(|(k, v)| (k, v.into_iter()))
178                )
179                .build()
180                .unwrap()
181                .response_key()
182                .unwrap(),
183            "config"
184        );
185    }
186
187    #[cfg(feature = "sync")]
188    #[test]
189    fn endpoint() {
190        let server = MockServer::start();
191        let client = FakeOpenStackClient::new(server.base_url());
192        let mock = server.mock(|when, then| {
193            when.method(httpmock::Method::PUT).path(format!(
194                "/domains/{domain_id}/config",
195                domain_id = "domain_id",
196            ));
197
198            then.status(200)
199                .header("content-type", "application/json")
200                .json_body(json!({ "config": {} }));
201        });
202
203        let endpoint = Request::builder()
204            .domain_id("domain_id")
205            .config(
206                BTreeMap::<String, BTreeMap<String, Value>>::new()
207                    .into_iter()
208                    .map(|(k, v)| (k, v.into_iter())),
209            )
210            .build()
211            .unwrap();
212        let _: serde_json::Value = endpoint.query(&client).unwrap();
213        mock.assert();
214    }
215
216    #[cfg(feature = "sync")]
217    #[test]
218    fn endpoint_headers() {
219        let server = MockServer::start();
220        let client = FakeOpenStackClient::new(server.base_url());
221        let mock = server.mock(|when, then| {
222            when.method(httpmock::Method::PUT)
223                .path(format!(
224                    "/domains/{domain_id}/config",
225                    domain_id = "domain_id",
226                ))
227                .header("foo", "bar")
228                .header("not_foo", "not_bar");
229            then.status(200)
230                .header("content-type", "application/json")
231                .json_body(json!({ "config": {} }));
232        });
233
234        let endpoint = Request::builder()
235            .domain_id("domain_id")
236            .config(
237                BTreeMap::<String, BTreeMap<String, Value>>::new()
238                    .into_iter()
239                    .map(|(k, v)| (k, v.into_iter())),
240            )
241            .headers(
242                [(
243                    Some(HeaderName::from_static("foo")),
244                    HeaderValue::from_static("bar"),
245                )]
246                .into_iter(),
247            )
248            .header("not_foo", "not_bar")
249            .build()
250            .unwrap();
251        let _: serde_json::Value = endpoint.query(&client).unwrap();
252        mock.assert();
253    }
254}