Skip to main content

openstack_cli_load_balancer/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/lbaas/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::load_balancer::v2::flavor::create;
33use openstack_types::load_balancer::v2::flavor::response;
34
35/// Creates a flavor.
36#[derive(Args)]
37pub struct FlavorCommand {
38    /// Request Query parameters
39    #[command(flatten)]
40    query: QueryParameters,
41
42    /// Path parameters
43    #[command(flatten)]
44    path: PathParameters,
45
46    /// Defines mandatory and optional attributes of a POST request.
47    #[command(flatten)]
48    flavor: Flavor,
49}
50
51/// Query parameters
52#[derive(Args)]
53struct QueryParameters {}
54
55/// Path parameters
56#[derive(Args)]
57struct PathParameters {}
58/// Flavor Body data
59#[derive(Args, Clone)]
60struct Flavor {
61    #[arg(help_heading = "Body parameters", long)]
62    description: Option<String>,
63
64    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
65    enabled: Option<bool>,
66
67    #[arg(help_heading = "Body parameters", long)]
68    flavor_profile_id: String,
69
70    #[arg(help_heading = "Body parameters", long)]
71    name: String,
72}
73
74impl FlavorCommand {
75    /// Perform command action
76    pub async fn take_action<C: CliArgs>(
77        &self,
78        parsed_args: &C,
79        client: &mut AsyncOpenStack,
80    ) -> Result<(), OpenStackCliError> {
81        info!("Create Flavor");
82
83        let op =
84            OutputProcessor::from_args(parsed_args, Some("load-balancer.flavor"), Some("create"));
85        op.validate_args(parsed_args)?;
86
87        let mut ep_builder = create::Request::builder();
88
89        // Set body parameters
90        // Set Request.flavor data
91        let args = &self.flavor;
92        let mut flavor_builder = create::FlavorBuilder::default();
93        if let Some(val) = &args.description {
94            flavor_builder.description(val);
95        }
96
97        if let Some(val) = &args.enabled {
98            flavor_builder.enabled(*val);
99        }
100
101        flavor_builder.flavor_profile_id(&args.flavor_profile_id);
102
103        flavor_builder.name(&args.name);
104
105        ep_builder.flavor(
106            flavor_builder
107                .build()
108                .wrap_err("error preparing the request data")?,
109        );
110
111        let ep = ep_builder
112            .build()
113            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
114
115        let data: serde_json::Value = ep.query_async(client).await?;
116
117        op.output_single::<response::create::FlavorResponse>(data.clone())?;
118        // Show command specific hints
119        op.show_command_hint()?;
120        Ok(())
121    }
122}