openstack_sdk_identity/v3/user/password/
set.rs1use derive_builder::Builder;
24use http::{HeaderMap, HeaderName, HeaderValue};
25
26use openstack_sdk_core::api::rest_endpoint_prelude::*;
27
28use openstack_sdk_core::api::common::serialize_sensitive_string;
29use secrecy::SecretString;
30use serde::Deserialize;
31use serde::Serialize;
32use std::borrow::Cow;
33
34#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
36#[builder(setter(strip_option))]
37pub struct User {
38 #[serde(serialize_with = "serialize_sensitive_string")]
40 #[builder(setter(into))]
41 pub(crate) original_password: SecretString,
42
43 #[serde(serialize_with = "serialize_sensitive_string")]
45 #[builder(setter(into))]
46 pub(crate) password: SecretString,
47}
48
49#[derive(Builder, Debug, Clone)]
50#[builder(setter(strip_option))]
51pub struct Request<'a> {
52 #[builder(setter(into))]
54 pub(crate) user: User,
55
56 #[builder(default, setter(into))]
58 user_id: Cow<'a, str>,
59
60 #[builder(setter(name = "_headers"), default, private)]
61 _headers: Option<HeaderMap>,
62}
63impl<'a> Request<'a> {
64 pub fn builder() -> RequestBuilder<'a> {
66 RequestBuilder::default()
67 }
68}
69
70impl<'a> RequestBuilder<'a> {
71 pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
73 where
74 K: Into<HeaderName>,
75 V: Into<HeaderValue>,
76 {
77 self._headers
78 .get_or_insert(None)
79 .get_or_insert_with(HeaderMap::new)
80 .insert(header_name.into(), header_value.into());
81 self
82 }
83
84 pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
86 where
87 I: Iterator<Item = T>,
88 T: Into<(Option<HeaderName>, HeaderValue)>,
89 {
90 self._headers
91 .get_or_insert(None)
92 .get_or_insert_with(HeaderMap::new)
93 .extend(iter.map(Into::into));
94 self
95 }
96}
97
98impl RestEndpoint for Request<'_> {
99 fn method(&self) -> http::Method {
100 http::Method::POST
101 }
102
103 fn endpoint(&self) -> Cow<'static, str> {
104 format!("users/{user_id}/password", user_id = self.user_id.as_ref(),).into()
105 }
106
107 fn parameters(&self) -> QueryParams<'_> {
108 QueryParams::default()
109 }
110
111 fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
112 let mut params = JsonBodyParams::default();
113
114 params.push("user", serde_json::to_value(&self.user)?);
115
116 params.into_body()
117 }
118
119 fn service_type(&self) -> ServiceType {
120 ServiceType::Identity
121 }
122
123 fn response_key(&self) -> Option<Cow<'static, str>> {
124 None
125 }
126
127 fn request_headers(&self) -> Option<&HeaderMap> {
129 self._headers.as_ref()
130 }
131
132 fn api_version(&self) -> Option<ApiVersion> {
134 Some(ApiVersion::new(3, 0))
135 }
136}
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141 use http::{HeaderName, HeaderValue};
142 use httpmock::MockServer;
143 #[cfg(feature = "sync")]
144 use openstack_sdk_core::api::Query;
145 use openstack_sdk_core::test::client::FakeOpenStackClient;
146 use openstack_sdk_core::types::ServiceType;
147 use serde_json::json;
148
149 #[test]
150 fn test_service_type() {
151 assert_eq!(
152 Request::builder()
153 .user(
154 UserBuilder::default()
155 .original_password("foo")
156 .password("foo")
157 .build()
158 .unwrap()
159 )
160 .build()
161 .unwrap()
162 .service_type(),
163 ServiceType::Identity
164 );
165 }
166
167 #[test]
168 fn test_response_key() {
169 assert!(
170 Request::builder()
171 .user(
172 UserBuilder::default()
173 .original_password("foo")
174 .password("foo")
175 .build()
176 .unwrap()
177 )
178 .build()
179 .unwrap()
180 .response_key()
181 .is_none()
182 )
183 }
184
185 #[cfg(feature = "sync")]
186 #[test]
187 fn endpoint() {
188 let server = MockServer::start();
189 let client = FakeOpenStackClient::new(server.base_url());
190 let mock = server.mock(|when, then| {
191 when.method(httpmock::Method::POST)
192 .path(format!("/users/{user_id}/password", user_id = "user_id",));
193
194 then.status(200)
195 .header("content-type", "application/json")
196 .json_body(json!({ "dummy": {} }));
197 });
198
199 let endpoint = Request::builder()
200 .user_id("user_id")
201 .user(
202 UserBuilder::default()
203 .original_password("foo")
204 .password("foo")
205 .build()
206 .unwrap(),
207 )
208 .build()
209 .unwrap();
210 let _: serde_json::Value = endpoint.query(&client).unwrap();
211 mock.assert();
212 }
213
214 #[cfg(feature = "sync")]
215 #[test]
216 fn endpoint_headers() {
217 let server = MockServer::start();
218 let client = FakeOpenStackClient::new(server.base_url());
219 let mock = server.mock(|when, then| {
220 when.method(httpmock::Method::POST)
221 .path(format!("/users/{user_id}/password", user_id = "user_id",))
222 .header("foo", "bar")
223 .header("not_foo", "not_bar");
224 then.status(200)
225 .header("content-type", "application/json")
226 .json_body(json!({ "dummy": {} }));
227 });
228
229 let endpoint = Request::builder()
230 .user_id("user_id")
231 .user(
232 UserBuilder::default()
233 .original_password("foo")
234 .password("foo")
235 .build()
236 .unwrap(),
237 )
238 .headers(
239 [(
240 Some(HeaderName::from_static("foo")),
241 HeaderValue::from_static("bar"),
242 )]
243 .into_iter(),
244 )
245 .header(
246 HeaderName::from_static("not_foo"),
247 HeaderValue::from_static("not_bar"),
248 )
249 .build()
250 .unwrap();
251 let _: serde_json::Value = endpoint.query(&client).unwrap();
252 mock.assert();
253 }
254}