Skip to main content

openstack_cli_compute/v2/flavor/
create_21.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 Flavor command [microversion = 2.1]
19//!
20//! Wraps invoking of the `v2.1/flavors` 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 openstack_sdk::api::QueryAsync;
32use openstack_sdk::api::compute::v2::flavor::create_21;
33use openstack_types::compute::v2::flavor::response;
34
35/// Creates a flavor.
36///
37/// Creating a flavor is typically only available to administrators of a cloud
38/// because this has implications for scheduling efficiently in the cloud.
39///
40/// Normal response codes: 200
41///
42/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
43/// conflict(409)
44#[derive(Args)]
45#[command(about = "Create Flavor (microversion = 2.1)")]
46pub struct FlavorCommand {
47    /// Request Query parameters
48    #[command(flatten)]
49    query: QueryParameters,
50
51    /// Path parameters
52    #[command(flatten)]
53    path: PathParameters,
54
55    /// The ID and links for the flavor for your server instance. A flavor is a
56    /// combination of memory, disk size, and CPUs.
57    #[command(flatten)]
58    flavor: Flavor,
59}
60
61/// Query parameters
62#[derive(Args)]
63struct QueryParameters {}
64
65/// Path parameters
66#[derive(Args)]
67struct PathParameters {}
68/// Flavor Body data
69#[derive(Args, Clone)]
70struct Flavor {
71    /// The size of a dedicated swap disk that will be allocated, in MiB. If 0
72    /// (the default), no dedicated swap disk will be created.
73    #[arg(help_heading = "Body parameters", long)]
74    disk: i32,
75
76    /// Only alphanumeric characters with hyphen ‘-’, underscore ‘\_’, spaces
77    /// and dots ‘.’ are permitted. If an ID is not provided, then a default
78    /// UUID will be assigned.
79    #[arg(help_heading = "Body parameters", long)]
80    id: Option<String>,
81
82    /// Set explicit NULL for the id
83    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "id")]
84    no_id: bool,
85
86    /// The display name of a flavor.
87    #[arg(help_heading = "Body parameters", long)]
88    name: String,
89
90    /// Whether the flavor is public (available to all projects) or scoped to a
91    /// set of projects. Default is True if not specified.
92    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
93    os_flavor_access_is_public: Option<bool>,
94
95    /// The size of a dedicated swap disk that will be allocated, in MiB. If 0
96    /// (the default), no dedicated swap disk will be created.
97    #[arg(help_heading = "Body parameters", long)]
98    os_flv_ext_data_ephemeral: Option<i32>,
99
100    /// The number of virtual CPUs that will be allocated to the server.
101    #[arg(help_heading = "Body parameters", long)]
102    ram: i32,
103
104    /// The receive / transmit factor (as a float) that will be set on ports if
105    /// the network backend supports the QOS extension. Otherwise it will be
106    /// ignored. It defaults to 1.0.
107    ///
108    /// **Available until version 2.101**
109    #[arg(help_heading = "Body parameters", long)]
110    rxtx_factor: Option<String>,
111
112    /// The size of a dedicated swap disk that will be allocated, in MiB. If 0
113    /// (the default), no dedicated swap disk will be created.
114    #[arg(help_heading = "Body parameters", long)]
115    swap: Option<i32>,
116
117    /// The number of virtual CPUs that will be allocated to the server.
118    #[arg(help_heading = "Body parameters", long)]
119    vcpus: i32,
120}
121
122impl FlavorCommand {
123    /// Perform command action
124    pub async fn take_action<C: CliArgs>(
125        &self,
126        parsed_args: &C,
127        client: &mut AsyncOpenStack,
128    ) -> Result<(), OpenStackCliError> {
129        info!("Create Flavor");
130
131        let op = OutputProcessor::from_args(parsed_args, Some("compute.flavor"), Some("create"));
132        op.validate_args(parsed_args)?;
133
134        let mut ep_builder = create_21::Request::builder();
135        ep_builder.header(
136            http::header::HeaderName::from_static("openstack-api-version"),
137            http::header::HeaderValue::from_static("compute 2.1"),
138        );
139
140        // Set body parameters
141        // Set Request.flavor data
142        let args = &self.flavor;
143        let mut flavor_builder = create_21::FlavorBuilder::default();
144        if let Some(val) = &args.os_flv_ext_data_ephemeral {
145            flavor_builder.os_flv_ext_data_ephemeral(*val);
146        }
147
148        flavor_builder.disk(args.disk);
149
150        if let Some(val) = &args.id {
151            flavor_builder.id(Some(val.into()));
152        } else if args.no_id {
153            flavor_builder.id(None);
154        }
155
156        flavor_builder.name(&args.name);
157
158        if let Some(val) = &args.os_flavor_access_is_public {
159            flavor_builder.os_flavor_access_is_public(*val);
160        }
161
162        flavor_builder.ram(args.ram);
163
164        if let Some(val) = &args.rxtx_factor {
165            flavor_builder.rxtx_factor(val);
166        }
167
168        if let Some(val) = &args.swap {
169            flavor_builder.swap(*val);
170        }
171
172        flavor_builder.vcpus(args.vcpus);
173
174        ep_builder.flavor(
175            flavor_builder
176                .build()
177                .wrap_err("error preparing the request data")?,
178        );
179
180        let ep = ep_builder
181            .build()
182            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
183
184        let data: serde_json::Value = ep.query_async(client).await?;
185
186        op.output_single::<response::create_20::FlavorResponse>(data.clone())?;
187        // Show command specific hints
188        op.show_command_hint()?;
189        Ok(())
190    }
191}