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`.

//! Set Cluster command
//!
//! Wraps invoking of the `v1/clusters/{cluster_id}` with `PATCH` method

use clap::Args;
use eyre::{OptionExt, WrapErr};
use tracing::info;

use openstack_sdk::AsyncOpenStack;

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

use clap::ValueEnum;
use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::container_infrastructure_management::v1::cluster::set;
use openstack_types::container_infrastructure_management::v1::cluster::response::set::ClusterResponse;

/// Update information of one cluster attributes using operations including:
/// `add`, `replace` or `remove`. The attributes to `add` and `replace` in the
/// form of `key=value` while `remove` only needs the keys.
#[derive(Args)]
#[command(about = "Update information of cluster")]
pub struct ClusterCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// The operation used to modify resource’s attributes. Supported
    /// operations are following: `add`, `replace` and `remove`. In case of
    /// `remove`, users only need to provide `path` for deleting attribute.
    #[arg(help_heading = "Body parameters", long)]
    op: Op,

    /// Resource attribute’s name.
    #[arg(help_heading = "Body parameters", long)]
    path: String,

    /// Resource attribute’s value.
    #[arg(help_heading = "Body parameters", long)]
    value: Option<String>,
}

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

/// Path parameters
#[derive(Args)]
struct PathParameters {
    /// cluster_id parameter for /v1/clusters/{cluster_id} API
    #[arg(
        help_heading = "Path parameters",
        id = "path_param_id",
        value_name = "ID"
    )]
    id: String,
}

#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum Op {
    Add,
    Remove,
    Replace,
}

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

        let op = OutputProcessor::from_args(
            parsed_args,
            Some("container-infrastructure-management.cluster"),
            Some("set"),
        );
        op.validate_args(parsed_args)?;

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

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

        // Set body parameters
        // Set Request.op data
        let tmp = match &self.op {
            Op::Add => set::Op::Add,
            Op::Remove => set::Op::Remove,
            Op::Replace => set::Op::Replace,
        };
        ep_builder.op(tmp);

        // Set Request.path data
        ep_builder.path(&self.path);

        // Set Request.value data
        if let Some(arg) = &self.value {
            ep_builder.value(arg);
        }

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

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