use chrono::prelude::*;
use clap::Parser;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::info;
use crate::Cli;
use crate::OpenStackCliError;
use crate::output::{self, OutputProcessor};
use structable::{StructTable, StructTableOptions};
use openstack_sdk::AsyncOpenStack;
use openstack_sdk::types::identity::v3::AuthResponse;
#[derive(Debug, Parser)]
pub struct ShowCommand {}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize, StructTable)]
pub struct Auth {
#[structable(optional, serialize)]
pub roles: Option<Value>,
#[structable(serialize)]
pub user: Value,
#[structable(optional, serialize)]
pub project: Option<Value>,
#[structable(optional, serialize)]
pub domain: Option<Value>,
#[structable(optional, serialize)]
pub system: Option<Value>,
#[structable(optional, serialize)]
pub issued_at: Option<DateTime<Utc>>,
#[structable(serialize)]
pub expires_at: DateTime<Utc>,
}
impl TryFrom<&AuthResponse> for Auth {
type Error = eyre::Report;
fn try_from(value: &AuthResponse) -> Result<Self, Self::Error> {
let roles: Option<Value> = if let Some(val) = &value.token.roles {
Some(serde_json::to_value(val)?)
} else {
None
};
let project: Option<Value> = if let Some(val) = &value.token.project {
Some(serde_json::to_value(val)?)
} else {
None
};
let domain: Option<Value> = if let Some(val) = &value.token.domain {
Some(serde_json::to_value(val)?)
} else {
None
};
let system: Option<Value> = if let Some(val) = &value.token.system {
Some(serde_json::to_value(val)?)
} else {
None
};
Ok(Self {
roles,
user: serde_json::to_value(&value.token.user)?,
project,
domain,
system,
issued_at: value.token.issued_at,
expires_at: value.token.expires_at,
})
}
}
impl ShowCommand {
pub async fn take_action(
&self,
parsed_args: &Cli,
client: &mut AsyncOpenStack,
) -> Result<(), OpenStackCliError> {
info!("Show auth info");
let op = OutputProcessor::from_args(parsed_args, Some("auth"), Some("show"));
if let Some(auth_info) = client.get_auth_info() {
match op.target {
output::OutputFor::Human => {
op.output_human(&Auth::try_from(&auth_info)?)?;
}
_ => {
op.output_machine(serde_json::to_value(auth_info)?)?;
}
}
}
op.show_command_hint()?;
Ok(())
}
}