Skip to main content

openstack_cli_network/v2/flavor/
create.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
19//!
20//! Wraps invoking of the `v2.0/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::network::v2::flavor::create;
33use openstack_types::network::v2::flavor::response;
34
35/// Creates a flavor.
36///
37/// This operation establishes a new flavor.
38///
39/// The service_type to which the flavor applies is a required parameter. The
40/// corresponding service plugin must have been activated as part of the
41/// configuration. Check [Service providers](#list-service-providers) for how
42/// to see currently loaded service types. Additionally the service plugin
43/// needs to support the use of flavors.
44///
45/// Creation currently limited to administrators. Other users will receive a
46/// `Forbidden 403` response code with a response body NeutronError message
47/// expressing that creation is disallowed by policy.
48///
49/// Until one or more service profiles are associated with the flavor by the
50/// operator, attempts to use the flavor during resource creations will
51/// currently return a `Not Found 404` with a response body that indicates no
52/// service profile could be found.
53///
54/// If the API cannot fulfill the request due to insufficient data or data that
55/// is not valid, the service returns the HTTP `Bad Request (400)` response
56/// code with information about the failure in the response body. Validation
57/// errors require that you correct the error and submit the request again.
58///
59/// Normal response codes: 201
60///
61/// Error response codes: 400, 401, 403, 404
62#[derive(Args)]
63#[command(about = "Create flavor")]
64pub struct FlavorCommand {
65    /// Request Query parameters
66    #[command(flatten)]
67    query: QueryParameters,
68
69    /// Path parameters
70    #[command(flatten)]
71    path: PathParameters,
72
73    /// A `flavor` object.
74    #[command(flatten)]
75    flavor: Flavor,
76}
77
78/// Query parameters
79#[derive(Args)]
80struct QueryParameters {}
81
82/// Path parameters
83#[derive(Args)]
84struct PathParameters {}
85/// Flavor Body data
86#[derive(Args, Clone)]
87struct Flavor {
88    /// The human-readable description for the flavor.
89    #[arg(help_heading = "Body parameters", long)]
90    description: Option<String>,
91
92    /// Set explicit NULL for the description
93    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
94    no_description: bool,
95
96    /// Indicates whether the flavor is enabled or not. Default is true.
97    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
98    enabled: Option<Option<bool>>,
99
100    /// Name of the flavor.
101    #[arg(help_heading = "Body parameters", long)]
102    name: Option<String>,
103
104    /// Parameter is an array, may be provided multiple times.
105    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
106    service_profiles: Option<Vec<String>>,
107
108    /// Service type for the flavor. Example: FIREWALL.
109    #[arg(help_heading = "Body parameters", long)]
110    service_type: Option<String>,
111}
112
113impl FlavorCommand {
114    /// Perform command action
115    pub async fn take_action<C: CliArgs>(
116        &self,
117        parsed_args: &C,
118        client: &mut AsyncOpenStack,
119    ) -> Result<(), OpenStackCliError> {
120        info!("Create Flavor");
121
122        let op = OutputProcessor::from_args(parsed_args, Some("network.flavor"), Some("create"));
123        op.validate_args(parsed_args)?;
124
125        let mut ep_builder = create::Request::builder();
126
127        // Set body parameters
128        // Set Request.flavor data
129        let args = &self.flavor;
130        let mut flavor_builder = create::FlavorBuilder::default();
131        if let Some(val) = &args.description {
132            flavor_builder.description(Some(val.into()));
133        } else if args.no_description {
134            flavor_builder.description(None);
135        }
136
137        if let Some(val) = &args.enabled {
138            flavor_builder.enabled(*val);
139        }
140
141        if let Some(val) = &args.name {
142            flavor_builder.name(val);
143        }
144
145        if let Some(val) = &args.service_profiles {
146            flavor_builder.service_profiles(val.iter().map(Into::into).collect::<Vec<_>>());
147        }
148
149        if let Some(val) = &args.service_type {
150            flavor_builder.service_type(val);
151        }
152
153        ep_builder.flavor(
154            flavor_builder
155                .build()
156                .wrap_err("error preparing the request data")?,
157        );
158
159        let ep = ep_builder
160            .build()
161            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
162
163        let data: serde_json::Value = ep.query_async(client).await?;
164
165        op.output_single::<response::create::FlavorResponse>(data.clone())?;
166        // Show command specific hints
167        op.show_command_hint()?;
168        Ok(())
169    }
170}