openstack_sdk/api/compute/v2/migration/
get.rs1use derive_builder::Builder;
32use http::{HeaderMap, HeaderName, HeaderValue};
33
34use crate::api::rest_endpoint_prelude::*;
35
36use std::borrow::Cow;
37
38use crate::api::Pageable;
39#[derive(Builder, Debug, Clone)]
40#[builder(setter(strip_option))]
41pub struct Request<'a> {
42 #[builder(default, setter(into))]
43 changes_before: Option<Cow<'a, str>>,
44
45 #[builder(default, setter(into))]
46 changes_since: Option<Cow<'a, str>>,
47
48 #[builder(default, setter(into))]
49 hidden: Option<Cow<'a, str>>,
50
51 #[builder(default, setter(into))]
52 host: Option<Cow<'a, str>>,
53
54 #[builder(default, setter(into))]
55 instance_uuid: Option<Cow<'a, str>>,
56
57 #[builder(default)]
58 limit: Option<u32>,
59
60 #[builder(default, setter(into))]
61 marker: Option<Cow<'a, str>>,
62
63 #[builder(default, setter(into))]
64 migration_type: Option<Cow<'a, str>>,
65
66 #[builder(default, setter(into))]
67 project_id: Option<Cow<'a, str>>,
68
69 #[builder(default, setter(into))]
70 source_compute: Option<Cow<'a, str>>,
71
72 #[builder(default, setter(into))]
73 status: Option<Cow<'a, str>>,
74
75 #[builder(default, setter(into))]
76 user_id: Option<Cow<'a, str>>,
77
78 #[builder(setter(name = "_headers"), default, private)]
79 _headers: Option<HeaderMap>,
80}
81impl<'a> Request<'a> {
82 pub fn builder() -> RequestBuilder<'a> {
84 RequestBuilder::default()
85 }
86}
87
88impl<'a> RequestBuilder<'a> {
89 pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
91 where
92 K: Into<HeaderName>,
93 V: Into<HeaderValue>,
94 {
95 self._headers
96 .get_or_insert(None)
97 .get_or_insert_with(HeaderMap::new)
98 .insert(header_name.into(), header_value.into());
99 self
100 }
101
102 pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
104 where
105 I: Iterator<Item = T>,
106 T: Into<(Option<HeaderName>, HeaderValue)>,
107 {
108 self._headers
109 .get_or_insert(None)
110 .get_or_insert_with(HeaderMap::new)
111 .extend(iter.map(Into::into));
112 self
113 }
114}
115
116impl RestEndpoint for Request<'_> {
117 fn method(&self) -> http::Method {
118 http::Method::GET
119 }
120
121 fn endpoint(&self) -> Cow<'static, str> {
122 "os-migrations".to_string().into()
123 }
124
125 fn parameters(&self) -> QueryParams<'_> {
126 let mut params = QueryParams::default();
127 params.push_opt("changes-before", self.changes_before.as_ref());
128 params.push_opt("changes-since", self.changes_since.as_ref());
129 params.push_opt("hidden", self.hidden.as_ref());
130 params.push_opt("host", self.host.as_ref());
131 params.push_opt("instance_uuid", self.instance_uuid.as_ref());
132 params.push_opt("limit", self.limit);
133 params.push_opt("marker", self.marker.as_ref());
134 params.push_opt("migration_type", self.migration_type.as_ref());
135 params.push_opt("project_id", self.project_id.as_ref());
136 params.push_opt("source_compute", self.source_compute.as_ref());
137 params.push_opt("status", self.status.as_ref());
138 params.push_opt("user_id", self.user_id.as_ref());
139
140 params
141 }
142
143 fn service_type(&self) -> ServiceType {
144 ServiceType::Compute
145 }
146
147 fn response_key(&self) -> Option<Cow<'static, str>> {
148 Some("migrations".into())
149 }
150
151 fn request_headers(&self) -> Option<&HeaderMap> {
153 self._headers.as_ref()
154 }
155
156 fn api_version(&self) -> Option<ApiVersion> {
158 Some(ApiVersion::new(2, 1))
159 }
160}
161impl Pageable for Request<'_> {}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166 #[cfg(feature = "sync")]
167 use crate::api::Query;
168 use crate::test::client::FakeOpenStackClient;
169 use crate::types::ServiceType;
170 use http::{HeaderName, HeaderValue};
171 use httpmock::MockServer;
172 use serde_json::json;
173
174 #[test]
175 fn test_service_type() {
176 assert_eq!(
177 Request::builder().build().unwrap().service_type(),
178 ServiceType::Compute
179 );
180 }
181
182 #[test]
183 fn test_response_key() {
184 assert_eq!(
185 Request::builder().build().unwrap().response_key().unwrap(),
186 "migrations"
187 );
188 }
189
190 #[cfg(feature = "sync")]
191 #[test]
192 fn endpoint() {
193 let server = MockServer::start();
194 let client = FakeOpenStackClient::new(server.base_url());
195 let mock = server.mock(|when, then| {
196 when.method(httpmock::Method::GET)
197 .path("/os-migrations".to_string());
198
199 then.status(200)
200 .header("content-type", "application/json")
201 .json_body(json!({ "migrations": {} }));
202 });
203
204 let endpoint = Request::builder().build().unwrap();
205 let _: serde_json::Value = endpoint.query(&client).unwrap();
206 mock.assert();
207 }
208
209 #[cfg(feature = "sync")]
210 #[test]
211 fn endpoint_headers() {
212 let server = MockServer::start();
213 let client = FakeOpenStackClient::new(server.base_url());
214 let mock = server.mock(|when, then| {
215 when.method(httpmock::Method::GET)
216 .path("/os-migrations".to_string())
217 .header("foo", "bar")
218 .header("not_foo", "not_bar");
219 then.status(200)
220 .header("content-type", "application/json")
221 .json_body(json!({ "migrations": {} }));
222 });
223
224 let endpoint = Request::builder()
225 .headers(
226 [(
227 Some(HeaderName::from_static("foo")),
228 HeaderValue::from_static("bar"),
229 )]
230 .into_iter(),
231 )
232 .header(
233 HeaderName::from_static("not_foo"),
234 HeaderValue::from_static("not_bar"),
235 )
236 .build()
237 .unwrap();
238 let _: serde_json::Value = endpoint.query(&client).unwrap();
239 mock.assert();
240 }
241}