openstack-cli-network 0.13.7

OpenStack CLI Network commands
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`.

//! Set BandwidthLimitRule command
//!
//! Wraps invoking of the `v2.0/qos/policies/{policy_id}/bandwidth_limit_rules/{id}` with `PUT` method

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

use openstack_cli_core::cli::CliArgs;
use openstack_cli_core::error::OpenStackCliError;
use openstack_cli_core::output::OutputProcessor;
use openstack_sdk::AsyncOpenStack;

use clap::ValueEnum;
use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::network::v2::qos::policy::bandwidth_limit_rule::set;
use openstack_types::network::v2::qos::policy::bandwidth_limit_rule::response;

/// Updates a bandwidth limit rule for a QoS policy.
///
/// Normal response codes: 200
///
/// Error response codes: 400, 401, 404
#[derive(Args)]
#[command(about = "Update bandwidth limit rule")]
pub struct BandwidthLimitRuleCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// A `bandwidth_limit_rule` object.
    #[command(flatten)]
    bandwidth_limit_rule: BandwidthLimitRule,
}

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

/// Path parameters
#[derive(Args)]
struct PathParameters {
    /// id parameter for
    /// /v2.0/qos/policies/{policy_id}/bandwidth_limit_rules/{id} API
    #[arg(
        help_heading = "Path parameters",
        id = "path_param_id",
        value_name = "ID"
    )]
    id: String,

    /// policy_id parameter for
    /// /v2.0/qos/policies/{policy_id}/bandwidth_limit_rules/{id} API
    #[arg(
        help_heading = "Path parameters",
        id = "path_param_policy_id",
        value_name = "POLICY_ID"
    )]
    policy_id: String,
}

#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum Direction {
    Egress,
    Ingress,
}

/// BandwidthLimitRule Body data
#[derive(Args, Clone)]
struct BandwidthLimitRule {
    /// The direction of the traffic to which the QoS rule is applied, as seen
    /// from the point of view of the `port`. Valid values are `egress` and
    /// `ingress`.
    #[arg(help_heading = "Body parameters", long)]
    direction: Option<Direction>,

    /// The maximum burst size (in kilobits). Default is `0`.
    #[arg(help_heading = "Body parameters", long)]
    max_burst_kbps: Option<i32>,

    /// The maximum KBPS (kilobits per second) value. If you specify this
    /// value, must be greater than 0 otherwise max_kbps will have no value.
    #[arg(help_heading = "Body parameters", long)]
    max_kbps: Option<i32>,
}

impl BandwidthLimitRuleCommand {
    /// Perform command action
    pub async fn take_action<C: CliArgs>(
        &self,
        parsed_args: &C,
        client: &mut AsyncOpenStack,
    ) -> Result<(), OpenStackCliError> {
        info!("Set BandwidthLimitRule");

        let op = OutputProcessor::from_args(
            parsed_args,
            Some("network.qos/policy/bandwidth_limit_rule"),
            Some("set"),
        );
        op.validate_args(parsed_args)?;

        let mut ep_builder = set::Request::builder();

        ep_builder.id(&self.path.id);
        ep_builder.policy_id(&self.path.policy_id);

        // Set body parameters
        // Set Request.bandwidth_limit_rule data
        let args = &self.bandwidth_limit_rule;
        let mut bandwidth_limit_rule_builder = set::BandwidthLimitRuleBuilder::default();
        if let Some(val) = &args.direction {
            let tmp = match val {
                Direction::Egress => set::Direction::Egress,
                Direction::Ingress => set::Direction::Ingress,
            };
            bandwidth_limit_rule_builder.direction(tmp);
        }

        if let Some(val) = &args.max_burst_kbps {
            bandwidth_limit_rule_builder.max_burst_kbps(*val);
        }

        if let Some(val) = &args.max_kbps {
            bandwidth_limit_rule_builder.max_kbps(*val);
        }

        ep_builder.bandwidth_limit_rule(
            bandwidth_limit_rule_builder
                .build()
                .wrap_err("error preparing the request data")?,
        );

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

        let data: serde_json::Value = ep.query_async(client).await?;

        op.output_single::<response::set::BandwidthLimitRuleResponse>(data.clone())?;
        // Show command specific hints
        op.show_command_hint()?;
        Ok(())
    }
}