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

//! List Loadbalancers command
//!
//! Wraps invoking of the `v2/lbaas/loadbalancers` with `GET` method

use clap::Args;
use eyre::OptionExt;
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_by_name;
use openstack_sdk::api::identity::v3::project::find as find_project;
use openstack_sdk::api::load_balancer::v2::loadbalancer::list;
use openstack_sdk::api::{Pagination, paged};
use openstack_types::load_balancer::v2::loadbalancer::response::list::LoadbalancerResponse;
use tracing::warn;

/// Lists all load balancers for the project.
///
/// Use the `fields` query parameter to control which fields are returned in
/// the response body. Additionally, you can filter results by using query
/// string parameters. For information, see
/// [Filtering and column selection](#filtering).
///
/// Administrative users can specify a project ID that is different than their
/// own to list load balancers for other projects.
///
/// The list might be empty.
#[derive(Args)]
#[command(about = "List Load Balancers")]
pub struct LoadbalancersCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// Total limit of entities count to return. Use this when there are too many entries.
    #[arg(long, default_value_t = 10000)]
    max_items: usize,
}

/// Query parameters
#[derive(Args)]
struct QueryParameters {
    /// An availability zone name.
    #[arg(help_heading = "Query parameters", long)]
    availability_zone: Option<String>,

    /// The UTC date and timestamp when the resource was created.
    #[arg(help_heading = "Query parameters", long)]
    created_at: Option<String>,

    /// A human-readable description for the resource.
    #[arg(help_heading = "Query parameters", long)]
    description: Option<String>,

    /// The ID of the flavor.
    #[arg(help_heading = "Query parameters", long)]
    flavor_id: Option<String>,

    /// The ID of the resource
    #[arg(help_heading = "Query parameters", long)]
    id: Option<String>,

    /// Page size
    #[arg(
        help_heading = "Query parameters",
        long("page-size"),
        visible_alias("limit")
    )]
    limit: Option<i32>,

    /// ID of the last item in the previous list
    #[arg(help_heading = "Query parameters", long)]
    marker: Option<String>,

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

    /// Return the list of entities that do not have one or more of the given
    /// tags.
    #[arg(help_heading = "Query parameters", long)]
    not_tags: Option<String>,

    /// Return the list of entities that do not have at least one of the given
    /// tags.
    #[arg(help_heading = "Query parameters", long)]
    not_tags_any: Option<String>,

    /// The operating status of the resource.
    #[arg(help_heading = "Query parameters", long, value_parser = ["DEGRADED","DRAINING","ERROR","NO_MONITOR","OFFLINE","ONLINE"])]
    operating_status: Option<String>,

    /// The page direction.
    #[arg(action=clap::ArgAction::Set, help_heading = "Query parameters", long)]
    page_reverse: Option<bool>,

    /// Project resource for which the operation should be performed.
    #[command(flatten)]
    project: ProjectInput,

    /// Provider name for the load balancer.
    #[arg(help_heading = "Query parameters", long)]
    provider: Option<String>,

    /// The provisioning status of the resource.
    #[arg(help_heading = "Query parameters", long, value_parser = ["ACTIVE","DELETED","ERROR","PENDING_CREATE","PENDING_DELETE","PENDING_UPDATE"])]
    provisioning_status: Option<String>,

    /// Return the list of entities that have this tag or tags.
    #[arg(help_heading = "Query parameters", long)]
    tags: Option<String>,

    /// Return the list of entities that have one or more of the given tags.
    #[arg(help_heading = "Query parameters", long)]
    tags_any: Option<String>,

    /// The UTC date and timestamp when the resource was last updated.
    #[arg(help_heading = "Query parameters", long)]
    updated_at: Option<String>,

    /// The IP address of the Virtual IP (VIP).
    #[arg(help_heading = "Query parameters", long)]
    vip_address: Option<String>,

    /// The ID of the network for the Virtual IP (VIP).
    #[arg(help_heading = "Query parameters", long)]
    vip_network_id: Option<String>,

    /// The ID of the Virtual IP (VIP) port.
    #[arg(help_heading = "Query parameters", long)]
    vip_port_id: Option<String>,

    /// The ID of the QoS Policy which will apply to the Virtual IP (VIP).
    #[arg(help_heading = "Query parameters", long)]
    vip_qos_policy_id: Option<String>,

    /// The ID of the subnet for the Virtual IP (VIP).
    #[arg(help_heading = "Query parameters", long)]
    vip_subnet_id: Option<String>,
}

