openstack_cli 0.13.5

OpenStack client rewritten in Rust
Documentation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
//
// WARNING: This file is automatically generated from OpenAPI schema using
// `openstack-codegenerator`.

//! Create Flavor command [microversion = 2.0]
//!
//! Wraps invoking of the `v2.1/flavors` with `POST` method

use clap::Args;
use eyre::WrapErr;
use tracing::info;

use openstack_sdk::AsyncOpenStack;

use crate::Cli;
use crate::OpenStackCliError;
use crate::output::OutputProcessor;

use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::compute::v2::flavor::create_20;
use openstack_types::compute::v2::flavor::response::create::FlavorResponse;

/// Creates a flavor.
///
/// Creating a flavor is typically only available to administrators of a cloud
/// because this has implications for scheduling efficiently in the cloud.
///
/// Normal response codes: 200
///
/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
/// conflict(409)
#[derive(Args)]
#[command(about = "Create Flavor (microversion = 2.0)")]
pub struct FlavorCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

    /// Path parameters
    #[command(flatten)]
    path: PathParameters,

    /// The ID and links for the flavor for your server instance. A flavor is a
    /// combination of memory, disk size, and CPUs.
    #[command(flatten)]
    flavor: Flavor,
}

/// Query parameters
#[derive(Args)]
struct QueryParameters {}

/// Path parameters
#[derive(Args)]
struct PathParameters {}
/// Flavor Body data
#[derive(Args, Clone)]
struct Flavor {
    /// The size of a dedicated swap disk that will be allocated, in MiB. If 0
    /// (the default), no dedicated swap disk will be created.
    #[arg(help_heading = "Body parameters", long)]
    disk: i32,

    /// Only alphanumeric characters with hyphen ‘-’, underscore ‘\_’, spaces
    /// and dots ‘.’ are permitted. If an ID is not provided, then a default
    /// UUID will be assigned.
    #[arg(help_heading = "Body parameters", long)]
    id: Option<String>,

    /// Set explicit NULL for the id
    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "id")]
    no_id: bool,

    /// The display name of a flavor.
    #[arg(help_heading = "Body parameters", long)]
    name: String,

    /// Whether the flavor is public (available to all projects) or scoped to a
    /// set of projects. Default is True if not specified.
    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
    os_flavor_access_is_public: Option<bool>,

    /// The size of a dedicated swap disk that will be allocated, in MiB. If 0
    /// (the default), no dedicated swap disk will be created.
    #[arg(help_heading = "Body parameters", long)]
    os_flv_ext_data_ephemeral: Option<i32>,

    /// The number of virtual CPUs that will be allocated to the server.
    #[arg(help_heading = "Body parameters", long)]
    ram: i32,

    /// The receive / transmit factor (as a float) that will be set on ports if
    /// the network backend supports the QOS extension. Otherwise it will be
    /// ignored. It defaults to 1.0.
    #[arg(help_heading = "Body parameters", long)]
    rxtx_factor: Option<String>,

    /// The size of a dedicated swap disk that will be allocated, in MiB. If 0
    /// (the default), no dedicated swap disk will be created.
    #[arg(help_heading = "Body parameters", long)]
    swap: Option<i32>,

    /// The number of virtual CPUs that will be allocated to the server.
    #[arg(help_heading = "Body parameters", long)]
    vcpus: i32,
}

impl FlavorCommand {
    /// Perform command action
    pub async fn take_action(
        &self,
        parsed_args: &Cli,
        client: &mut AsyncOpenStack,
    ) -> Result<(), OpenStackCliError> {
        info!("Create Flavor");

        let op = OutputProcessor::from_args(parsed_args, Some("compute.flavor"), Some("create"));
        op.validate_args(parsed_args)?;

        let mut ep_builder = create_20::Request::builder();
        ep_builder.header(
            http::header::HeaderName::from_static("openstack-api-version"),
            http::header::HeaderValue::from_static("compute 2.0"),
        );

        // Set body parameters
        // Set Request.flavor data
        let args = &self.flavor;
        let mut flavor_builder = create_20::FlavorBuilder::default();
        if let Some(val) = &args.os_flv_ext_data_ephemeral {
            flavor_builder.os_flv_ext_data_ephemeral(*val);
        }

        flavor_builder.disk(args.disk);

        if let Some(val) = &args.id {
            flavor_builder.id(Some(val.into()));
        } else if args.no_id {
            flavor_builder.id(None);
        }

        flavor_builder.name(&args.name);

        if let Some(val) = &args.os_flavor_access_is_public {
            flavor_builder.os_flavor_access_is_public(*val);
        }

        flavor_builder.ram(args.ram);

        if let Some(val) = &args.rxtx_factor {
            flavor_builder.rxtx_factor(val);
        }

        if let Some(val) = &args.swap {
            flavor_builder.swap(*val);
        }

        flavor_builder.vcpus(args.vcpus);

        ep_builder.flavor(
            flavor_builder
                .build()
                .wrap_err("error preparing the request data")?,
        );

        let ep = ep_builder
            .build()
            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;

        let data = ep.query_async(client).await?;
        op.output_single::<FlavorResponse>(data)?;
        // Show command specific hints
        op.show_command_hint()?;
        Ok(())
    }
}