openstack_sdk/api/compute/v2/server/
rebuild_290.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;
26use std::collections::BTreeMap;
27
28#[derive(Debug, Deserialize, Clone, Serialize)]
29pub enum OsDcfDiskConfig {
30    #[serde(rename = "AUTO")]
31    Auto,
32    #[serde(rename = "MANUAL")]
33    Manual,
34}
35
36/// The action to rebuild a server.
37#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
38#[builder(setter(strip_option))]
39pub struct Rebuild<'a> {
40    /// IPv4 address that should be used to access this server.
41    #[serde(rename = "accessIPv4", skip_serializing_if = "Option::is_none")]
42    #[builder(default, setter(into))]
43    pub(crate) access_ipv4: Option<Cow<'a, str>>,
44
45    /// IPv6 address that should be used to access this server.
46    #[serde(rename = "accessIPv6", skip_serializing_if = "Option::is_none")]
47    #[builder(default, setter(into))]
48    pub(crate) access_ipv6: Option<Cow<'a, str>>,
49
50    /// The administrative password of the server. If you omit this parameter,
51    /// the operation generates a new password.
52    #[serde(rename = "adminPass", skip_serializing_if = "Option::is_none")]
53    #[builder(default, setter(into))]
54    pub(crate) admin_pass: Option<Cow<'a, str>>,
55
56    /// A free form description of the server. Limited to 255 characters in
57    /// length. Before microversion 2.19 this was set to the server name.
58    ///
59    /// **New in version 2.19**
60    #[serde(skip_serializing_if = "Option::is_none")]
61    #[builder(default, setter(into))]
62    pub(crate) description: Option<Option<Cow<'a, str>>>,
63
64    /// The hostname to configure for the instance in the metadata service.
65    ///
66    /// Starting with microversion 2.94, this can be a Fully Qualified Domain
67    /// Name (FQDN) of up to 255 characters in length.
68    ///
69    /// Note
70    ///
71    /// This information is published via the metadata service and requires
72    /// application such as `cloud-init` to propagate it through to the
73    /// instance.
74    ///
75    /// **New in version 2.90**
76    #[serde(skip_serializing_if = "Option::is_none")]
77    #[builder(default, setter(into))]
78    pub(crate) hostname: Option<Cow<'a, str>>,
79
80    /// The UUID of the image to rebuild for your server instance. It must be a
81    /// valid UUID otherwise API will return 400. To rebuild a volume-backed
82    /// server with a new image, at least microversion 2.93 needs to be
83    /// provided in the request else the request will fall back to old
84    /// behaviour i.e. the API will return 400 (for an image different from the
85    /// image used when creating the volume). For non-volume-backed servers,
86    /// specifying a new image will result in validating that the image is
87    /// acceptable for the current compute host on which the server exists. If
88    /// the new image is not valid, the server will go into `ERROR` status.
89    #[serde(rename = "imageRef")]
90    #[builder(setter(into))]
91    pub(crate) image_ref: Cow<'a, str>,
92
93    /// Key pair name for rebuild API. If `null` is specified, the existing
94    /// keypair is unset.
95    ///
96    /// Note
97    ///
98    /// Users within the same project are able to rebuild other user’s
99    /// instances in that project with a new keypair. Keys are owned by users
100    /// (which is the only resource that’s true of). Servers are owned by
101    /// projects. Because of this a rebuild with a key_name is looking up the
102    /// keypair by the user calling rebuild.
103    ///
104    /// **New in version 2.54**
105    #[serde(skip_serializing_if = "Option::is_none")]
106    #[builder(default, setter(into))]
107    pub(crate) key_name: Option<Option<Cow<'a, str>>>,
108
109    /// Metadata key and value pairs. The maximum size of the metadata key and
110    /// value is 255 bytes each.
111    #[serde(skip_serializing_if = "Option::is_none")]
112    #[builder(default, private, setter(into, name = "_metadata"))]
113    pub(crate) metadata: Option<BTreeMap<Cow<'a, str>, Cow<'a, str>>>,
114
115    /// The server name.
116    #[serde(skip_serializing_if = "Option::is_none")]
117    #[builder(default, setter(into))]
118    pub(crate) name: Option<Cow<'a, str>>,
119
120    /// Controls how the API partitions the disk when you create, rebuild, or
121    /// resize servers. A server inherits the `OS-DCF:diskConfig` value from
122    /// the image from which it was created, and an image inherits the
123    /// `OS-DCF:diskConfig` value from the server from which it was created. To
124    /// override the inherited setting, you can include this attribute in the
125    /// request body of a server create, rebuild, or resize request. If the
126    /// `OS-DCF:diskConfig` value for an image is `MANUAL`, you cannot create a
127    /// server from that image and set its `OS-DCF:diskConfig` value to `AUTO`.
128    /// A valid value is:
129    ///
130    /// - `AUTO`. The API builds the server with a single partition the size of
131    ///   the target flavor disk. The API automatically adjusts the file system
132    ///   to fit the entire partition.
133    /// - `MANUAL`. The API builds the server by using whatever partition
134    ///   scheme and file system is in the source image. If the target flavor
135    ///   disk is larger, the API does not partition the remaining disk space.
136    #[serde(rename = "OS-DCF:diskConfig", skip_serializing_if = "Option::is_none")]
137    #[builder(default)]
138    pub(crate) os_dcf_disk_config: Option<OsDcfDiskConfig>,
139
140    /// Indicates whether the server is rebuilt with the preservation of the
141    /// ephemeral partition (`true`).
142    ///
143    /// Note
144    ///
145    /// This only works with baremetal servers provided by Ironic. Passing it
146    /// to any other server instance results in a fault and will prevent the
147    /// rebuild from happening.
148    #[serde(skip_serializing_if = "Option::is_none")]
149    #[builder(default, setter(into))]
150    pub(crate) preserve_ephemeral: Option<bool>,
151
152    /// A list of trusted certificate IDs, which are used during image
153    /// signature verification to verify the signing certificate. The list is
154    /// restricted to a maximum of 50 IDs. This parameter is optional in server
155    /// rebuild requests if allowed by policy, and is not supported for
156    /// volume-backed instances.
157    ///
158    /// If `null` is specified, the existing trusted certificate IDs are either
159    /// unset or reset to the configured defaults.
160    ///
161    /// **New in version 2.63**
162    #[serde(skip_serializing_if = "Option::is_none")]
163    #[builder(default, setter(into))]
164    pub(crate) trusted_image_certificates: Option<Vec<Cow<'a, str>>>,
165
166    /// Configuration information or scripts to use upon rebuild. Must be
167    /// Base64 encoded. Restricted to 65535 bytes. If `null` is specified, the
168    /// existing user_data is unset.
169    ///
170    /// **New in version 2.57**
171    #[serde(skip_serializing_if = "Option::is_none")]
172    #[builder(default, setter(into))]
173    pub(crate) user_data: Option<Option<Cow<'a, str>>>,
174}
175
176impl<'a> RebuildBuilder<'a> {
177    /// Metadata key and value pairs. The maximum size of the metadata key and
178    /// value is 255 bytes each.
179    pub fn metadata<I, K, V>(&mut self, iter: I) -> &mut Self
180    where
181        I: Iterator<Item = (K, V)>,
182        K: Into<Cow<'a, str>>,
183        V: Into<Cow<'a, str>>,
184    {
185        self.metadata
186            .get_or_insert(None)
187            .get_or_insert_with(BTreeMap::new)
188            .extend(iter.map(|(k, v)| (k.into(), v.into())));
189        self
190    }
191}
192
193#[derive(Builder, Debug, Clone)]
194#[builder(setter(strip_option))]
195pub struct Request<'a> {
196    /// The action to rebuild a server.
197    #[builder(setter(into))]
198    pub(crate) rebuild: Rebuild<'a>,
199
200    /// id parameter for /v2.1/servers/{id}/action API
201    #[builder(default, setter(into))]
202    id: Cow<'a, str>,
203
204    #[builder(setter(name = "_headers"), default, private)]
205    _headers: Option<HeaderMap>,
206}
207impl<'a> Request<'a> {
208    /// Create a builder for the endpoint.
209    pub fn builder() -> RequestBuilder<'a> {
210        RequestBuilder::default()
211    }
212}
213
214impl<'a> RequestBuilder<'a> {
215    /// Add a single header to the Server.
216    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
217    where
218        K: Into<HeaderName>,
219        V: Into<HeaderValue>,
220    {
221        self._headers
222            .get_or_insert(None)
223            .get_or_insert_with(HeaderMap::new)
224            .insert(header_name.into(), header_value.into());
225        self
226    }
227
228    /// Add multiple headers.
229    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
230    where
231        I: Iterator<Item = T>,
232        T: Into<(Option<HeaderName>, HeaderValue)>,
233    {
234        self._headers
235            .get_or_insert(None)
236            .get_or_insert_with(HeaderMap::new)
237            .extend(iter.map(Into::into));
238        self
239    }
240}
241
242impl RestEndpoint for Request<'_> {
243    fn method(&self) -> http::Method {
244        http::Method::POST
245    }
246
247    fn endpoint(&self) -> Cow<'static, str> {
248        format!("servers/{id}/action", id = self.id.as_ref(),).into()
249    }
250
251    fn parameters(&self) -> QueryParams<'_> {
252        QueryParams::default()
253    }
254
255    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
256        let mut params = JsonBodyParams::default();
257
258        params.push("rebuild", serde_json::to_value(&self.rebuild)?);
259
260        params.into_body()
261    }
262
263    fn service_type(&self) -> ServiceType {
264        ServiceType::Compute
265    }
266
267    fn response_key(&self) -> Option<Cow<'static, str>> {
268        None
269    }
270
271    /// Returns headers to be set into the request
272    fn request_headers(&self) -> Option<&HeaderMap> {
273        self._headers.as_ref()
274    }
275
276    /// Returns required API version
277    fn api_version(&self) -> Option<ApiVersion> {
278        Some(ApiVersion::new(2, 90))
279    }
280}
281
282#[cfg(test)]
283mod tests {
284    use super::*;
285    #[cfg(feature = "sync")]
286    use crate::api::Query;
287    use crate::test::client::FakeOpenStackClient;
288    use crate::types::ServiceType;
289    use http::{HeaderName, HeaderValue};
290    use httpmock::MockServer;
291    use serde_json::json;
292
293    #[test]
294    fn test_service_type() {
295        assert_eq!(
296            Request::builder()
297                .rebuild(RebuildBuilder::default().image_ref("foo").build().unwrap())
298                .build()
299                .unwrap()
300                .service_type(),
301            ServiceType::Compute
302        );
303    }
304
305    #[test]
306    fn test_response_key() {
307        assert!(Request::builder()
308            .rebuild(RebuildBuilder::default().image_ref("foo").build().unwrap())
309            .build()
310            .unwrap()
311            .response_key()
312            .is_none())
313    }
314
315    #[cfg(feature = "sync")]
316    #[test]
317    fn endpoint() {
318        let server = MockServer::start();
319        let client = FakeOpenStackClient::new(server.base_url());
320        let mock = server.mock(|when, then| {
321            when.method(httpmock::Method::POST)
322                .path(format!("/servers/{id}/action", id = "id",));
323
324            then.status(200)
325                .header("content-type", "application/json")
326                .json_body(json!({ "dummy": {} }));
327        });
328
329        let endpoint = Request::builder()
330            .id("id")
331            .rebuild(RebuildBuilder::default().image_ref("foo").build().unwrap())
332            .build()
333            .unwrap();
334        let _: serde_json::Value = endpoint.query(&client).unwrap();
335        mock.assert();
336    }
337
338    #[cfg(feature = "sync")]
339    #[test]
340    fn endpoint_headers() {
341        let server = MockServer::start();
342        let client = FakeOpenStackClient::new(server.base_url());
343        let mock = server.mock(|when, then| {
344            when.method(httpmock::Method::POST)
345                .path(format!("/servers/{id}/action", id = "id",))
346                .header("foo", "bar")
347                .header("not_foo", "not_bar");
348            then.status(200)
349                .header("content-type", "application/json")
350                .json_body(json!({ "dummy": {} }));
351        });
352
353        let endpoint = Request::builder()
354            .id("id")
355            .rebuild(RebuildBuilder::default().image_ref("foo").build().unwrap())
356            .headers(
357                [(
358                    Some(HeaderName::from_static("foo")),
359                    HeaderValue::from_static("bar"),
360                )]
361                .into_iter(),
362            )
363            .header(
364                HeaderName::from_static("not_foo"),
365                HeaderValue::from_static("not_bar"),
366            )
367            .build()
368            .unwrap();
369        let _: serde_json::Value = endpoint.query(&client).unwrap();
370        mock.assert();
371    }
372}