use clap::Args;
use tracing::info;
use openstack_cli_core::cli::CliArgs;
use openstack_cli_core::error::OpenStackCliError;
use openstack_cli_core::output::OutputProcessor;
use openstack_sdk::AsyncOpenStack;
use clap::ValueEnum;
use openstack_cli_core::common::parse_key_val;
use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::image::v2::image::create;
use openstack_types::image::v2::image::response;
use serde_json::Value;
#[derive(Args)]
#[command(about = "Create image")]
pub struct ImageCommand {
#[command(flatten)]
query: QueryParameters,
#[command(flatten)]
path: PathParameters,
#[arg(help_heading = "Body parameters", long)]
container_format: Option<ContainerFormat>,
#[arg(help_heading = "Body parameters", long)]
disk_format: Option<DiskFormat>,
#[arg(help_heading = "Body parameters", long)]
id: Option<String>,
#[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=openstack_cli_core::common::parse_json)]
locations: Option<Vec<Value>>,
#[arg(help_heading = "Body parameters", long)]
min_disk: Option<i32>,
#[arg(help_heading = "Body parameters", long)]
min_ram: Option<i32>,
#[arg(help_heading = "Body parameters", long)]
name: Option<String>,
#[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
os_hidden: Option<bool>,
#[arg(help_heading = "Body parameters", long)]
owner: Option<String>,
#[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
protected: Option<bool>,
#[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long)]
tags: Option<Vec<String>>,
#[arg(help_heading = "Body parameters", long)]
visibility: Option<Visibility>,
#[arg(long="property", value_name="key=value", value_parser=parse_key_val::<String, String>)]
#[arg(help_heading = "Body parameters")]
properties: Option<Vec<(String, String)>>,
}
#[derive(Args)]
struct QueryParameters {}
#[derive(Args)]
struct PathParameters {}
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum ContainerFormat {
Aki,
Ami,
Ari,
Bare,
Compressed,
Docker,
Ova,
Ovf,
}
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum DiskFormat {
Aki,
Ami,
Ari,
Iso,
Ploop,
Qcow2,
Raw,
Vdi,
Vhd,
Vhdx,
Vmdk,
}
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum Visibility {
Community,
Private,
Public,
Shared,
}
impl ImageCommand {
pub async fn take_action<C: CliArgs>(
&self,
parsed_args: &C,
client: &mut AsyncOpenStack,
) -> Result<(), OpenStackCliError> {
info!("Create Image");
let op = OutputProcessor::from_args(parsed_args, Some("image.image"), Some("create"));
op.validate_args(parsed_args)?;
let mut ep_builder = create::Request::builder();
if let Some(arg) = &self.container_format {
let tmp = match arg {
ContainerFormat::Aki => create::ContainerFormat::Aki,
ContainerFormat::Ami => create::ContainerFormat::Ami,
ContainerFormat::Ari => create::ContainerFormat::Ari,
ContainerFormat::Bare => create::ContainerFormat::Bare,
ContainerFormat::Compressed => create::ContainerFormat::Compressed,
ContainerFormat::Docker => create::ContainerFormat::Docker,
ContainerFormat::Ova => create::ContainerFormat::Ova,
ContainerFormat::Ovf => create::ContainerFormat::Ovf,
};
ep_builder.container_format(tmp);
}
if let Some(arg) = &self.disk_format {
let tmp = match arg {
DiskFormat::Aki => create::DiskFormat::Aki,
DiskFormat::Ami => create::DiskFormat::Ami,
DiskFormat::Ari => create::DiskFormat::Ari,
DiskFormat::Iso => create::DiskFormat::Iso,
DiskFormat::Ploop => create::DiskFormat::Ploop,
DiskFormat::Qcow2 => create::DiskFormat::Qcow2,
DiskFormat::Raw => create::DiskFormat::Raw,
DiskFormat::Vdi => create::DiskFormat::Vdi,
DiskFormat::Vhd => create::DiskFormat::Vhd,
DiskFormat::Vhdx => create::DiskFormat::Vhdx,
DiskFormat::Vmdk => create::DiskFormat::Vmdk,
};
ep_builder.disk_format(tmp);
}
if let Some(arg) = &self.id {
ep_builder.id(arg);
}
if let Some(arg) = &self.locations {
let locations_builder: Vec<create::Locations> = arg
.iter()
.flat_map(|v| serde_json::from_value::<create::Locations>(v.to_owned()))
.collect::<Vec<create::Locations>>();
ep_builder.locations(locations_builder);
}
if let Some(arg) = &self.min_disk {
ep_builder.min_disk(*arg);
}
if let Some(arg) = &self.min_ram {
ep_builder.min_ram(*arg);
}
if let Some(arg) = &self.name {
ep_builder.name(Some(arg.into()));
}
if let Some(arg) = &self.os_hidden {
ep_builder.os_hidden(*arg);
}
if let Some(arg) = &self.owner {
ep_builder.owner(Some(arg.into()));
}
if let Some(arg) = &self.protected {
ep_builder.protected(*arg);
}
if let Some(arg) = &self.tags {
ep_builder.tags(arg.iter().map(Into::into).collect::<Vec<_>>());
}
if let Some(arg) = &self.visibility {
let tmp = match arg {
Visibility::Community => create::Visibility::Community,
Visibility::Private => create::Visibility::Private,
Visibility::Public => create::Visibility::Public,
Visibility::Shared => create::Visibility::Shared,
};
ep_builder.visibility(tmp);
}
if let Some(properties) = &self.properties {
ep_builder.properties(properties.iter().cloned());
}
let ep = ep_builder
.build()
.map_err(|x| OpenStackCliError::EndpointBuild(x.to_string()))?;
let data: serde_json::Value = ep.query_async(client).await?;
op.output_single::<response::create::ImageResponse>(data.clone())?;
op.show_command_hint()?;
Ok(())
}
}