use clap::Args;
use eyre::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::compute::v2::server::volume_attachment::create_20;
use openstack_types::compute::v2::server::volume_attachment::response::create::VolumeAttachmentResponse;
#[derive(Args)]
#[command(about = "Attach a volume to an instance (microversion = 2.0)")]
pub struct VolumeAttachmentCommand {
#[command(flatten)]
query: QueryParameters,
#[command(flatten)]
path: PathParameters,
#[command(flatten)]
volume_attachment: VolumeAttachment,
}
#[derive(Args)]
struct QueryParameters {}
#[derive(Args)]
struct PathParameters {
#[arg(
help_heading = "Path parameters",
id = "path_param_server_id",
value_name = "SERVER_ID"
)]
server_id: String,
}
#[derive(Args, Clone)]
struct VolumeAttachment {
#[arg(help_heading = "Body parameters", long)]
device: Option<String>,
#[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "device")]
no_device: bool,
#[arg(help_heading = "Body parameters", long)]
volume_id: String,
}
impl VolumeAttachmentCommand {
pub async fn take_action(
&self,
parsed_args: &Cli,
client: &mut AsyncOpenStack,
) -> Result<(), OpenStackCliError> {
info!("Create VolumeAttachment");
let op = OutputProcessor::from_args(
parsed_args,
Some("compute.server/volume_attachment"),
Some("create"),
);
op.validate_args(parsed_args)?;
let mut ep_builder = create_20::Request::builder();
ep_builder.header(
http::header::HeaderName::from_static("openstack-api-version"),
http::header::HeaderValue::from_static("compute 2.0"),
);
ep_builder.server_id(&self.path.server_id);
let args = &self.volume_attachment;
let mut volume_attachment_builder = create_20::VolumeAttachmentBuilder::default();
if let Some(val) = &args.device {
volume_attachment_builder.device(Some(val.into()));
} else if args.no_device {
volume_attachment_builder.device(None);
}
volume_attachment_builder.volume_id(&args.volume_id);
ep_builder.volume_attachment(
volume_attachment_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::<VolumeAttachmentResponse>(data)?;
op.show_command_hint()?;
Ok(())
}
}