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