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 EndpointGroup command
//!
//! Wraps invoking of the `v2.0/vpn/endpoint-groups` 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 clap::ValueEnum;
use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::network::v2::vpn::endpoint_group::create;
use openstack_types::network::v2::vpn::endpoint_group::response::create::EndpointGroupResponse;

/// Creates a VPN endpoint group.
///
/// The endpoint group contains one or more endpoints of a specific type that
/// you can use to create a VPN connections.
///
/// Normal response codes: 201
///
/// Error response codes: 400, 401
#[derive(Args)]
#[command(about = "Create VPN endpoint group")]
pub struct EndpointGroupCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    #[command(flatten)]
    endpoint_group: EndpointGroup,
}

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

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

#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum Type {
    Cidr,
    Network,
    Router,
    Subnet,
    Vlan,
}

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

    /// List of endpoints of the same type, for the endpoint group. The values
    /// will depend on type.
    ///
    /// Parameter is an array, may be provided multiple times.
    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
    endpoints: Option<Vec<String>>,

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

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

    /// The type of the endpoints in the group. A valid value is `subnet`,
    /// `cidr`, `network`, `router`, or `vlan`. Only `subnet` and `cidr` are
    /// supported at this moment.
    #[arg(help_heading = "Body parameters", long)]
    _type: Option<Type>,
}

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

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

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

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

        if let Some(val) = &args.endpoints {
            endpoint_group_builder.endpoints(val.iter().map(Into::into).collect::<Vec<_>>());
        }

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

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

        if let Some(val) = &args._type {
            let tmp = match val {
                Type::Cidr => create::Type::Cidr,
                Type::Network => create::Type::Network,
                Type::Router => create::Type::Router,
                Type::Subnet => create::Type::Subnet,
                Type::Vlan => create::Type::Vlan,
            };
            endpoint_group_builder._type(tmp);
        }

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