Skip to main content

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