Skip to main content

openstack_sdk_identity/v3/domain/
create.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.
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 serde::Deserialize;
29use serde::Serialize;
30use std::borrow::Cow;
31
32/// The resource options for the domain. Available resource options are
33/// `immutable`.
34#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
35#[builder(setter(strip_option))]
36pub struct Options {
37    #[serde(skip_serializing_if = "Option::is_none")]
38    #[builder(default, setter(into))]
39    pub(crate) immutable: Option<bool>,
40}
41
42/// A `domain` object
43#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
44#[builder(setter(strip_option))]
45pub struct Domain<'a> {
46    /// The description of the domain.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    #[builder(default, setter(into))]
49    pub(crate) description: Option<Option<Cow<'a, str>>>,
50
51    /// If set to `true`, domain is created enabled. If set to `false`, domain
52    /// is created disabled. The default is `true`.
53    ///
54    /// Users can only authorize against an enabled domain (and any of its
55    /// projects). In addition, users can only authenticate if the domain that
56    /// owns them is also enabled. Disabling a domain prevents both of these
57    /// things.
58    #[serde(skip_serializing_if = "Option::is_none")]
59    #[builder(default, setter(into))]
60    pub(crate) enabled: Option<bool>,
61
62    /// The ID of the domain. A domain created this way will not use an
63    /// auto-generated ID, but will use the ID passed in instead. Identifiers
64    /// passed in this way must conform to the existing ID generation scheme:
65    /// UUID4 without dashes.
66    #[serde(skip_serializing_if = "Option::is_none")]
67    #[builder(default, setter(into))]
68    pub(crate) explicit_domain_id: Option<Cow<'a, str>>,
69
70    /// The name of the domain.
71    #[serde()]
72    #[builder(setter(into))]
73    pub(crate) name: Cow<'a, str>,
74
75    /// The resource options for the domain. Available resource options are
76    /// `immutable`.
77    #[serde(skip_serializing_if = "Option::is_none")]
78    #[builder(default, setter(into))]
79    pub(crate) options: Option<Options>,
80
81    #[serde(skip_serializing_if = "Option::is_none")]
82    #[builder(default, setter(into))]
83    pub(crate) tags: Option<Vec<Cow<'a, str>>>,
84}
85
86#[derive(Builder, Debug, Clone)]
87#[builder(setter(strip_option))]
88pub struct Request<'a> {
89    /// A `domain` object
90    #[builder(setter(into))]
91    pub(crate) domain: Domain<'a>,
92
93    #[builder(setter(name = "_headers"), default, private)]
94    _headers: Option<HeaderMap>,
95}
96impl<'a> Request<'a> {
97    /// Create a builder for the endpoint.
98    pub fn builder() -> RequestBuilder<'a> {
99        RequestBuilder::default()
100    }
101}
102
103impl<'a> RequestBuilder<'a> {
104    /// Add a single header to the Domain.
105    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
106    where
107        K: Into<HeaderName>,
108        V: Into<HeaderValue>,
109    {
110        self._headers
111            .get_or_insert(None)
112            .get_or_insert_with(HeaderMap::new)
113            .insert(header_name.into(), header_value.into());
114        self
115    }
116
117    /// Add multiple headers.
118    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
119    where
120        I: Iterator<Item = T>,
121        T: Into<(Option<HeaderName>, HeaderValue)>,
122    {
123        self._headers
124            .get_or_insert(None)
125            .get_or_insert_with(HeaderMap::new)
126            .extend(iter.map(Into::into));
127        self
128    }
129}
130
131impl RestEndpoint for Request<'_> {
132    fn method(&self) -> http::Method {
133        http::Method::POST
134    }
135
136    fn endpoint(&self) -> Cow<'static, str> {
137        "domains".to_string().into()
138    }
139
140    fn parameters(&self) -> QueryParams<'_> {
141        QueryParams::default()
142    }
143
144    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
145        let mut params = JsonBodyParams::default();
146
147        params.push("domain", serde_json::to_value(&self.domain)?);
148
149        params.into_body()
150    }
151
152    fn service_type(&self) -> ServiceType {
153        ServiceType::Identity
154    }
155
156    fn response_key(&self) -> Option<Cow<'static, str>> {
157        Some("domain".into())
158    }
159
160    /// Returns headers to be set into the request
161    fn request_headers(&self) -> Option<&HeaderMap> {
162        self._headers.as_ref()
163    }
164
165    /// Returns required API version
166    fn api_version(&self) -> Option<ApiVersion> {
167        Some(ApiVersion::new(3, 0))
168    }
169}
170
171#[cfg(test)]
172mod tests {
173    use super::*;
174    use http::{HeaderName, HeaderValue};
175    use httpmock::MockServer;
176    #[cfg(feature = "sync")]
177    use openstack_sdk_core::api::Query;
178    use openstack_sdk_core::test::client::FakeOpenStackClient;
179    use openstack_sdk_core::types::ServiceType;
180    use serde_json::json;
181
182    #[test]
183    fn test_service_type() {
184        assert_eq!(
185            Request::builder()
186                .domain(DomainBuilder::default().name("foo").build().unwrap())
187                .build()
188                .unwrap()
189                .service_type(),
190            ServiceType::Identity
191        );
192    }
193
194    #[test]
195    fn test_response_key() {
196        assert_eq!(
197            Request::builder()
198                .domain(DomainBuilder::default().name("foo").build().unwrap())
199                .build()
200                .unwrap()
201                .response_key()
202                .unwrap(),
203            "domain"
204        );
205    }
206
207    #[cfg(feature = "sync")]
208    #[test]
209    fn endpoint() {
210        let server = MockServer::start();
211        let client = FakeOpenStackClient::new(server.base_url());
212        let mock = server.mock(|when, then| {
213            when.method(httpmock::Method::POST)
214                .path("/domains".to_string());
215
216            then.status(200)
217                .header("content-type", "application/json")
218                .json_body(json!({ "domain": {} }));
219        });
220
221        let endpoint = Request::builder()
222            .domain(DomainBuilder::default().name("foo").build().unwrap())
223            .build()
224            .unwrap();
225        let _: serde_json::Value = endpoint.query(&client).unwrap();
226        mock.assert();
227    }
228
229    #[cfg(feature = "sync")]
230    #[test]
231    fn endpoint_headers() {
232        let server = MockServer::start();
233        let client = FakeOpenStackClient::new(server.base_url());
234        let mock = server.mock(|when, then| {
235            when.method(httpmock::Method::POST)
236                .path("/domains".to_string())
237                .header("foo", "bar")
238                .header("not_foo", "not_bar");
239            then.status(200)
240                .header("content-type", "application/json")
241                .json_body(json!({ "domain": {} }));
242        });
243
244        let endpoint = Request::builder()
245            .domain(DomainBuilder::default().name("foo").build().unwrap())
246            .headers(
247                [(
248                    Some(HeaderName::from_static("foo")),
249                    HeaderValue::from_static("bar"),
250                )]
251                .into_iter(),
252            )
253            .header(
254                HeaderName::from_static("not_foo"),
255                HeaderValue::from_static("not_bar"),
256            )
257            .build()
258            .unwrap();
259        let _: serde_json::Value = endpoint.query(&client).unwrap();
260        mock.assert();
261    }
262}