openstack_sdk_identity/v3/project/
create.rs1use 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#[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#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
44#[builder(setter(strip_option))]
45pub struct Project<'a> {
46 #[serde(skip_serializing_if = "Option::is_none")]
48 #[builder(default, setter(into))]
49 pub(crate) description: Option<Option<Cow<'a, str>>>,
50
51 #[serde(skip_serializing_if = "Option::is_none")]
64 #[builder(default, setter(into))]
65 pub(crate) domain_id: Option<Option<Cow<'a, str>>>,
66
67 #[serde(skip_serializing_if = "Option::is_none")]
70 #[builder(default, setter(into))]
71 pub(crate) enabled: Option<bool>,
72
73 #[serde(skip_serializing_if = "Option::is_none")]
76 #[builder(default, setter(into))]
77 pub(crate) is_domain: Option<bool>,
78
79 #[serde()]
82 #[builder(setter(into))]
83 pub(crate) name: Cow<'a, str>,
84
85 #[serde(skip_serializing_if = "Option::is_none")]
88 #[builder(default, setter(into))]
89 pub(crate) options: Option<Options>,
90
91 #[serde(skip_serializing_if = "Option::is_none")]
106 #[builder(default, setter(into))]
107 pub(crate) parent_id: Option<Option<Cow<'a, str>>>,
108
109 #[serde(skip_serializing_if = "Option::is_none")]
112 #[builder(default, setter(into))]
113 pub(crate) tags: Option<Vec<Cow<'a, str>>>,
114}
115
116#[derive(Builder, Debug, Clone)]
117#[builder(setter(strip_option))]
118pub struct Request<'a> {
119 #[builder(setter(into))]
121 pub(crate) project: Project<'a>,
122
123 #[builder(setter(name = "_headers"), default, private)]
124 _headers: Option<HeaderMap>,
125}
126impl<'a> Request<'a> {
127 pub fn builder() -> RequestBuilder<'a> {
129 RequestBuilder::default()
130 }
131}
132
133impl<'a> RequestBuilder<'a> {
134 pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
136 where
137 K: Into<HeaderName>,
138 V: Into<HeaderValue>,
139 {
140 self._headers
141 .get_or_insert(None)
142 .get_or_insert_with(HeaderMap::new)
143 .insert(header_name.into(), header_value.into());
144 self
145 }
146
147 pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
149 where
150 I: Iterator<Item = T>,
151 T: Into<(Option<HeaderName>, HeaderValue)>,
152 {
153 self._headers
154 .get_or_insert(None)
155 .get_or_insert_with(HeaderMap::new)
156 .extend(iter.map(Into::into));
157 self
158 }
159}
160
161impl RestEndpoint for Request<'_> {
162 fn method(&self) -> http::Method {
163 http::Method::POST
164 }
165
166 fn endpoint(&self) -> Cow<'static, str> {
167 "projects".to_string().into()
168 }
169
170 fn parameters(&self) -> QueryParams<'_> {
171 QueryParams::default()
172 }
173
174 fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
175 let mut params = JsonBodyParams::default();
176
177 params.push("project", serde_json::to_value(&self.project)?);
178
179 params.into_body()
180 }
181
182 fn service_type(&self) -> ServiceType {
183 ServiceType::Identity
184 }
185
186 fn response_key(&self) -> Option<Cow<'static, str>> {
187 Some("project".into())
188 }
189
190 fn request_headers(&self) -> Option<&HeaderMap> {
192 self._headers.as_ref()
193 }
194
195 fn api_version(&self) -> Option<ApiVersion> {
197 Some(ApiVersion::new(3, 0))
198 }
199}
200
201#[cfg(test)]
202mod tests {
203 use super::*;
204 use http::{HeaderName, HeaderValue};
205 use httpmock::MockServer;
206 #[cfg(feature = "sync")]
207 use openstack_sdk_core::api::Query;
208 use openstack_sdk_core::test::client::FakeOpenStackClient;
209 use openstack_sdk_core::types::ServiceType;
210 use serde_json::json;
211
212 #[test]
213 fn test_service_type() {
214 assert_eq!(
215 Request::builder()
216 .project(ProjectBuilder::default().name("foo").build().unwrap())
217 .build()
218 .unwrap()
219 .service_type(),
220 ServiceType::Identity
221 );
222 }
223
224 #[test]
225 fn test_response_key() {
226 assert_eq!(
227 Request::builder()
228 .project(ProjectBuilder::default().name("foo").build().unwrap())
229 .build()
230 .unwrap()
231 .response_key()
232 .unwrap(),
233 "project"
234 );
235 }
236
237 #[cfg(feature = "sync")]
238 #[test]
239 fn endpoint() {
240 let server = MockServer::start();
241 let client = FakeOpenStackClient::new(server.base_url());
242 let mock = server.mock(|when, then| {
243 when.method(httpmock::Method::POST)
244 .path("/projects".to_string());
245
246 then.status(200)
247 .header("content-type", "application/json")
248 .json_body(json!({ "project": {} }));
249 });
250
251 let endpoint = Request::builder()
252 .project(ProjectBuilder::default().name("foo").build().unwrap())
253 .build()
254 .unwrap();
255 let _: serde_json::Value = endpoint.query(&client).unwrap();
256 mock.assert();
257 }
258
259 #[cfg(feature = "sync")]
260 #[test]
261 fn endpoint_headers() {
262 let server = MockServer::start();
263 let client = FakeOpenStackClient::new(server.base_url());
264 let mock = server.mock(|when, then| {
265 when.method(httpmock::Method::POST)
266 .path("/projects".to_string())
267 .header("foo", "bar")
268 .header("not_foo", "not_bar");
269 then.status(200)
270 .header("content-type", "application/json")
271 .json_body(json!({ "project": {} }));
272 });
273
274 let endpoint = Request::builder()
275 .project(ProjectBuilder::default().name("foo").build().unwrap())
276 .headers(
277 [(
278 Some(HeaderName::from_static("foo")),
279 HeaderValue::from_static("bar"),
280 )]
281 .into_iter(),
282 )
283 .header(
284 HeaderName::from_static("not_foo"),
285 HeaderValue::from_static("not_bar"),
286 )
287 .build()
288 .unwrap();
289 let _: serde_json::Value = endpoint.query(&client).unwrap();
290 mock.assert();
291 }
292}