/// Project input select group
#[derive(Args)]
#[group(required = false, multiple = false)]
struct ProjectInput {
    /// Project Name.
    #[arg(long, help_heading = "Path parameters", value_name = "PROJECT_NAME")]
    project_name: Option<String>,
    /// Project ID.
    #[arg(long, help_heading = "Path parameters", value_name = "PROJECT_ID")]
    project_id: Option<String>,
    /// Current project.
    #[arg(long, help_heading = "Path parameters", action = clap::ArgAction::SetTrue)]
    current_project: bool,
}

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

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

        let op = OutputProcessor::from_args(
            parsed_args,
            Some("load-balancer.loadbalancer"),
            Some("list"),
        );
        op.validate_args(parsed_args)?;

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

        // Set query parameters
        if let Some(val) = &self.query.availability_zone {
            ep_builder.availability_zone(val);
        }
        if let Some(val) = &self.query.created_at {
            ep_builder.created_at(val);
        }
        if let Some(val) = &self.query.description {
            ep_builder.description(val);
        }
        if let Some(val) = &self.query.flavor_id {
            ep_builder.flavor_id(val);
        }
        if let Some(val) = &self.query.id {
            ep_builder.id(val);
        }
        if let Some(val) = &self.query.limit {
            ep_builder.limit(*val);
        }
        if let Some(val) = &self.query.marker {
            ep_builder.marker(val);
        }
        if let Some(val) = &self.query.name {
            ep_builder.name(val);
        }
        if let Some(val) = &self.query.not_tags {
            ep_builder.not_tags(val);
        }
        if let Some(val) = &self.query.not_tags_any {
            ep_builder.not_tags_any(val);
        }
        if let Some(val) = &self.query.operating_status {
            ep_builder.operating_status(val);
        }
        if let Some(val) = &self.query.page_reverse {
            ep_builder.page_reverse(*val);
        }
        if let Some(id) = &self.query.project.project_id {
            // project_id is passed. No need to lookup
            ep_builder.project_id(id);
        } else if let Some(name) = &self.query.project.project_name {
            // project_name is passed. Need to lookup resource
            let mut sub_find_builder = find_project::Request::builder();
            warn!(
                "Querying project by name (because of `--project-name` parameter passed) may not be definite. This may fail in which case parameter `--project-id` should be used instead."
            );

            sub_find_builder.id(name);
            let find_ep = sub_find_builder
                .build()
                .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
            let find_data: serde_json::Value = find_by_name(find_ep).query_async(client).await?;
            // Try to extract resource id
            match find_data.get("id") {
                Some(val) => match val.as_str() {
                    Some(id_str) => {
                        ep_builder.project_id(id_str.to_owned());
                    }
                    None => {
                        return Err(OpenStackCliError::ResourceAttributeNotString(
                            serde_json::to_string(&val)?,
                        ));
                    }
                },
                None => {
                    return Err(OpenStackCliError::ResourceAttributeMissing(
                        "id".to_string(),
                    ));
                }
            };
        } else if self.query.project.current_project {
            ep_builder.project_id(
                client
                    .get_auth_info()
                    .ok_or_eyre("Cannot determine current authentication information")?
                    .token
                    .user
                    .id,
            );
        }
        if let Some(val) = &self.query.provider {
            ep_builder.provider(val);
        }
        if let Some(val) = &self.query.provisioning_status {
            ep_builder.provisioning_status(val);
        }
        if let Some(val) = &self.query.tags {
            ep_builder.tags(val);
        }
        if let Some(val) = &self.query.tags_any {
            ep_builder.tags_any(val);
        }
        if let Some(val) = &self.query.updated_at {
            ep_builder.updated_at(val);
        }
        if let Some(val) = &self.query.vip_address {
            ep_builder.vip_address(val);
        }
        if let Some(val) = &self.query.vip_network_id {
            ep_builder.vip_network_id(val);
        }
        if let Some(val) = &self.query.vip_port_id {
            ep_builder.vip_port_id(val);
        }
        if let Some(val) = &self.query.vip_qos_policy_id {
            ep_builder.vip_qos_policy_id(val);
        }
        if let Some(val) = &self.query.vip_subnet_id {
            ep_builder.vip_subnet_id(val);
        }

        let ep = ep_builder
            .build()
            .map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;

        let data: Vec<serde_json::Value> = paged(ep, Pagination::Limit(self.max_items))
            .query_async(client)
            .await?;
        op.output_list::<LoadbalancerResponse>(data)?;
        // Show command specific hints
        op.show_command_hint()?;
        Ok(())
    }
}