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