openstack_sdk/api/identity/v3/os_trust/trust/
create.rs1use derive_builder::Builder;
25use http::{HeaderMap, HeaderName, HeaderValue};
26
27use crate::api::rest_endpoint_prelude::*;
28
29use serde::Deserialize;
30use serde::Serialize;
31use serde_json::Value;
32use std::borrow::Cow;
33use std::collections::BTreeMap;
34
35#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
36#[builder(setter(strip_option))]
37pub struct Roles<'a> {
38 #[serde(skip_serializing_if = "Option::is_none")]
39 #[builder(default, setter(into))]
40 pub(crate) id: Option<Cow<'a, str>>,
41
42 #[serde(skip_serializing_if = "Option::is_none")]
44 #[builder(default, setter(into))]
45 pub(crate) name: Option<Cow<'a, str>>,
46}
47
48#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
49#[builder(setter(strip_option))]
50pub struct Trust<'a> {
51 #[serde(skip_serializing_if = "Option::is_none")]
55 #[builder(default, setter(into))]
56 pub(crate) allow_redelegation: Option<Option<bool>>,
57
58 #[serde(skip_serializing_if = "Option::is_none")]
65 #[builder(default, setter(into))]
66 pub(crate) expires_at: Option<Option<Cow<'a, str>>>,
67
68 #[serde()]
74 #[builder(setter(into))]
75 pub(crate) impersonation: bool,
76
77 #[serde(skip_serializing_if = "Option::is_none")]
80 #[builder(default, setter(into))]
81 pub(crate) project_id: Option<Option<Cow<'a, str>>>,
82
83 #[serde(skip_serializing_if = "Option::is_none")]
86 #[builder(default, setter(into))]
87 pub(crate) redelegated_trust_id: Option<Option<Cow<'a, str>>>,
88
89 #[serde(skip_serializing_if = "Option::is_none")]
108 #[builder(default, setter(into))]
109 pub(crate) redelegation_count: Option<Option<u32>>,
110
111 #[serde(skip_serializing_if = "Option::is_none")]
118 #[builder(default, setter(into))]
119 pub(crate) remaining_uses: Option<Option<i32>>,
120
121 #[serde(skip_serializing_if = "Option::is_none")]
122 #[builder(default, setter(into))]
123 pub(crate) roles: Option<Vec<Roles<'a>>>,
124
125 #[serde()]
127 #[builder(setter(into))]
128 pub(crate) trustee_user_id: Cow<'a, str>,
129
130 #[serde()]
133 #[builder(setter(into))]
134 pub(crate) trustor_user_id: Cow<'a, str>,
135
136 #[builder(setter(name = "_properties"), default, private)]
137 #[serde(flatten)]
138 _properties: BTreeMap<Cow<'a, str>, Value>,
139}
140
141impl<'a> TrustBuilder<'a> {
142 pub fn properties<I, K, V>(&mut self, iter: I) -> &mut Self
143 where
144 I: Iterator<Item = (K, V)>,
145 K: Into<Cow<'a, str>>,
146 V: Into<Value>,
147 {
148 self._properties
149 .get_or_insert_with(BTreeMap::new)
150 .extend(iter.map(|(k, v)| (k.into(), v.into())));
151 self
152 }
153}
154
155#[derive(Builder, Debug, Clone)]
156#[builder(setter(strip_option))]
157pub struct Request<'a> {
158 #[builder(setter(into))]
159 pub(crate) trust: Trust<'a>,
160
161 #[builder(setter(name = "_headers"), default, private)]
162 _headers: Option<HeaderMap>,
163}
164impl<'a> Request<'a> {
165 pub fn builder() -> RequestBuilder<'a> {
167 RequestBuilder::default()
168 }
169}
170
171impl<'a> RequestBuilder<'a> {
172 pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
174 where
175 K: Into<HeaderName>,
176 V: Into<HeaderValue>,
177 {
178 self._headers
179 .get_or_insert(None)
180 .get_or_insert_with(HeaderMap::new)
181 .insert(header_name.into(), header_value.into());
182 self
183 }
184
185 pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
187 where
188 I: Iterator<Item = T>,
189 T: Into<(Option<HeaderName>, HeaderValue)>,
190 {
191 self._headers
192 .get_or_insert(None)
193 .get_or_insert_with(HeaderMap::new)
194 .extend(iter.map(Into::into));
195 self
196 }
197}
198
199impl RestEndpoint for Request<'_> {
200 fn method(&self) -> http::Method {
201 http::Method::POST
202 }
203
204 fn endpoint(&self) -> Cow<'static, str> {
205 "OS-TRUST/trusts".to_string().into()
206 }
207
208 fn parameters(&self) -> QueryParams<'_> {
209 QueryParams::default()
210 }
211
212 fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
213 let mut params = JsonBodyParams::default();
214
215 params.push("trust", serde_json::to_value(&self.trust)?);
216
217 params.into_body()
218 }
219
220 fn service_type(&self) -> ServiceType {
221 ServiceType::Identity
222 }
223
224 fn response_key(&self) -> Option<Cow<'static, str>> {
225 Some("trust".into())
226 }
227
228 fn request_headers(&self) -> Option<&HeaderMap> {
230 self._headers.as_ref()
231 }
232
233 fn api_version(&self) -> Option<ApiVersion> {
235 Some(ApiVersion::new(3, 0))
236 }
237}
238
239#[cfg(test)]
240mod tests {
241 use super::*;
242 #[cfg(feature = "sync")]
243 use crate::api::Query;
244 use crate::test::client::FakeOpenStackClient;
245 use crate::types::ServiceType;
246 use http::{HeaderName, HeaderValue};
247 use httpmock::MockServer;
248 use serde_json::json;
249
250 #[test]
251 fn test_service_type() {
252 assert_eq!(
253 Request::builder()
254 .trust(
255 TrustBuilder::default()
256 .impersonation(false)
257 .trustee_user_id("foo")
258 .trustor_user_id("foo")
259 .build()
260 .unwrap()
261 )
262 .build()
263 .unwrap()
264 .service_type(),
265 ServiceType::Identity
266 );
267 }
268
269 #[test]
270 fn test_response_key() {
271 assert_eq!(
272 Request::builder()
273 .trust(
274 TrustBuilder::default()
275 .impersonation(false)
276 .trustee_user_id("foo")
277 .trustor_user_id("foo")
278 .build()
279 .unwrap()
280 )
281 .build()
282 .unwrap()
283 .response_key()
284 .unwrap(),
285 "trust"
286 );
287 }
288
289 #[cfg(feature = "sync")]
290 #[test]
291 fn endpoint() {
292 let server = MockServer::start();
293 let client = FakeOpenStackClient::new(server.base_url());
294 let mock = server.mock(|when, then| {
295 when.method(httpmock::Method::POST)
296 .path("/OS-TRUST/trusts".to_string());
297
298 then.status(200)
299 .header("content-type", "application/json")
300 .json_body(json!({ "trust": {} }));
301 });
302
303 let endpoint = Request::builder()
304 .trust(
305 TrustBuilder::default()
306 .impersonation(false)
307 .trustee_user_id("foo")
308 .trustor_user_id("foo")
309 .build()
310 .unwrap(),
311 )
312 .build()
313 .unwrap();
314 let _: serde_json::Value = endpoint.query(&client).unwrap();
315 mock.assert();
316 }
317
318 #[cfg(feature = "sync")]
319 #[test]
320 fn endpoint_headers() {
321 let server = MockServer::start();
322 let client = FakeOpenStackClient::new(server.base_url());
323 let mock = server.mock(|when, then| {
324 when.method(httpmock::Method::POST)
325 .path("/OS-TRUST/trusts".to_string())
326 .header("foo", "bar")
327 .header("not_foo", "not_bar");
328 then.status(200)
329 .header("content-type", "application/json")
330 .json_body(json!({ "trust": {} }));
331 });
332
333 let endpoint = Request::builder()
334 .trust(
335 TrustBuilder::default()
336 .impersonation(false)
337 .trustee_user_id("foo")
338 .trustor_user_id("foo")
339 .build()
340 .unwrap(),
341 )
342 .headers(
343 [(
344 Some(HeaderName::from_static("foo")),
345 HeaderValue::from_static("bar"),
346 )]
347 .into_iter(),
348 )
349 .header(
350 HeaderName::from_static("not_foo"),
351 HeaderValue::from_static("not_bar"),
352 )
353 .build()
354 .unwrap();
355 let _: serde_json::Value = endpoint.query(&client).unwrap();
356 mock.assert();
357 }
358}