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 RemoteConsole command [microversion = 2.99]
//!
//! Wraps invoking of the `v2.1/servers/{server_id}/remote-consoles` 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 clap::ValueEnum;
use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::compute::v2::server::remote_console::create_299;
use openstack_types::compute::v2::server::remote_console::response::create::RemoteConsoleResponse;

/// The API provides a unified request for creating a remote console. The user
/// can get a URL to connect the console from this API. The URL includes the
/// token which is used to get permission to access the console. Servers may
/// support different console protocols. To return a remote console using a
/// specific protocol, such as VNC, set the `protocol` parameter to `vnc`.
///
/// Normal response codes: 200
///
/// Error response codes: badRequest(400), unauthorized(401), forbidden(403),
/// itemNotFound(404), conflict(409), notImplemented(501)
#[derive(Args)]
#[command(about = "Create Console (microversion = 2.99)")]
pub struct RemoteConsoleCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// The remote console object.
    #[command(flatten)]
    remote_console: RemoteConsole,
}

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

/// Path parameters
#[derive(Args)]
struct PathParameters {
    /// server_id parameter for /v2.1/servers/{server_id}/remote-consoles API
    #[arg(
        help_heading = "Path parameters",
        id = "path_param_server_id",
        value_name = "SERVER_ID"
    )]
    server_id: String,
}

#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum Protocol {
    Mks,
    Rdp,
    Serial,
    Spice,
    Vnc,
}

#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum Type {
    Novnc,
    RdpHtml5,
    Serial,
    SpiceDirect,
    SpiceHtml5,
    Webmks,
    Xvpvnc,
}

/// RemoteConsole Body data
#[derive(Args, Clone)]
struct RemoteConsole {
    /// The protocol of remote console. The valid values are `vnc`, `spice`,
    /// `rdp`, `serial` and `mks`. The protocol `mks` is added since
    /// Microversion `2.8`. The protocol `rdp` requires the Hyper-V driver
    /// which was removed in the 29.0.0 (Caracal) release.
    #[arg(help_heading = "Body parameters", long)]
    protocol: Protocol,

    /// The type of remote console. The valid values are `novnc`, `rdp-html5`,
    /// `spice-html5`, `spice-direct`, `serial`, and `webmks`. The type
    /// `webmks` was added in Microversion `2.8` and the type `spice-direct`
    /// was added in Microversion `2.99`. The type `rdp-html5` requires the
    /// Hyper-V driver which was removed in the 29.0.0 (Caracal) release.
    #[arg(help_heading = "Body parameters", long)]
    _type: Type,
}

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

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

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

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

        // Set body parameters
        // Set Request.remote_console data
        let args = &self.remote_console;
        let mut remote_console_builder = create_299::RemoteConsoleBuilder::default();

        let tmp = match &args.protocol {
            Protocol::Mks => create_299::Protocol::Mks,
            Protocol::Rdp => create_299::Protocol::Rdp,
            Protocol::Serial => create_299::Protocol::Serial,
            Protocol::Spice => create_299::Protocol::Spice,
            Protocol::Vnc => create_299::Protocol::Vnc,
        };
        remote_console_builder.protocol(tmp);

        let tmp = match &args._type {
            Type::Novnc => create_299::Type::Novnc,
            Type::RdpHtml5 => create_299::Type::RdpHtml5,
            Type::Serial => create_299::Type::Serial,
            Type::SpiceDirect => create_299::Type::SpiceDirect,
            Type::SpiceHtml5 => create_299::Type::SpiceHtml5,
            Type::Webmks => create_299::Type::Webmks,
            Type::Xvpvnc => create_299::Type::Xvpvnc,
        };
        remote_console_builder._type(tmp);

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