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 Interface command [microversion = 2.0]
//!
//! Wraps invoking of the `v2.1/servers/{server_id}/os-interface` with `POST` 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 openstack_sdk::api::QueryAsync;
use openstack_sdk::api::compute::v2::server::interface::create_20;
use openstack_types::compute::v2::server::interface::response::create::InterfaceResponse;

/// Creates a port interface and uses it to attach a port to a server.
///
/// Normal response codes: 200
///
/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
/// itemNotFound(404), conflict(409), computeFault(500), NotImplemented(501)
#[derive(Args)]
#[command(about = "Create Interface (microversion = 2.0)")]
pub struct InterfaceCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// Specify the `interfaceAttachment` action in the request body.
    #[command(flatten)]
    interface_attachment: InterfaceAttachment,
}

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

/// Path parameters
#[derive(Args)]
struct PathParameters {
    /// server_id parameter for /v2.1/servers/{server_id}/os-interface/{id} API
    #[arg(
        help_heading = "Path parameters",
        id = "path_param_server_id",
        value_name = "SERVER_ID"
    )]
    server_id: String,
}
/// InterfaceAttachment Body data
#[derive(Args, Clone)]
struct InterfaceAttachment {
    /// Fixed IP addresses. If you request a specific fixed IP address without
    /// a `net_id`, the request returns a `Bad Request (400)` response code.
    ///
    /// Parameter is an array, may be provided multiple times.
    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
    fixed_ips: Option<Vec<String>>,

    /// The ID of the network for which you want to create a port interface.
    /// The `net_id` and `port_id` parameters are mutually exclusive. If you do
    /// not specify the `net_id` parameter, the OpenStack Networking API v2.0
    /// uses the network information cache that is associated with the
    /// instance.
    #[arg(help_heading = "Body parameters", long)]
    net_id: Option<String>,

    /// The ID of the port for which you want to create an interface. The
    /// `net_id` and `port_id` parameters are mutually exclusive. If you do not
    /// specify the `port_id` parameter, the OpenStack Networking API v2.0
    /// allocates a port and creates an interface for it on the network.
    #[arg(help_heading = "Body parameters", long)]
    port_id: Option<String>,
}

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

        let op = OutputProcessor::from_args(
            parsed_args,
            Some("compute.server/interface"),
            Some("create"),
        );
        op.validate_args(parsed_args)?;

        let mut ep_builder = create_20::Request::builder();
        ep_builder.header(
            http::header::HeaderName::from_static("openstack-api-version"),
            http::header::HeaderValue::from_static("compute 2.0"),
        );

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

        // Set body parameters
        // Set Request.interface_attachment data
        let args = &self.interface_attachment;
        let mut interface_attachment_builder = create_20::InterfaceAttachmentBuilder::default();
        if let Some(val) = &args.fixed_ips {
            let fixed_ips_builder: Vec<create_20::FixedIps> = val
                .iter()
                .flat_map(|v| create_20::FixedIpsBuilder::default().ip_address(v).build())
                .collect();
            interface_attachment_builder.fixed_ips(fixed_ips_builder);
        }

        if let Some(val) = &args.net_id {
            interface_attachment_builder.net_id(val);
        }

        if let Some(val) = &args.port_id {
            interface_attachment_builder.port_id(val);
        }

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