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 Endpoint command
//!
//! Wraps invoking of the `v3/endpoints/{endpoint_id}` with `PATCH` 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::identity::v3::endpoint::set;
use openstack_types::identity::v3::endpoint::response::set::EndpointResponse;

/// Updates an endpoint.
///
/// Relationship:
/// `https://docs.openstack.org/api/openstack-identity/3/rel/endpoint`
#[derive(Args)]
#[command(about = "Update endpoint")]
pub struct EndpointCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// An `endpoint` object.
    #[command(flatten)]
    endpoint: Endpoint,
}

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

/// Path parameters
#[derive(Args)]
struct PathParameters {
    /// endpoint_id parameter for /v3/endpoints/{endpoint_id} API
    #[arg(
        help_heading = "Path parameters",
        id = "path_param_id",
        value_name = "ID"
    )]
    id: String,
}

#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum Interface {
    Admin,
    Internal,
    Public,
}

/// Endpoint Body data
#[derive(Args, Clone)]
struct Endpoint {
    /// The endpoint description. It is returned only when set on the resource.
    #[arg(help_heading = "Body parameters", long)]
    description: Option<String>,

    /// Set explicit NULL for the description
    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "description")]
    no_description: bool,

    /// Indicates whether the endpoint appears in the service catalog -false.
    /// The endpoint does not appear in the service catalog. -true. The
    /// endpoint appears in the service catalog.
    #[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
    enabled: Option<bool>,

    /// The interface type, which describes the visibility of the endpoint.
    /// Value is: - `public`. Visible by end users on a publicly available
    /// network interface. - `internal`. Visible by end users on an unmetered
    /// internal network interface. - `admin`. Visible by administrative users
    /// on a secure network interface.
    #[arg(help_heading = "Body parameters", long)]
    interface: Option<Interface>,

    /// (Deprecated) The endpoint name. The field will only be returned in
    /// responses when set on the resource.
    ///
    /// This field is deprecated as it provides no value. Endpoints are better
    /// described by the combination of service, region and interface they
    /// describe or by their ID.
    #[arg(help_heading = "Body parameters", long)]
    name: Option<String>,

    /// (Deprecated in v3.2) The geographic location of the service endpoint.
    #[arg(help_heading = "Body parameters", long)]
    region: Option<String>,

    /// Set explicit NULL for the region
    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "region")]
    no_region: bool,

    /// (Since v3.2) The ID of the region that contains the service endpoint.
    #[arg(help_heading = "Body parameters", long)]
    region_id: Option<String>,

    /// Set explicit NULL for the region_id
    #[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "region_id")]
    no_region_id: bool,

    /// The UUID of the service to which the endpoint belongs.
    #[arg(help_heading = "Body parameters", long)]
    service_id: Option<String>,

    /// The endpoint URL.
    #[arg(help_heading = "Body parameters", long)]
    url: Option<String>,
}

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

        let op = OutputProcessor::from_args(parsed_args, Some("identity.endpoint"), Some("set"));
        op.validate_args(parsed_args)?;

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

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

        // Set body parameters
        // Set Request.endpoint data
        let args = &self.endpoint;
        let mut endpoint_builder = set::EndpointBuilder::default();
        if let Some(val) = &args.description {
            endpoint_builder.description(Some(val.into()));
        } else if args.no_description {
            endpoint_builder.description(None);
        }

        if let Some(val) = &args.enabled {
            endpoint_builder.enabled(*val);
        }

        if let Some(val) = &args.interface {
            let tmp = match val {
                Interface::Admin => set::Interface::Admin,
                Interface::Internal => set::Interface::Internal,
                Interface::Public => set::Interface::Public,
            };
            endpoint_builder.interface(tmp);
        }

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

        if let Some(val) = &args.region {
            endpoint_builder.region(Some(val.into()));
        } else if args.no_region {
            endpoint_builder.region(None);
        }

        if let Some(val) = &args.region_id {
            endpoint_builder.region_id(Some(val.into()));
        } else if args.no_region_id {
            endpoint_builder.region_id(None);
        }

        if let Some(val) = &args.service_id {
            endpoint_builder.service_id(val);
        }

        if let Some(val) = &args.url {
            endpoint_builder.url(val);
        }

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