openstack_cli_compute/v2/server_group/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 ServerGroup command [microversion = 2.0]
19//!
20//! Wraps invoking of the `v2.1/os-server-groups` with `POST` method
21
22use clap::Args;
23use eyre::WrapErr;
24use tracing::info;
25
26use openstack_cli_core::cli::CliArgs;
27use openstack_cli_core::error::OpenStackCliError;
28use openstack_cli_core::output::OutputProcessor;
29use openstack_sdk::AsyncOpenStack;
30
31use clap::ValueEnum;
32use openstack_sdk::api::QueryAsync;
33use openstack_sdk::api::compute::v2::server_group::create_20;
34
35/// Creates a server group.
36///
37/// Normal response codes: 200
38///
39/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
40/// conflict(409)
41#[derive(Args)]
42#[command(about = "Create Server Group (microversion = 2.0)")]
43pub struct ServerGroupCommand {
44 /// Request Query parameters
45 #[command(flatten)]
46 query: QueryParameters,
47
48 /// Path parameters
49 #[command(flatten)]
50 path: PathParameters,
51
52 /// The server group object.
53 #[command(flatten)]
54 server_group: ServerGroup,
55}
56
57/// Query parameters
58#[derive(Args)]
59struct QueryParameters {}
60
61/// Path parameters
62#[derive(Args)]
63struct PathParameters {}
64
65#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
66enum Policies {
67 Affinity,
68 AntiAffinity,
69}
70
71/// ServerGroup Body data
72#[derive(Args, Clone)]
73struct ServerGroup {
74 /// The name of the server group.
75 #[arg(help_heading = "Body parameters", long)]
76 name: String,
77
78 /// A list of exactly one policy name to associate with the server group.
79 /// The current valid policy names are:
80 ///
81 /// - `anti-affinity` - servers in this group must be scheduled to
82 /// different hosts.
83 /// - `affinity` - servers in this group must be scheduled to the same
84 /// host.
85 /// - `soft-anti-affinity` - servers in this group should be scheduled to
86 /// different hosts if possible, but if not possible then they should
87 /// still be scheduled instead of resulting in a build failure. This
88 /// policy was added in microversion 2.15.
89 /// - `soft-affinity` - servers in this group should be scheduled to the
90 /// same host if possible, but if not possible then they should still be
91 /// scheduled instead of resulting in a build failure. This policy was
92 /// added in microversion 2.15.
93 ///
94 /// **Available until version 2.63**
95 ///
96 /// Parameter is an array, may be provided multiple times.
97 #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
98 policies: Vec<Policies>,
99}
100
101impl ServerGroupCommand {
102 /// Perform command action
103 pub async fn take_action<C: CliArgs>(
104 &self,
105 parsed_args: &C,
106 client: &mut AsyncOpenStack,
107 ) -> Result<(), OpenStackCliError> {
108 info!("Create ServerGroup");
109
110 let op =
111 OutputProcessor::from_args(parsed_args, Some("compute.server_group"), Some("create"));
112 op.validate_args(parsed_args)?;
113
114 let mut ep_builder = create_20::Request::builder();
115 ep_builder.header(
116 http::header::HeaderName::from_static("openstack-api-version"),
117 http::header::HeaderValue::from_static("compute 2.0"),
118 );
119
120 // Set body parameters
121 // Set Request.server_group data
122 let args = &self.server_group;
123 let mut server_group_builder = create_20::ServerGroupBuilder::default();
124
125 server_group_builder.name(&args.name);
126
127 server_group_builder.policies(
128 args.policies
129 .iter()
130 .map(|x| match x {
131 Policies::Affinity => create_20::Policies::Affinity,
132 Policies::AntiAffinity => create_20::Policies::AntiAffinity,
133 })
134 .collect::<Vec<_>>(),
135 );
136
137 ep_builder.server_group(
138 server_group_builder
139 .build()
140 .wrap_err("error preparing the request data")?,
141 );
142
143 let ep = ep_builder
144 .build()
145 .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
146 openstack_sdk::api::ignore(ep).query_async(client).await?;
147 // Show command specific hints
148 op.show_command_hint()?;
149 Ok(())
150 }
151}