use clap::Args;
use eyre::{OptionExt, WrapErr};
use tracing::info;
use openstack_sdk::AsyncOpenStack;
use crate::Cli;
use crate::OpenStackCliError;
use crate::output::OutputProcessor;
use crate::common::parse_key_val;
use openstack_sdk::api::QueryAsync;
use openstack_sdk::api::identity::v3::os_trust::trust::create;
use openstack_types::identity::v3::os_trust::trust::response::create::TrustResponse;
use serde_json::Value;
#[derive(Args)]
pub struct TrustCommand {
#[command(flatten)]
query: QueryParameters,
#[command(flatten)]
path: PathParameters,
#[command(flatten)]
trust: Trust,
}
#[derive(Args)]
struct QueryParameters {}
#[derive(Args)]
struct PathParameters {}
#[derive(Args, Clone)]
struct Trust {
#[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
allow_redelegation: Option<Option<bool>>,
#[arg(help_heading = "Body parameters", long)]
expires_at: Option<String>,
#[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "expires_at")]
no_expires_at: bool,
#[arg(action=clap::ArgAction::Set, help_heading = "Body parameters", long)]
impersonation: bool,
#[arg(help_heading = "Body parameters", long)]
project_id: Option<String>,
#[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "project_id")]
no_project_id: bool,
#[arg(help_heading = "Body parameters", long)]
redelegated_trust_id: Option<String>,
#[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "redelegated_trust_id")]
no_redelegated_trust_id: bool,
#[arg(help_heading = "Body parameters", long)]
redelegation_count: Option<Option<u32>>,
#[arg(help_heading = "Body parameters", long)]
remaining_uses: Option<Option<i32>>,
#[arg(action=clap::ArgAction::Append, help_heading = "Body parameters", long, value_name="JSON", value_parser=crate::common::parse_json)]
roles: Option<Vec<Value>>,
#[arg(help_heading = "Body parameters", long)]
trustee_user_id: String,
#[arg(help_heading = "Body parameters", long)]
trustor_user_id: String,
}
impl TrustCommand {
pub async fn take_action(
&self,
parsed_args: &Cli,
client: &mut AsyncOpenStack,
) -> Result<(), OpenStackCliError> {
info!("Create Trust");
let op = OutputProcessor::from_args(
parsed_args,
Some("identity.OS_TRUST/trust"),
Some("create"),
);
op.validate_args(parsed_args)?;
let mut ep_builder = create::Request::builder();
let args = &self.trust;
let mut trust_builder = create::TrustBuilder::default();
if let Some(val) = &args.allow_redelegation {
trust_builder.allow_redelegation(*val);
}
if let Some(val) = &args.expires_at {
trust_builder.expires_at(Some(val.into()));
} else if args.no_expires_at {
trust_builder.expires_at(None);
}
trust_builder.impersonation(args.impersonation);
if let Some(val) = &args.project_id {
trust_builder.project_id(Some(val.into()));
} else if args.no_project_id {
trust_builder.project_id(None);
}
if let Some(val) = &args.redelegated_trust_id {
trust_builder.redelegated_trust_id(Some(val.into()));
} else if args.no_redelegated_trust_id {
trust_builder.redelegated_trust_id(None);
}
if let Some(val) = &args.redelegation_count {
trust_builder.redelegation_count(*val);
}
if let Some(val) = &args.remaining_uses {
trust_builder.remaining_uses(*val);
}
if let Some(val) = &args.roles {
let roles_builder: Vec<create::Roles> = val
.iter()
.flat_map(|v| serde_json::from_value::<create::Roles>(v.to_owned()))
.collect::<Vec<create::Roles>>();
trust_builder.roles(roles_builder);
}
trust_builder.trustee_user_id(&args.trustee_user_id);
trust_builder.trustor_user_id(&args.trustor_user_id);
ep_builder.trust(
trust_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::<TrustResponse>(data)?;
op.show_command_hint()?;
Ok(())
}
}