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 AssistedVolumeSnapshot command
//!
//! Wraps invoking of the `v2.1/os-assisted-volume-snapshots` 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 clap::ValueEnum;
use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::compute::v2::assisted_volume_snapshot::create;
use openstack_types::compute::v2::assisted_volume_snapshot::response::create::AssistedVolumeSnapshotResponse;

/// Creates an assisted volume snapshot.
///
/// Normal response codes: 200
///
/// Error response codes: badRequest(400),unauthorized(401), forbidden(403)
#[derive(Args)]
#[command(about = "Create Assisted Volume Snapshots")]
pub struct AssistedVolumeSnapshotCommand {
    /// Request Query parameters
    #[command(flatten)]
    query: QueryParameters,

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

    /// A partial representation of a snapshot that is used to create a
    /// snapshot.
    #[command(flatten)]
    snapshot: Snapshot,
}

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

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

#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum Type {
    Qcow2,
}

/// CreateInfo Body data
#[derive(Args, Clone)]
#[group(required = true, multiple = true)]
struct CreateInfo {
    /// Its an arbitrary string that gets passed back to the user.
    #[arg(help_heading = "Body parameters", long)]
    id: Option<String>,

    /// The name of the qcow2 file that Block Storage creates, which becomes
    /// the active image for the VM.
    #[arg(help_heading = "Body parameters", long, required = false)]
    new_file: String,

    /// The UUID for a snapshot.
    #[arg(help_heading = "Body parameters", long, required = false)]
    snapshot_id: String,

    /// The snapshot type. A valid value is `qcow2`.
    #[arg(help_heading = "Body parameters", long, required = false)]
    _type: Type,
}

/// Snapshot Body data
#[derive(Args, Clone)]
struct Snapshot {
    /// Information for snapshot creation.
    #[command(flatten)]
    create_info: CreateInfo,

    /// The source volume ID.
    #[arg(help_heading = "Body parameters", long)]
    volume_id: String,
}

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

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

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

        // Set body parameters
        // Set Request.snapshot data
        let args = &self.snapshot;
        let mut snapshot_builder = create::SnapshotBuilder::default();

        let mut create_info_builder = create::CreateInfoBuilder::default();
        if let Some(val) = &&args.create_info.id {
            create_info_builder.id(val);
        }

        create_info_builder.new_file(&args.create_info.new_file);

        create_info_builder.snapshot_id(&args.create_info.snapshot_id);

        let tmp = match &&args.create_info._type {
            Type::Qcow2 => create::Type::Qcow2,
        };
        create_info_builder._type(tmp);
        snapshot_builder.create_info(
            create_info_builder
                .build()
                .wrap_err("error preparing the request data")?,
        );

        snapshot_builder.volume_id(&args.volume_id);

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