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::network::v2::default_security_group_rule::create;
use openstack_types::network::v2::default_security_group_rule::response::create::DefaultSecurityGroupRuleResponse;
#[derive(Args)]
#[command(about = "Create security group default rule")]
pub struct DefaultSecurityGroupRuleCommand {
#[command(flatten)]
query: QueryParameters,
#[command(flatten)]
path: PathParameters,
#[command(flatten)]
default_security_group_rule: DefaultSecurityGroupRule,
}
#[derive(Args)]
struct QueryParameters {}
#[derive(Args)]
struct PathParameters {}
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum Direction {
Egress,
Ingress,
}
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, ValueEnum)]
enum Ethertype {
Ipv4,
Ipv6,
}
#[derive(Args, Clone)]
struct DefaultSecurityGroupRule {
#[arg(help_heading = "Body parameters", long)]
description: Option<String>,
#[arg(help_heading = "Body parameters", long)]
direction: Option<Direction>,
#[arg(help_heading = "Body parameters", long)]
ethertype: Option<Ethertype>,
#[arg(help_heading = "Body parameters", long)]
port_range_max: Option<Option<i32>>,
#[arg(help_heading = "Body parameters", long)]
port_range_min: Option<Option<i32>>,
#[arg(help_heading = "Body parameters", long)]
protocol: Option<String>,
#[arg(help_heading = "Body parameters", long)]
remote_address_group_id: Option<String>,
#[arg(help_heading = "Body parameters", long)]
remote_group_id: Option<String>,
#[arg(help_heading = "Body parameters", long)]
remote_ip_prefix: Option<String>,
#[arg(help_heading = "Body parameters", long)]
tenant_id: Option<String>,
#[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
used_in_default_sg: Option<bool>,
#[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
used_in_non_default_sg: Option<bool>,
}
impl DefaultSecurityGroupRuleCommand {
pub async fn take_action(
&self,
parsed_args: &Cli,
client: &mut AsyncOpenStack,
) -> Result<(), OpenStackCliError> {
info!("Create DefaultSecurityGroupRule");
let op = OutputProcessor::from_args(
parsed_args,
Some("network.default_security_group_rule"),
Some("create"),
);
op.validate_args(parsed_args)?;
let mut ep_builder = create::Request::builder();
let args = &self.default_security_group_rule;
let mut default_security_group_rule_builder =
create::DefaultSecurityGroupRuleBuilder::default();
if let Some(val) = &args.description {
default_security_group_rule_builder.description(val);
}
if let Some(val) = &args.direction {
let tmp = match val {
Direction::Egress => create::Direction::Egress,
Direction::Ingress => create::Direction::Ingress,
};
default_security_group_rule_builder.direction(tmp);
}
if let Some(val) = &args.ethertype {
let tmp = match val {
Ethertype::Ipv4 => create::Ethertype::Ipv4,
Ethertype::Ipv6 => create::Ethertype::Ipv6,
};
default_security_group_rule_builder.ethertype(tmp);
}
if let Some(val) = &args.port_range_max {
default_security_group_rule_builder.port_range_max(*val);
}
if let Some(val) = &args.port_range_min {
default_security_group_rule_builder.port_range_min(*val);
}
if let Some(val) = &args.protocol {
default_security_group_rule_builder.protocol(val);
}
if let Some(val) = &args.remote_address_group_id {
default_security_group_rule_builder.remote_address_group_id(val);
}
if let Some(val) = &args.remote_group_id {
default_security_group_rule_builder.remote_group_id(val);
}
if let Some(val) = &args.remote_ip_prefix {
default_security_group_rule_builder.remote_ip_prefix(val);
}
if let Some(val) = &args.tenant_id {
default_security_group_rule_builder.tenant_id(val);
}
if let Some(val) = &args.used_in_default_sg {
default_security_group_rule_builder.used_in_default_sg(*val);
}
if let Some(val) = &args.used_in_non_default_sg {
default_security_group_rule_builder.used_in_non_default_sg(*val);
}
ep_builder.default_security_group_rule(
default_security_group_rule_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::<DefaultSecurityGroupRuleResponse>(data)?;
op.show_command_hint()?;
Ok(())
}
}