Skip to main content

openstack_cli_compute/v2/flavor/
create_255.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.55]
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_255;
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.55)")]
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    /// A free form description of the flavor. Limited to 65535 characters in
72    /// length. Only printable characters are allowed.
73    ///
74    /// **New in version 2.55**
75    #[arg(help_heading = "Body parameters", long)]
76    description: Option<String>,
77
78    /// Set explicit NULL for the description
79    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
80    no_description: bool,
81
82    /// The size of a dedicated swap disk that will be allocated, in MiB. If 0
83    /// (the default), no dedicated swap disk will be created.
84    #[arg(help_heading = "Body parameters", long)]
85    disk: i32,
86
87    /// Only alphanumeric characters with hyphen ‘-’, underscore ‘\_’, spaces
88    /// and dots ‘.’ are permitted. If an ID is not provided, then a default
89    /// UUID will be assigned.
90    #[arg(help_heading = "Body parameters", long)]
91    id: Option<String>,
92
93    /// Set explicit NULL for the id
94    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "id")]
95    no_id: bool,
96
97    /// The display name of a flavor.
98    #[arg(help_heading = "Body parameters", long)]
99    name: String,
100
101    /// Whether the flavor is public (available to all projects) or scoped to a
102    /// set of projects. Default is True if not specified.
103    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
104    os_flavor_access_is_public: Option<bool>,
105
106    /// The size of a dedicated swap disk that will be allocated, in MiB. If 0
107    /// (the default), no dedicated swap disk will be created.
108    #[arg(help_heading = "Body parameters", long)]
109    os_flv_ext_data_ephemeral: Option<i32>,
110
111    /// The number of virtual CPUs that will be allocated to the server.
112    #[arg(help_heading = "Body parameters", long)]
113    ram: i32,
114
115    /// The receive / transmit factor (as a float) that will be set on ports if
116    /// the network backend supports the QOS extension. Otherwise it will be
117    /// ignored. It defaults to 1.0.
118    ///
119    /// **Available until version 2.101**
120    #[arg(help_heading = "Body parameters", long)]
121    rxtx_factor: Option<String>,
122
123    /// The size of a dedicated swap disk that will be allocated, in MiB. If 0
124    /// (the default), no dedicated swap disk will be created.
125    #[arg(help_heading = "Body parameters", long)]
126    swap: Option<i32>,
127
128    /// The number of virtual CPUs that will be allocated to the server.
129    #[arg(help_heading = "Body parameters", long)]
130    vcpus: i32,
131}
132
133impl FlavorCommand {
134    /// Perform command action
135    pub async fn take_action<C: CliArgs>(
136        &self,
137        parsed_args: &C,
138        client: &mut AsyncOpenStack,
139    ) -> Result<(), OpenStackCliError> {
140        info!("Create Flavor");
141
142        let op = OutputProcessor::from_args(parsed_args, Some("compute.flavor"), Some("create"));
143        op.validate_args(parsed_args)?;
144
145        let mut ep_builder = create_255::Request::builder();
146        ep_builder.header(
147            http::header::HeaderName::from_static("openstack-api-version"),
148            http::header::HeaderValue::from_static("compute 2.55"),
149        );
150
151        // Set body parameters
152        // Set Request.flavor data
153        let args = &self.flavor;
154        let mut flavor_builder = create_255::FlavorBuilder::default();
155        if let Some(val) = &args.os_flv_ext_data_ephemeral {
156            flavor_builder.os_flv_ext_data_ephemeral(*val);
157        }
158
159        if let Some(val) = &args.description {
160            flavor_builder.description(Some(val.into()));
161        } else if args.no_description {
162            flavor_builder.description(None);
163        }
164
165        flavor_builder.disk(args.disk);
166
167        if let Some(val) = &args.id {
168            flavor_builder.id(Some(val.into()));
169        } else if args.no_id {
170            flavor_builder.id(None);
171        }
172
173        flavor_builder.name(&args.name);
174
175        if let Some(val) = &args.os_flavor_access_is_public {
176            flavor_builder.os_flavor_access_is_public(*val);
177        }
178
179        flavor_builder.ram(args.ram);
180
181        if let Some(val) = &args.rxtx_factor {
182            flavor_builder.rxtx_factor(val);
183        }
184
185        if let Some(val) = &args.swap {
186            flavor_builder.swap(*val);
187        }
188
189        flavor_builder.vcpus(args.vcpus);
190
191        ep_builder.flavor(
192            flavor_builder
193                .build()
194                .wrap_err("error preparing the request data")?,
195        );
196
197        let ep = ep_builder
198            .build()
199            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
200
201        let data: serde_json::Value = ep.query_async(client).await?;
202
203        op.output_single::<response::create_255::FlavorResponse>(data.clone())?;
204        // Show command specific hints
205        op.show_command_hint()?;
206        Ok(())
207    }
208}