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`.

//! Set Member command
//!
//! Wraps invoking of the `v2/lbaas/pools/{pool_id}/members` with `PUT` 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::find;
use openstack_sdk::api::load_balancer::v2::pool::member::find;
use openstack_sdk::api::load_balancer::v2::pool::member::replace;
use openstack_types::load_balancer::v2::pool::member::response::replace::MemberResponse;
use serde_json::Value;

/// Set the state of members for a pool in one API call. This may include
/// creating new members, deleting old members, and updating existing members.
/// Existing members are matched based on address/port combination.
///
/// For example, assume a pool currently has two members. These members have
/// the following address/port combinations: ‘192.0.2.15:80’ and
/// ‘192.0.2.16:80’. Now assume a PUT request is made that includes members
/// with address/port combinations: ‘192.0.2.16:80’ and ‘192.0.2.17:80’.
///
/// The member ‘192.0.2.15:80’ will be deleted, because it was not in the
/// request.
///
/// The member ‘192.0.2.16:80’ will be updated to match the request data for
/// that member, because it was matched.
///
/// The member ‘192.0.2.17:80’ will be created, because no such member existed.
///
/// The optional parameter `additive_only` when defined as `true` will skip
/// deletions for members missing from the provided list. If this were set in
/// the above example, the member ‘192.0.2.15:80’ would have remained in the
/// pool.
///
/// If the request is valid, the service returns the `Accepted (202)` response
/// code. To confirm the updates, check that the member provisioning statuses
/// are `ACTIVE` for new or updated members, and that any unspecified members
/// were correctly deleted. If the statuses are `PENDING_UPDATE` or
/// `PENDING_DELETE`, use GET to poll the member objects for changes.
#[derive(Args)]
#[command(about = "Batch Update Members")]
pub struct MemberCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// Parameter is an array, may be provided multiple times.
    #[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=crate::common::parse_json)]
    members: Vec<Value>,
}

/// 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,
}

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

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

        let mut find_builder = find::Request::builder();

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

        let find_ep = find_builder
            .build()
            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
        let find_data: serde_json::Value = find(find_ep).query_async(client).await?;

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

        let resource_id = find_data["id"]
            .as_str()
            .ok_or_else(|| eyre::eyre!("resource ID must be a string"))?
            .to_string();
        ep_builder.pool_id(resource_id.clone());

        // Set body parameters
        // Set Request.members data

        let members_builder: Vec<replace::Members> = self
            .members
            .iter()
            .flat_map(|v| serde_json::from_value::<replace::Members>(v.to_owned()))
            .collect::<Vec<replace::Members>>();
        ep_builder.members(members_builder);

        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(())
    }
}