Skip to main content

openstack_sdk_identity/v3/credential/
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 credential.
19//!
20//! The following example shows how to create an EC2-style credential. The
21//! credential blob is a string that contains a JSON-serialized dictionary with
22//! the `access` and `secret` keys. This format is required when you specify
23//! the `ec2` type. To specify other credentials, such as `access_key`, change
24//! the type and contents of the data blob.
25//!
26//! Relationship:
27//! `https://docs.openstack.org/api/openstack-identity/3/rel/credentials`
28//!
29use derive_builder::Builder;
30use http::{HeaderMap, HeaderName, HeaderValue};
31
32use openstack_sdk_core::api::rest_endpoint_prelude::*;
33
34use serde::Deserialize;
35use serde::Serialize;
36use serde_json::Value;
37use std::borrow::Cow;
38use std::collections::BTreeMap;
39
40/// A `credential` object.
41#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
42#[builder(setter(strip_option))]
43pub struct Credential<'a> {
44    /// The credential itself, as a serialized blob.
45    #[serde()]
46    #[builder(setter(into))]
47    pub(crate) blob: Cow<'a, str>,
48
49    /// The UUID for the credential.
50    #[serde(skip_serializing_if = "Option::is_none")]
51    #[builder(default, setter(into))]
52    pub(crate) id: Option<Cow<'a, str>>,
53
54    /// The ID for the project.
55    #[serde(skip_serializing_if = "Option::is_none")]
56    #[builder(default, setter(into))]
57    pub(crate) project_id: Option<Option<Cow<'a, str>>>,
58
59    /// The credential type, such as `ec2` or `cert`. The implementation
60    /// determines the list of supported types.
61    #[serde(rename = "type")]
62    #[builder(setter(into))]
63    pub(crate) _type: Cow<'a, str>,
64
65    /// The ID of the user who owns the credential.
66    #[serde()]
67    #[builder(setter(into))]
68    pub(crate) user_id: Cow<'a, str>,
69
70    #[builder(setter(name = "_properties"), default, private)]
71    #[serde(flatten)]
72    _properties: BTreeMap<Cow<'a, str>, Value>,
73}
74
75impl<'a> CredentialBuilder<'a> {
76    pub fn properties<I, K, V>(&mut self, iter: I) -> &mut Self
77    where
78        I: Iterator<Item = (K, V)>,
79        K: Into<Cow<'a, str>>,
80        V: Into<Value>,
81    {
82        self._properties
83            .get_or_insert_with(BTreeMap::new)
84            .extend(iter.map(|(k, v)| (k.into(), v.into())));
85        self
86    }
87}
88
89#[derive(Builder, Debug, Clone)]
90#[builder(setter(strip_option))]
91pub struct Request<'a> {
92    /// A `credential` object.
93    #[builder(setter(into))]
94    pub(crate) credential: Credential<'a>,
95
96    #[builder(setter(name = "_headers"), default, private)]
97    _headers: Option<HeaderMap>,
98}
99impl<'a> Request<'a> {
100    /// Create a builder for the endpoint.
101    pub fn builder() -> RequestBuilder<'a> {
102        RequestBuilder::default()
103    }
104}
105
106impl<'a> RequestBuilder<'a> {
107    /// Add a single header to the Credential.
108    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
109    where
110        K: Into<HeaderName>,
111        V: Into<HeaderValue>,
112    {
113        self._headers
114            .get_or_insert(None)
115            .get_or_insert_with(HeaderMap::new)
116            .insert(header_name.into(), header_value.into());
117        self
118    }
119
120    /// Add multiple headers.
121    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
122    where
123        I: Iterator<Item = T>,
124        T: Into<(Option<HeaderName>, HeaderValue)>,
125    {
126        self._headers
127            .get_or_insert(None)
128            .get_or_insert_with(HeaderMap::new)
129            .extend(iter.map(Into::into));
130        self
131    }
132}
133
134impl RestEndpoint for Request<'_> {
135    fn method(&self) -> http::Method {
136        http::Method::POST
137    }
138
139    fn endpoint(&self) -> Cow<'static, str> {
140        "credentials".to_string().into()
141    }
142
143    fn parameters(&self) -> QueryParams<'_> {
144        QueryParams::default()
145    }
146
147    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
148        let mut params = JsonBodyParams::default();
149
150        params.push("credential", serde_json::to_value(&self.credential)?);
151
152        params.into_body()
153    }
154
155    fn service_type(&self) -> ServiceType {
156        ServiceType::Identity
157    }
158
159    fn response_key(&self) -> Option<Cow<'static, str>> {
160        Some("credential".into())
161    }
162
163    /// Returns headers to be set into the request
164    fn request_headers(&self) -> Option<&HeaderMap> {
165        self._headers.as_ref()
166    }
167
168    /// Returns required API version
169    fn api_version(&self) -> Option<ApiVersion> {
170        Some(ApiVersion::new(3, 0))
171    }
172}
173
174#[cfg(test)]
175mod tests {
176    use super::*;
177    use http::{HeaderName, HeaderValue};
178    use httpmock::MockServer;
179    #[cfg(feature = "sync")]
180    use openstack_sdk_core::api::Query;
181    use openstack_sdk_core::test::client::FakeOpenStackClient;
182    use openstack_sdk_core::types::ServiceType;
183    use serde_json::json;
184
185    #[test]
186    fn test_service_type() {
187        assert_eq!(
188            Request::builder()
189                .credential(
190                    CredentialBuilder::default()
191                        ._type("foo")
192                        .blob("foo")
193                        .user_id("foo")
194                        .build()
195                        .unwrap()
196                )
197                .build()
198                .unwrap()
199                .service_type(),
200            ServiceType::Identity
201        );
202    }
203
204    #[test]
205    fn test_response_key() {
206        assert_eq!(
207            Request::builder()
208                .credential(
209                    CredentialBuilder::default()
210                        ._type("foo")
211                        .blob("foo")
212                        .user_id("foo")
213                        .build()
214                        .unwrap()
215                )
216                .build()
217                .unwrap()
218                .response_key()
219                .unwrap(),
220            "credential"
221        );
222    }
223
224    #[cfg(feature = "sync")]
225    #[test]
226    fn endpoint() {
227        let server = MockServer::start();
228        let client = FakeOpenStackClient::new(server.base_url());
229        let mock = server.mock(|when, then| {
230            when.method(httpmock::Method::POST)
231                .path("/credentials".to_string());
232
233            then.status(200)
234                .header("content-type", "application/json")
235                .json_body(json!({ "credential": {} }));
236        });
237
238        let endpoint = Request::builder()
239            .credential(
240                CredentialBuilder::default()
241                    ._type("foo")
242                    .blob("foo")
243                    .user_id("foo")
244                    .build()
245                    .unwrap(),
246            )
247            .build()
248            .unwrap();
249        let _: serde_json::Value = endpoint.query(&client).unwrap();
250        mock.assert();
251    }
252
253    #[cfg(feature = "sync")]
254    #[test]
255    fn endpoint_headers() {
256        let server = MockServer::start();
257        let client = FakeOpenStackClient::new(server.base_url());
258        let mock = server.mock(|when, then| {
259            when.method(httpmock::Method::POST)
260                .path("/credentials".to_string())
261                .header("foo", "bar")
262                .header("not_foo", "not_bar");
263            then.status(200)
264                .header("content-type", "application/json")
265                .json_body(json!({ "credential": {} }));
266        });
267
268        let endpoint = Request::builder()
269            .credential(
270                CredentialBuilder::default()
271                    ._type("foo")
272                    .blob("foo")
273                    .user_id("foo")
274                    .build()
275                    .unwrap(),
276            )
277            .headers(
278                [(
279                    Some(HeaderName::from_static("foo")),
280                    HeaderValue::from_static("bar"),
281                )]
282                .into_iter(),
283            )
284            .header(
285                HeaderName::from_static("not_foo"),
286                HeaderValue::from_static("not_bar"),
287            )
288            .build()
289            .unwrap();
290        let _: serde_json::Value = endpoint.query(&client).unwrap();
291        mock.assert();
292    }
293}