Skip to main content

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