openstack_sdk/api/compute/v2/server_external_event/
create_293.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
18//! Creates one or more external events, which the API dispatches to the host a
19//! server is assigned to. If the server is not currently assigned to a host
20//! the event will not be delivered.
21//!
22//! You will receive back the list of events that you submitted, with an
23//! updated `code` and `status` indicating their level of success.
24//!
25//! Normal response codes: 200, 207
26//!
27//! A 200 will be returned if all events succeeded, 207 will be returned if any
28//! events could not be processed. The `code` attribute for the event will
29//! explain further what went wrong.
30//!
31//! Error response codes: badRequest(400), unauthorized(401), forbidden(403)
32//!
33use derive_builder::Builder;
34use http::{HeaderMap, HeaderName, HeaderValue};
35
36use crate::api::rest_endpoint_prelude::*;
37
38use serde::Deserialize;
39use serde::Serialize;
40use std::borrow::Cow;
41
42#[derive(Debug, Deserialize, Clone, Serialize)]
43pub enum Name {
44    #[serde(rename = "accelerator-request-bound")]
45    AcceleratorRequestBound,
46    #[serde(rename = "network-changed")]
47    NetworkChanged,
48    #[serde(rename = "network-vif-deleted")]
49    NetworkVifDeleted,
50    #[serde(rename = "network-vif-plugged")]
51    NetworkVifPlugged,
52    #[serde(rename = "network-vif-unplugged")]
53    NetworkVifUnplugged,
54    #[serde(rename = "power-update")]
55    PowerUpdate,
56    #[serde(rename = "volume-extended")]
57    VolumeExtended,
58    #[serde(rename = "volume-reimaged")]
59    VolumeReimaged,
60}
61
62#[derive(Debug, Deserialize, Clone, Serialize)]
63pub enum Status {
64    #[serde(rename = "completed")]
65    Completed,
66    #[serde(rename = "failed")]
67    Failed,
68    #[serde(rename = "in-progress")]
69    InProgress,
70}
71
72#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
73#[builder(setter(strip_option))]
74pub struct Events<'a> {
75    /// The event name. A valid value is:
76    ///
77    /// - `network-changed`
78    /// - `network-vif-plugged`
79    /// - `network-vif-unplugged`
80    /// - `network-vif-deleted`
81    /// - `volume-extended` (since microversion `2.51`)
82    /// - `power-update` (since microversion `2.76`)
83    /// - `accelerator-request-bound` (since microversion `2.82`)
84    /// - `volume-reimaged` (since microversion `2.93`)
85    #[serde()]
86    #[builder()]
87    pub(crate) name: Name,
88
89    /// The UUID of the server instance to which the API dispatches the event.
90    /// You must assign this instance to a host. Otherwise, this call does not
91    /// dispatch the event to the instance.
92    #[serde()]
93    #[builder(setter(into))]
94    pub(crate) server_uuid: Cow<'a, str>,
95
96    /// The event status. A valid value is `failed`, `completed`, or
97    /// `in-progress`. Default is `completed`.
98    #[serde(skip_serializing_if = "Option::is_none")]
99    #[builder(default)]
100    pub(crate) status: Option<Status>,
101
102    /// A string value that identifies the event. Certain types of events
103    /// require specific tags:
104    ///
105    /// - For the `accelerator-request-bound` event, the tag must be the
106    ///   accelerator request UUID.
107    /// - For the `power-update` event the tag must be either be `POWER_ON` or
108    ///   `POWER_OFF`.
109    /// - For the `volume-extended` event the tag must be the volume id.
110    #[serde(skip_serializing_if = "Option::is_none")]
111    #[builder(default, setter(into))]
112    pub(crate) tag: Option<Cow<'a, str>>,
113}
114
115#[derive(Builder, Debug, Clone)]
116#[builder(setter(strip_option))]
117pub struct Request<'a> {
118    /// List of external events to process.
119    #[builder(setter(into))]
120    pub(crate) events: Vec<Events<'a>>,
121
122    #[builder(setter(name = "_headers"), default, private)]
123    _headers: Option<HeaderMap>,
124}
125impl<'a> Request<'a> {
126    /// Create a builder for the endpoint.
127    pub fn builder() -> RequestBuilder<'a> {
128        RequestBuilder::default()
129    }
130}
131
132impl<'a> RequestBuilder<'a> {
133    /// Add a single header to the Server_External_Event.
134    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
135    where
136        K: Into<HeaderName>,
137        V: Into<HeaderValue>,
138    {
139        self._headers
140            .get_or_insert(None)
141            .get_or_insert_with(HeaderMap::new)
142            .insert(header_name.into(), header_value.into());
143        self
144    }
145
146    /// Add multiple headers.
147    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
148    where
149        I: Iterator<Item = T>,
150        T: Into<(Option<HeaderName>, HeaderValue)>,
151    {
152        self._headers
153            .get_or_insert(None)
154            .get_or_insert_with(HeaderMap::new)
155            .extend(iter.map(Into::into));
156        self
157    }
158}
159
160impl RestEndpoint for Request<'_> {
161    fn method(&self) -> http::Method {
162        http::Method::POST
163    }
164
165    fn endpoint(&self) -> Cow<'static, str> {
166        "os-server-external-events".to_string().into()
167    }
168
169    fn parameters(&self) -> QueryParams<'_> {
170        QueryParams::default()
171    }
172
173    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
174        let mut params = JsonBodyParams::default();
175
176        params.push("events", serde_json::to_value(&self.events)?);
177
178        params.into_body()
179    }
180
181    fn service_type(&self) -> ServiceType {
182        ServiceType::Compute
183    }
184
185    fn response_key(&self) -> Option<Cow<'static, str>> {
186        None
187    }
188
189    /// Returns headers to be set into the request
190    fn request_headers(&self) -> Option<&HeaderMap> {
191        self._headers.as_ref()
192    }
193
194    /// Returns required API version
195    fn api_version(&self) -> Option<ApiVersion> {
196        Some(ApiVersion::new(2, 93))
197    }
198}
199
200#[cfg(test)]
201mod tests {
202    use super::*;
203    #[cfg(feature = "sync")]
204    use crate::api::Query;
205    use crate::test::client::FakeOpenStackClient;
206    use crate::types::ServiceType;
207    use http::{HeaderName, HeaderValue};
208    use httpmock::MockServer;
209    use serde_json::json;
210
211    #[test]
212    fn test_service_type() {
213        assert_eq!(
214            Request::builder()
215                .events(Vec::from([EventsBuilder::default()
216                    .name(Name::AcceleratorRequestBound)
217                    .server_uuid("foo")
218                    .build()
219                    .unwrap()]))
220                .build()
221                .unwrap()
222                .service_type(),
223            ServiceType::Compute
224        );
225    }
226
227    #[test]
228    fn test_response_key() {
229        assert!(Request::builder()
230            .events(Vec::from([EventsBuilder::default()
231                .name(Name::AcceleratorRequestBound)
232                .server_uuid("foo")
233                .build()
234                .unwrap()]))
235            .build()
236            .unwrap()
237            .response_key()
238            .is_none())
239    }
240
241    #[cfg(feature = "sync")]
242    #[test]
243    fn endpoint() {
244        let server = MockServer::start();
245        let client = FakeOpenStackClient::new(server.base_url());
246        let mock = server.mock(|when, then| {
247            when.method(httpmock::Method::POST)
248                .path("/os-server-external-events".to_string());
249
250            then.status(200)
251                .header("content-type", "application/json")
252                .json_body(json!({ "dummy": {} }));
253        });
254
255        let endpoint = Request::builder()
256            .events(Vec::from([EventsBuilder::default()
257                .name(Name::AcceleratorRequestBound)
258                .server_uuid("foo")
259                .build()
260                .unwrap()]))
261            .build()
262            .unwrap();
263        let _: serde_json::Value = endpoint.query(&client).unwrap();
264        mock.assert();
265    }
266
267    #[cfg(feature = "sync")]
268    #[test]
269    fn endpoint_headers() {
270        let server = MockServer::start();
271        let client = FakeOpenStackClient::new(server.base_url());
272        let mock = server.mock(|when, then| {
273            when.method(httpmock::Method::POST)
274                .path("/os-server-external-events".to_string())
275                .header("foo", "bar")
276                .header("not_foo", "not_bar");
277            then.status(200)
278                .header("content-type", "application/json")
279                .json_body(json!({ "dummy": {} }));
280        });
281
282        let endpoint = Request::builder()
283            .events(Vec::from([EventsBuilder::default()
284                .name(Name::AcceleratorRequestBound)
285                .server_uuid("foo")
286                .build()
287                .unwrap()]))
288            .headers(
289                [(
290                    Some(HeaderName::from_static("foo")),
291                    HeaderValue::from_static("bar"),
292                )]
293                .into_iter(),
294            )
295            .header(
296                HeaderName::from_static("not_foo"),
297                HeaderValue::from_static("not_bar"),
298            )
299            .build()
300            .unwrap();
301        let _: serde_json::Value = endpoint.query(&client).unwrap();
302        mock.assert();
303    }
304}