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 AddressScope command
//!
//! Wraps invoking of the `v2.0/address-scopes` 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::network::v2::address_scope::create;
use openstack_types::network::v2::address_scope::response::create::AddressScopeResponse;

/// Creates an address scope.
///
/// Normal response codes: 201
///
/// Error response codes: 400, 401, 403, 404
#[derive(Args)]
#[command(about = "Create address scope")]
pub struct AddressScopeCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// An `address scope` object.
    #[command(flatten)]
    address_scope: AddressScope,
}

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

/// Path parameters
#[derive(Args)]
struct PathParameters {}
/// AddressScope Body data
#[derive(Args, Clone)]
struct AddressScope {
    /// The IP protocol version. Valid value is `4` or `6`.
    #[arg(help_heading = "Body parameters", long)]
    ip_version: Option<i32>,

    /// Human-readable name of the resource. Default is an empty string.
    #[arg(help_heading = "Body parameters", long)]
    name: Option<String>,

    /// Indicates whether this resource is shared across all projects. By
    /// default, only administrative users can change this value.
    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
    shared: Option<bool>,

    /// The ID of the project that owns the resource. Only administrative and
    /// users with advsvc role can specify a project ID other than their own.
    /// You cannot change this value through authorization policies.
    #[arg(help_heading = "Body parameters", long)]
    tenant_id: Option<String>,
}

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

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

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

        // Set body parameters
        // Set Request.address_scope data
        let args = &self.address_scope;
        let mut address_scope_builder = create::AddressScopeBuilder::default();
        if let Some(val) = &args.ip_version {
            address_scope_builder.ip_version(*val);
        }

        if let Some(val) = &args.name {
            address_scope_builder.name(val);
        }

        if let Some(val) = &args.shared {
            address_scope_builder.shared(*val);
        }

        if let Some(val) = &args.tenant_id {
            address_scope_builder.tenant_id(val);
        }

        ep_builder.address_scope(
            address_scope_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::<AddressScopeResponse>(data)?;
        // Show command specific hints
        op.show_command_hint()?;
        Ok(())
    }
}