pub mod api;
use std::sync::Arc;
use anyhow::Result;
use colored::Colorize;
use serde_json::{json, Value};
use toon_format::encode_default as toon_encode;
use crate::config::OutputFormat;
use crate::execution::{fan_out, report_errors_and_collect_successes, ExecutionTarget};
use crate::render;
use api::{CustomRole, RolesApi, SystemRole};
fn custom_role_to_json(role: &CustomRole, include_profile: bool, profile: &str) -> Value {
let mut v = json!({
"role_id": role.role_id,
"name": role.display_name(),
"description": role.display_description(),
"parent_role_id": role.parent_role_id,
"parent_role_name": role.parent_role_name,
"permissions_count": role.permissions_count(),
"team_id": role.team_id,
});
if include_profile {
if let Value::Object(ref mut m) = v {
m.insert("profile".to_string(), Value::String(profile.to_string()));
}
}
v
}
fn system_role_to_json(role: &SystemRole, include_profile: bool, profile: &str) -> Value {
let mut v = json!({
"role_id": role.role_id,
"name": role.display_name(),
"description": role.display_description(),
"permissions_count": role.permissions_count(),
});
if include_profile {
if let Value::Object(ref mut m) = v {
m.insert("profile".to_string(), Value::String(profile.to_string()));
}
}
v
}
fn read_from_file(path: &str) -> Result<Value> {
let raw = if path == "-" {
eprintln!("{}", "Reading role definition from stdin...".dimmed());
use std::io::Read;
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf)?;
buf
} else {
eprintln!(
"{}",
format!("Reading role definition from {path}...").dimmed()
);
std::fs::read_to_string(path)?
};
Ok(serde_json::from_str(&raw)?)
}
pub async fn run_list(targets: &[Arc<ExecutionTarget>], output: OutputFormat) -> Result<()> {
eprintln!("{}", "Fetching custom roles...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = RolesApi::new(&t.client);
Ok(api.list_custom().await?)
})
.await;
let mut all_json: Vec<Value> = Vec::new();
let mut all_items: Vec<(String, CustomRole)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
for role in resp.roles {
all_json.push(custom_role_to_json(&role, include_profile, &profile));
all_items.push((profile.clone(), role));
}
}
match output {
OutputFormat::Json => render::render_json(&all_json)?,
OutputFormat::Toon => {
let toon =
toon_encode(&all_json).map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {
if all_items.is_empty() {
render::print_no_results("No custom roles found.");
return Ok(());
}
let rows: Vec<Vec<String>> = all_items
.iter()
.map(|(profile, role)| {
vec![
profile.clone(),
role.role_id.clone().unwrap_or_default(),
role.display_name().to_string(),
role.display_description().to_string(),
role.parent_role_name.clone().unwrap_or_default(),
role.permissions_count().to_string(),
]
})
.collect();
render::render_table(
&[
"Role ID",
"Name",
"Description",
"Parent Role",
"Permissions",
],
rows,
include_profile,
);
}
}
Ok(())
}
pub async fn run_get(
targets: &[Arc<ExecutionTarget>],
id: &str,
output: OutputFormat,
) -> Result<()> {
eprintln!("{}", format!("Fetching custom role {id}...").dimmed());
let include_profile = targets.len() > 1;
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = RolesApi::new(&t.client);
Ok(api.get_custom(&id).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
if let Some(role) = resp.role {
all_results.push(custom_role_to_json(&role, include_profile, &profile));
}
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {
render::render_get_text(
&all_results,
include_profile,
"Custom role not found.",
None::<&dyn Fn(&Value)>,
)?;
}
}
Ok(())
}
pub async fn run_create(
targets: &[Arc<ExecutionTarget>],
from_file: &str,
output: OutputFormat,
) -> Result<()> {
let body = read_from_file(from_file)?;
eprintln!("{}", "Creating custom role...".dimmed());
let per_profile = fan_out(targets, |t| {
let body = body.clone();
async move {
let api = RolesApi::new(&t.client);
Ok(api.create(&body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
render::print_created("Created", "custom role", None, resp.id.as_deref(), &profile);
if let Some(id) = resp.id.as_deref() {
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::iam_role_url(b, id)
})
.await;
}
let mut v = json!({ "id": resp.id });
if targets.len() > 1 {
if let Value::Object(ref mut m) = v {
m.insert("profile".to_string(), Value::String(profile.to_string()));
}
}
all_results.push(v);
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {}
}
Ok(())
}
pub async fn run_update(
targets: &[Arc<ExecutionTarget>],
id: &str,
from_file: &str,
output: OutputFormat,
) -> Result<()> {
let body = read_from_file(from_file)?;
eprintln!("{}", format!("Updating custom role {id}...").dimmed());
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let body = body.clone();
let id = id.clone();
async move {
let api = RolesApi::new(&t.client);
Ok(api.update(&id, &body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, val) in report_errors_and_collect_successes(per_profile)? {
eprintln!(
"{}",
format!("Updated custom role in profile '{profile}'.").green()
);
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::iam_role_url(b, &id)
})
.await;
all_results.push(val);
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Toon => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {}
}
Ok(())
}
pub async fn run_delete(targets: &[Arc<ExecutionTarget>], id: &str) -> Result<()> {
eprintln!("{}", format!("Deleting custom role {id}...").dimmed());
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = RolesApi::new(&t.client);
api.delete(&id).await?;
Ok(())
}
})
.await;
for (profile, ()) in report_errors_and_collect_successes(per_profile)? {
eprintln!(
"{}",
format!("Custom role {id} deleted in profile '{profile}'.").green()
);
}
Ok(())
}
pub async fn run_system(targets: &[Arc<ExecutionTarget>], output: OutputFormat) -> Result<()> {
eprintln!("{}", "Fetching system roles...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = RolesApi::new(&t.client);
Ok(api.list_system().await?)
})
.await;
let mut all_json: Vec<Value> = Vec::new();
let mut all_items: Vec<(String, SystemRole)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
for role in resp.roles {
all_json.push(system_role_to_json(&role, include_profile, &profile));
all_items.push((profile.clone(), role));
}
}
match output {
OutputFormat::Json => render::render_json(&all_json)?,
OutputFormat::Toon => {
let toon =
toon_encode(&all_json).map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {
if all_items.is_empty() {
render::print_no_results("No system roles found.");
return Ok(());
}
let rows: Vec<Vec<String>> = all_items
.iter()
.map(|(profile, role)| {
vec![
profile.clone(),
role.role_id.clone().unwrap_or_default(),
role.display_name().to_string(),
role.display_description().to_string(),
role.permissions_count().to_string(),
]
})
.collect();
render::render_table(
&["Role ID", "Name", "Description", "Permissions"],
rows,
include_profile,
);
}
}
Ok(())
}