use clap::Args;
use eyre::WrapErr;
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 openstack_sdk::api::QueryAsync;
use openstack_sdk::api::network::v2::local_ip::port_association::create;
use openstack_types::network::v2::local_ip::port_association::response;
#[derive(Args)]
#[command(about = "Create Local IP Association")]
pub struct PortAssociationCommand {
#[command(flatten)]
query: QueryParameters,
#[command(flatten)]
path: PathParameters,
#[command(flatten)]
port_association: PortAssociation,
}
#[derive(Args)]
struct QueryParameters {}
#[derive(Args)]
struct PathParameters {
#[arg(
help_heading = "Path parameters",
id = "path_param_local_ip_id",
value_name = "LOCAL_IP_ID"
)]
local_ip_id: String,
}
#[derive(Args, Clone)]
struct PortAssociation {
#[arg(help_heading = "Body parameters", long)]
fixed_ip: Option<String>,
#[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "fixed_ip")]
no_fixed_ip: bool,
#[arg(help_heading = "Body parameters", long)]
fixed_port_id: Option<String>,
#[arg(help_heading = "Body parameters", long)]
project_id: Option<String>,
}
impl PortAssociationCommand {
pub async fn take_action<C: CliArgs>(
&self,
parsed_args: &C,
client: &mut AsyncOpenStack,
) -> Result<(), OpenStackCliError> {
info!("Create PortAssociation");
let op = OutputProcessor::from_args(
parsed_args,
Some("network.local_ip/port_association"),
Some("create"),
);
op.validate_args(parsed_args)?;
let mut ep_builder = create::Request::builder();
ep_builder.local_ip_id(&self.path.local_ip_id);
let args = &self.port_association;
let mut port_association_builder = create::PortAssociationBuilder::default();
if let Some(val) = &args.fixed_ip {
port_association_builder.fixed_ip(Some(val.into()));
} else if args.no_fixed_ip {
port_association_builder.fixed_ip(None);
}
if let Some(val) = &args.fixed_port_id {
port_association_builder.fixed_port_id(val);
}
if let Some(val) = &args.project_id {
port_association_builder.project_id(val);
}
ep_builder.port_association(
port_association_builder
.build()
.wrap_err("error preparing the request data")?,
);
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::PortAssociationResponse>(data.clone())?;
op.show_command_hint()?;
Ok(())
}
}