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