Skip to main content

openstack_cli_compute/v2/server_group/
create_264.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.64]
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_264;
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.64)")]
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 Policy {
68    Affinity,
69    AntiAffinity,
70    SoftAffinity,
71    SoftAntiAffinity,
72}
73
74/// Rules Body data
75#[derive(Args, Clone)]
76#[group(required = false, multiple = true)]
77struct Rules {
78    #[arg(help_heading = "Body parameters", long)]
79    max_server_per_host: Option<i32>,
80}
81
82/// ServerGroup Body data
83#[derive(Args, Clone)]
84struct ServerGroup {
85    /// The name of the server group.
86    #[arg(help_heading = "Body parameters", long)]
87    name: String,
88
89    /// The `policy` field represents the name of the policy. The current valid
90    /// policy names are:
91    ///
92    /// - `anti-affinity` - servers in this group must be scheduled to
93    ///   different hosts.
94    /// - `affinity` - servers in this group must be scheduled to the same
95    ///   host.
96    /// - `soft-anti-affinity` - servers in this group should be scheduled to
97    ///   different hosts if possible, but if not possible then they should
98    ///   still be scheduled instead of resulting in a build failure.
99    /// - `soft-affinity` - servers in this group should be scheduled to the
100    ///   same host if possible, but if not possible then they should still be
101    ///   scheduled instead of resulting in a build failure.
102    ///
103    /// **New in version 2.64**
104    #[arg(help_heading = "Body parameters", long)]
105    policy: Policy,
106
107    /// The `rules` field, which is a dict, can be applied to the policy.
108    /// Currently, only the `max_server_per_host` rule is supported for the
109    /// `anti-affinity` policy. The `max_server_per_host` rule allows
110    /// specifying how many members of the anti-affinity group can reside on
111    /// the same compute host. If not specified, only one member from the same
112    /// anti-affinity group can reside on a given host. Requesting policy rules
113    /// with any other policy than `anti-affinity` will be 400.
114    ///
115    /// **New in version 2.64**
116    #[command(flatten)]
117    rules: Option<Rules>,
118}
119
120impl ServerGroupCommand {
121    /// Perform command action
122    pub async fn take_action<C: CliArgs>(
123        &self,
124        parsed_args: &C,
125        client: &mut AsyncOpenStack,
126    ) -> Result<(), OpenStackCliError> {
127        info!("Create ServerGroup");
128
129        let op =
130            OutputProcessor::from_args(parsed_args, Some("compute.server_group"), Some("create"));
131        op.validate_args(parsed_args)?;
132
133        let mut ep_builder = create_264::Request::builder();
134        ep_builder.header(
135            http::header::HeaderName::from_static("openstack-api-version"),
136            http::header::HeaderValue::from_static("compute 2.64"),
137        );
138
139        // Set body parameters
140        // Set Request.server_group data
141        let args = &self.server_group;
142        let mut server_group_builder = create_264::ServerGroupBuilder::default();
143
144        server_group_builder.name(&args.name);
145
146        let tmp = match &args.policy {
147            Policy::Affinity => create_264::Policy::Affinity,
148            Policy::AntiAffinity => create_264::Policy::AntiAffinity,
149            Policy::SoftAffinity => create_264::Policy::SoftAffinity,
150            Policy::SoftAntiAffinity => create_264::Policy::SoftAntiAffinity,
151        };
152        server_group_builder.policy(tmp);
153
154        if let Some(val) = &args.rules {
155            let mut rules_builder = create_264::RulesBuilder::default();
156            if let Some(val) = &val.max_server_per_host {
157                rules_builder.max_server_per_host(*val);
158            }
159            server_group_builder.rules(
160                rules_builder
161                    .build()
162                    .wrap_err("error preparing the request data")?,
163            );
164        }
165
166        ep_builder.server_group(
167            server_group_builder
168                .build()
169                .wrap_err("error preparing the request data")?,
170        );
171
172        let ep = ep_builder
173            .build()
174            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
175
176        let data: serde_json::Value = ep.query_async(client).await?;
177
178        op.output_single::<response::create_264::ServerGroupResponse>(data.clone())?;
179        // Show command specific hints
180        op.show_command_hint()?;
181        Ok(())
182    }
183}