openstack_cli_compute/v2/server_external_event/create_20.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//! Create ServerExternalEvent command [microversion = 2.0]
19//!
20//! Wraps invoking of the `v2.1/os-server-external-events` with `POST` method
21
22use clap::Args;
23use tracing::info;
24
25use openstack_cli_core::cli::CliArgs;
26use openstack_cli_core::error::OpenStackCliError;
27use openstack_cli_core::output::OutputProcessor;
28use openstack_sdk::AsyncOpenStack;
29
30use openstack_sdk::api::QueryAsync;
31use openstack_sdk::api::compute::v2::server_external_event::create_20;
32use openstack_types::compute::v2::server_external_event::response;
33use serde_json::Value;
34
35/// Creates one or more external events, which the API dispatches to the host a
36/// server is assigned to. If the server is not currently assigned to a host
37/// the event will not be delivered.
38///
39/// You will receive back the list of events that you submitted, with an
40/// updated `code` and `status` indicating their level of success.
41///
42/// Normal response codes: 200, 207
43///
44/// A 200 will be returned if all events succeeded, 207 will be returned if any
45/// events could not be processed. The `code` attribute for the event will
46/// explain further what went wrong.
47///
48/// Error response codes: badRequest(400), unauthorized(401), forbidden(403)
49#[derive(Args)]
50#[command(about = "Run Events (microversion = 2.0)")]
51pub struct ServerExternalEventCommand {
52 /// Request Query parameters
53 #[command(flatten)]
54 query: QueryParameters,
55
56 /// Path parameters
57 #[command(flatten)]
58 path: PathParameters,
59
60 /// List of external events to process.
61 ///
62 /// Parameter is an array, may be provided multiple times.
63 #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=openstack_cli_core::common::parse_json)]
64 events: Vec<Value>,
65}
66
67/// Query parameters
68#[derive(Args)]
69struct QueryParameters {}
70
71/// Path parameters
72#[derive(Args)]
73struct PathParameters {}
74
75impl ServerExternalEventCommand {
76 /// Perform command action
77 pub async fn take_action<C: CliArgs>(
78 &self,
79 parsed_args: &C,
80 client: &mut AsyncOpenStack,
81 ) -> Result<(), OpenStackCliError> {
82 info!("Create ServerExternalEvent");
83
84 let op = OutputProcessor::from_args(
85 parsed_args,
86 Some("compute.server_external_event"),
87 Some("create"),
88 );
89 op.validate_args(parsed_args)?;
90
91 let mut ep_builder = create_20::Request::builder();
92 ep_builder.header(
93 http::header::HeaderName::from_static("openstack-api-version"),
94 http::header::HeaderValue::from_static("compute 2.0"),
95 );
96
97 // Set body parameters
98 // Set Request.events data
99
100 let events_builder: Vec<create_20::Events> = self
101 .events
102 .iter()
103 .flat_map(|v| serde_json::from_value::<create_20::Events>(v.to_owned()))
104 .collect::<Vec<create_20::Events>>();
105 ep_builder.events(events_builder);
106
107 let ep = ep_builder
108 .build()
109 .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
110
111 let data: serde_json::Value = ep.query_async(client).await?;
112
113 op.output_single::<response::create_20::ServerExternalEventResponse>(data.clone())?;
114 // Show command specific hints
115 op.show_command_hint()?;
116 Ok(())
117 }
118}