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 Member command
//!
//! Wraps invoking of the `v2/lbaas/pools/{pool_id}/members` 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::load_balancer::v2::pool::member::create;
use openstack_types::load_balancer::v2::pool::member::response::create::MemberResponse;

/// This operation provisions a member and adds it to a pool by using the
/// configuration that you define in the request object. After the API
/// validates the request and starts the provisioning process, it returns a
/// response object, which contains a unique ID.
///
/// In the response, the member [provisioning status](#prov-status) is
/// `ACTIVE`, `PENDING_CREATE`, or `ERROR`.
///
/// If the status is `PENDING_CREATE`, issue GET
/// `/v2/lbaas/pools/{pool_id}/members/{member_id}` to view the progress of the
/// provisioning operation. When the member status changes to `ACTIVE`, the
/// member is successfully provisioned and is ready for further configuration.
///
/// If the API cannot fulfill the request due to insufficient data or data that
/// is not valid, the service returns the HTTP `Bad Request (400)` response
/// code with information about the failure in the response body. Validation
/// errors require that you correct the error and submit the request again.
///
/// At a minimum, you must specify these member attributes:
///
/// Some attributes receive default values if you omit them from the request:
///
/// If you omit the `subnet_id` parameter, the `vip_subnet_id` for the parent
/// load balancer will be used for the member subnet UUID.
///
/// The member `address` does not necessarily need to be a member of the
/// `subnet_id` subnet. Members can be routable from the subnet specified
/// either via the default route or by using `host_routes` defined on the
/// subnet.
///
/// Administrative users can specify a project ID that is different than their
/// own to create members for other projects.
///
/// `monitor_address` and/or `monitor_port` can be used to have the health
/// monitor, if one is configured for the pool, connect to an alternate IP
/// address and port when executing a health check on the member.
///
/// To create a member, the load balancer must have an `ACTIVE` provisioning
/// status.
#[derive(Args)]
#[command(about = "Create Member")]
pub struct MemberCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// Defines mandatory and optional attributes of a POST request.
    #[command(flatten)]
    member: Member,
}

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

/// Path parameters
#[derive(Args)]
struct PathParameters {
    /// pool_id parameter for /v2/lbaas/pools/{pool_id}/members/{member_id} API
    #[arg(
        help_heading = "Path parameters",
        id = "path_param_pool_id",
        value_name = "POOL_ID"
    )]
    pool_id: String,
}
/// Member Body data
#[derive(Args, Clone)]
struct Member {
    /// The IP address of the resource.
    #[arg(help_heading = "Body parameters", long)]
    address: String,

    /// The administrative state of the resource, which is up (`true`) or down
    /// (`false`). Default is `true`.
    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
    admin_state_up: Option<bool>,

    /// Is the member a backup? Backup members only receive traffic when all
    /// non-backup members are down.
    ///
    /// **New in version 2.1**
    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
    backup: Option<bool>,

    /// An alternate IP address used for health monitoring a backend member.
    /// Default is `null` which monitors the member `address`.
    #[arg(help_heading = "Body parameters", long)]
    monitor_address: Option<String>,

    /// An alternate protocol port used for health monitoring a backend member.
    /// Default is `null` which monitors the member `protocol_port`.
    #[arg(help_heading = "Body parameters", long)]
    monitor_port: Option<i32>,

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

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

    /// The protocol port number for the resource.
    #[arg(help_heading = "Body parameters", long)]
    protocol_port: i32,

    /// Request that an SR-IOV VF be used for the member network port. Defaults
    /// to `false`.
    ///
    /// **New in version 2.29**
    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
    request_sriov: Option<bool>,

    /// The subnet ID the member service is accessible from.
    #[arg(help_heading = "Body parameters", long)]
    subnet_id: Option<String>,

    /// A list of simple strings assigned to the resource.
    ///
    /// **New in version 2.5**
    ///
    /// Parameter is an array, may be provided multiple times.
    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
    tags: Option<Vec<String>>,

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

    /// The weight of a member determines the portion of requests or
    /// connections it services compared to the other members of the pool. For
    /// example, a member with a weight of 10 receives five times as many
    /// requests as a member with a weight of 2. A value of 0 means the member
    /// does not receive new connections but continues to service existing
    /// connections. A valid value is from `0` to `256`. Default is `1`.
    #[arg(help_heading = "Body parameters", long)]
    weight: Option<i32>,
}

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

        let op = OutputProcessor::from_args(
            parsed_args,
            Some("load-balancer.pool/member"),
            Some("create"),
        );
        op.validate_args(parsed_args)?;

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

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

        // Set body parameters
        // Set Request.member data
        let args = &self.member;
        let mut member_builder = create::MemberBuilder::default();

        member_builder.address(&args.address);

        if let Some(val) = &args.admin_state_up {
            member_builder.admin_state_up(*val);
        }

        if let Some(val) = &args.backup {
            member_builder.backup(*val);
        }

        if let Some(val) = &args.monitor_address {
            member_builder.monitor_address(val);
        }

        if let Some(val) = &args.monitor_port {
            member_builder.monitor_port(*val);
        }

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

        if let Some(val) = &args.project_id {
            member_builder.project_id(val);
        }

        member_builder.protocol_port(args.protocol_port);

        if let Some(val) = &args.request_sriov {
            member_builder.request_sriov(*val);
        }

        if let Some(val) = &args.subnet_id {
            member_builder.subnet_id(val);
        }

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

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

        if let Some(val) = &args.weight {
            member_builder.weight(*val);
        }

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