openstack_sdk/api/compute/v2/server/
resize.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(Debug, Deserialize, Clone, Serialize)]
28pub enum OsDcfDiskConfig {
29    #[serde(rename = "AUTO")]
30    Auto,
31    #[serde(rename = "MANUAL")]
32    Manual,
33}
34
35/// The action to resize a server.
36#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
37#[builder(setter(strip_option))]
38pub struct Resize<'a> {
39    /// The flavor ID for resizing the server. The size of the disk in the
40    /// flavor being resized to must be greater than or equal to the size of
41    /// the disk in the current flavor.
42    ///
43    /// If a specified flavor ID is the same as the current one of the server,
44    /// the request returns a `Bad Request (400)` response code.
45    #[serde(rename = "flavorRef")]
46    #[builder(setter(into))]
47    pub(crate) flavor_ref: Cow<'a, str>,
48
49    /// Controls how the API partitions the disk when you create, rebuild, or
50    /// resize servers. A server inherits the `OS-DCF:diskConfig` value from
51    /// the image from which it was created, and an image inherits the
52    /// `OS-DCF:diskConfig` value from the server from which it was created. To
53    /// override the inherited setting, you can include this attribute in the
54    /// request body of a server create, rebuild, or resize request. If the
55    /// `OS-DCF:diskConfig` value for an image is `MANUAL`, you cannot create a
56    /// server from that image and set its `OS-DCF:diskConfig` value to `AUTO`.
57    /// A valid value is:
58    ///
59    /// - `AUTO`. The API builds the server with a single partition the size of
60    ///   the target flavor disk. The API automatically adjusts the file system
61    ///   to fit the entire partition.
62    /// - `MANUAL`. The API builds the server by using whatever partition
63    ///   scheme and file system is in the source image. If the target flavor
64    ///   disk is larger, the API does not partition the remaining disk space.
65    #[serde(rename = "OS-DCF:diskConfig", skip_serializing_if = "Option::is_none")]
66    #[builder(default)]
67    pub(crate) os_dcf_disk_config: Option<OsDcfDiskConfig>,
68}
69
70#[derive(Builder, Debug, Clone)]
71#[builder(setter(strip_option))]
72pub struct Request<'a> {
73    /// The action to resize a server.
74    #[builder(setter(into))]
75    pub(crate) resize: Resize<'a>,
76
77    /// id parameter for /v2.1/servers/{id}/action API
78    #[builder(default, setter(into))]
79    id: Cow<'a, str>,
80
81    #[builder(setter(name = "_headers"), default, private)]
82    _headers: Option<HeaderMap>,
83}
84impl<'a> Request<'a> {
85    /// Create a builder for the endpoint.
86    pub fn builder() -> RequestBuilder<'a> {
87        RequestBuilder::default()
88    }
89}
90
91impl<'a> RequestBuilder<'a> {
92    /// Add a single header to the Server.
93    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
94    where
95        K: Into<HeaderName>,
96        V: Into<HeaderValue>,
97    {
98        self._headers
99            .get_or_insert(None)
100            .get_or_insert_with(HeaderMap::new)
101            .insert(header_name.into(), header_value.into());
102        self
103    }
104
105    /// Add multiple headers.
106    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
107    where
108        I: Iterator<Item = T>,
109        T: Into<(Option<HeaderName>, HeaderValue)>,
110    {
111        self._headers
112            .get_or_insert(None)
113            .get_or_insert_with(HeaderMap::new)
114            .extend(iter.map(Into::into));
115        self
116    }
117}
118
119impl RestEndpoint for Request<'_> {
120    fn method(&self) -> http::Method {
121        http::Method::POST
122    }
123
124    fn endpoint(&self) -> Cow<'static, str> {
125        format!("servers/{id}/action", id = self.id.as_ref(),).into()
126    }
127
128    fn parameters(&self) -> QueryParams<'_> {
129        QueryParams::default()
130    }
131
132    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
133        let mut params = JsonBodyParams::default();
134
135        params.push("resize", serde_json::to_value(&self.resize)?);
136
137        params.into_body()
138    }
139
140    fn service_type(&self) -> ServiceType {
141        ServiceType::Compute
142    }
143
144    fn response_key(&self) -> Option<Cow<'static, str>> {
145        None
146    }
147
148    /// Returns headers to be set into the request
149    fn request_headers(&self) -> Option<&HeaderMap> {
150        self._headers.as_ref()
151    }
152
153    /// Returns required API version
154    fn api_version(&self) -> Option<ApiVersion> {
155        Some(ApiVersion::new(2, 1))
156    }
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162    #[cfg(feature = "sync")]
163    use crate::api::Query;
164    use crate::test::client::FakeOpenStackClient;
165    use crate::types::ServiceType;
166    use http::{HeaderName, HeaderValue};
167    use httpmock::MockServer;
168    use serde_json::json;
169
170    #[test]
171    fn test_service_type() {
172        assert_eq!(
173            Request::builder()
174                .resize(ResizeBuilder::default().flavor_ref("foo").build().unwrap())
175                .build()
176                .unwrap()
177                .service_type(),
178            ServiceType::Compute
179        );
180    }
181
182    #[test]
183    fn test_response_key() {
184        assert!(Request::builder()
185            .resize(ResizeBuilder::default().flavor_ref("foo").build().unwrap())
186            .build()
187            .unwrap()
188            .response_key()
189            .is_none())
190    }
191
192    #[cfg(feature = "sync")]
193    #[test]
194    fn endpoint() {
195        let server = MockServer::start();
196        let client = FakeOpenStackClient::new(server.base_url());
197        let mock = server.mock(|when, then| {
198            when.method(httpmock::Method::POST)
199                .path(format!("/servers/{id}/action", id = "id",));
200
201            then.status(200)
202                .header("content-type", "application/json")
203                .json_body(json!({ "dummy": {} }));
204        });
205
206        let endpoint = Request::builder()
207            .id("id")
208            .resize(ResizeBuilder::default().flavor_ref("foo").build().unwrap())
209            .build()
210            .unwrap();
211        let _: serde_json::Value = endpoint.query(&client).unwrap();
212        mock.assert();
213    }
214
215    #[cfg(feature = "sync")]
216    #[test]
217    fn endpoint_headers() {
218        let server = MockServer::start();
219        let client = FakeOpenStackClient::new(server.base_url());
220        let mock = server.mock(|when, then| {
221            when.method(httpmock::Method::POST)
222                .path(format!("/servers/{id}/action", id = "id",))
223                .header("foo", "bar")
224                .header("not_foo", "not_bar");
225            then.status(200)
226                .header("content-type", "application/json")
227                .json_body(json!({ "dummy": {} }));
228        });
229
230        let endpoint = Request::builder()
231            .id("id")
232            .resize(ResizeBuilder::default().flavor_ref("foo").build().unwrap())
233            .headers(
234                [(
235                    Some(HeaderName::from_static("foo")),
236                    HeaderValue::from_static("bar"),
237                )]
238                .into_iter(),
239            )
240            .header(
241                HeaderName::from_static("not_foo"),
242                HeaderValue::from_static("not_bar"),
243            )
244            .build()
245            .unwrap();
246        let _: serde_json::Value = endpoint.query(&client).unwrap();
247        mock.assert();
248    }
249}