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 Segment command
//!
//! Wraps invoking of the `v2.0/segments` 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::segment::create;
use openstack_types::network::v2::segment::response::create::SegmentResponse;

/// Creates a segment.
///
/// Normal response codes: 201
///
/// Error response codes: 400, 401
#[derive(Args)]
#[command(about = "Create segment")]
pub struct SegmentCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    #[command(flatten)]
    segment: Segment,
}

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

/// Path parameters
#[derive(Args)]
struct PathParameters {}
/// Segment Body data
#[derive(Args, Clone)]
struct Segment {
    /// A human-readable description for the resource. Default is an empty
    /// string.
    #[arg(help_heading = "Body parameters", long)]
    description: Option<String>,

    /// Human-readable name of the segment.
    #[arg(help_heading = "Body parameters", long)]
    name: Option<String>,

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

    /// The ID of the attached network.
    #[arg(help_heading = "Body parameters", long)]
    network_id: Option<String>,

    /// The type of physical network that maps to this network resource. For
    /// example, `flat`, `vlan`, `vxlan`, or `gre`.
    #[arg(help_heading = "Body parameters", long)]
    network_type: Option<String>,

    /// The physical network where this network/segment is implemented.
    #[arg(help_heading = "Body parameters", long)]
    physical_network: Option<String>,

    /// The ID of the isolated segment on the physical network. The
    /// `network_type` attribute defines the segmentation model. For example,
    /// if the `network_type` value is vlan, this ID is a vlan identifier. If
    /// the `network_type` value is gre, this ID is a gre key. `Note` that only
    /// the segmentation-id of VLAN type networks can be changed!
    #[arg(help_heading = "Body parameters", long)]
    segmentation_id: Option<String>,

    #[arg(help_heading = "Body parameters", long)]
    tenant_id: Option<String>,
}

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

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

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

        // Set body parameters
        // Set Request.segment data
        let args = &self.segment;
        let mut segment_builder = create::SegmentBuilder::default();
        if let Some(val) = &args.description {
            segment_builder.description(val);
        }

        if let Some(val) = &args.name {
            segment_builder.name(Some(val.into()));
        } else if args.no_name {
            segment_builder.name(None);
        }

        if let Some(val) = &args.network_id {
            segment_builder.network_id(val);
        }

        if let Some(val) = &args.network_type {
            segment_builder.network_type(val);
        }

        if let Some(val) = &args.physical_network {
            segment_builder.physical_network(val);
        }

        if let Some(val) = &args.segmentation_id {
            segment_builder.segmentation_id(val);
        }

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

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