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 Reshaper command [microversion = 1.34]
//!
//! Wraps invoking of the `reshaper` with `POST` method

use clap::Args;
use tracing::info;

use openstack_sdk::AsyncOpenStack;

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

use crate::common::parse_key_val;
use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::placement::v1::reshaper::create_134;
use serde_json::Value;

/// Atomically migrate resource provider inventories and associated
/// allocations. This is used when some of the inventory needs to move from one
/// resource provider to another, such as when a class of inventory moves from
/// a parent provider to a new child provider.
///
/// Normal Response Codes: 204
///
/// Error Response Codes: badRequest(400), conflict(409)
#[derive(Args)]
#[command(about = "Reshaper (microversion = 1.34)")]
pub struct ReshaperCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// A dictionary of multiple allocations, keyed by consumer uuid. Each
    /// collection of allocations describes the full set of allocations for
    /// each consumer. Each consumer allocations dict is itself a dictionary of
    /// resource allocations keyed by resource provider uuid. An empty
    /// dictionary indicates no change in existing allocations, whereas an
    /// empty `allocations` dictionary **within** a consumer dictionary
    /// indicates that all allocations for that consumer should be deleted.
    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, Value>)]
    allocations: Vec<(String, Value)>,

    /// A dictionary of multiple inventories, keyed by resource provider uuid.
    /// Each inventory describes the desired full inventory for each resource
    /// provider. An empty dictionary causes the inventory for that provider to
    /// be deleted.
    #[arg(help_heading = "Body parameters", long, value_name="key=value", value_parser=parse_key_val::<String, Value>)]
    inventories: Vec<(String, Value)>,
}

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

/// Path parameters
#[derive(Args)]
struct PathParameters {}

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

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

        let mut ep_builder = create_134::Request::builder();
        ep_builder.header(
            http::header::HeaderName::from_static("openstack-api-version"),
            http::header::HeaderValue::from_static("placement 1.34"),
        );

        // Set body parameters
        // Set Request.allocations data

        ep_builder.allocations(
            self.allocations
                .iter()
                .map(|(k, v)| {
                    serde_json::from_value(v.to_owned())
                        .map(|v: create_134::AllocationsItemStruct| (k, v))
                })
                .collect::<Result<Vec<_>, _>>()?
                .into_iter(),
        );

        // Set Request.inventories data

        ep_builder.inventories(
            self.inventories
                .iter()
                .map(|(k, v)| {
                    serde_json::from_value(v.to_owned())
                        .map(|v: create_134::InventoriesItemStruct| (k, v))
                })
                .collect::<Result<Vec<_>, _>>()?
                .into_iter(),
        );

        let ep = ep_builder
            .build()
            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
        openstack_sdk::api::ignore(ep).query_async(client).await?;
        // Show command specific hints
        op.show_command_hint()?;
        Ok(())
    }
}