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::{ContextualDataApi, ContextualDataIntegration};
fn item_to_json(item: &ContextualDataIntegration, include_profile: bool, profile: &str) -> Value {
let mut v = json!({
"id": item.id,
"name": item.name,
"description": item.description,
"deprecated": item.is_deprecated,
"instances": item.amount_integrations,
});
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 contextual data 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 contextual data definition from {path}...").dimmed()
);
std::fs::read_to_string(path)?
};
Ok(serde_json::from_str(&raw)?)
}
fn integration_metadata(body: &Value) -> Result<Value> {
if let Some(metadata) = body.get("metadata") {
return Ok(metadata.clone());
}
if body.get("integrationKey").is_some() {
let mut metadata = body.clone();
if let Some(parameters) = metadata.get("parameters").cloned() {
let Some(object) = metadata.as_object_mut() else {
unreachable!("a JSON value with integrationKey must be an object");
};
object.remove("parameters");
object.insert(
"integrationParameters".to_string(),
json!({ "parameters": parameters }),
);
}
metadata.as_object_mut().map(|o| o.remove("integrationId"));
return Ok(metadata);
}
Err(anyhow::anyhow!(
"contextual data JSON must contain `metadata` or `integrationKey` (with `version` and \
`parameters`)"
))
}
pub async fn run_list(targets: &[Arc<ExecutionTarget>], output: OutputFormat) -> Result<()> {
eprintln!("{}", "Fetching contextual data integrations...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = ContextualDataApi::new(&t.client);
Ok(api.list().await?)
})
.await;
let mut all_json: Vec<Value> = Vec::new();
let mut all_items: Vec<(String, ContextualDataIntegration)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
if !resp.integrations.is_empty() {
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::integrations_url(b)
})
.await;
}
for wrapper in resp.integrations {
let item: ContextualDataIntegration = wrapper.into();
let item_json = item_to_json(&item, include_profile, &profile);
all_json.push(item_json);
all_items.push((profile.clone(), item));
}
}
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 contextual data integrations found.");
return Ok(());
}
let rows: Vec<Vec<String>> = all_items
.iter()
.map(|(profile, item)| {
vec![
profile.clone(),
item.id.clone().unwrap_or_default(),
item.name.clone().unwrap_or_default(),
item.display_description().to_string(),
if item.is_deprecated {
"deprecated".to_string()
} else {
"active".to_string()
},
item.amount_integrations
.map(|n| n.to_string())
.unwrap_or_default(),
]
})
.collect();
render::render_table(
&["ID", "Name", "Description", "Status", "Instances"],
rows,
include_profile,
);
}
}
Ok(())
}
pub async fn run_get(
targets: &[Arc<ExecutionTarget>],
id: &str,
output: OutputFormat,
) -> Result<()> {
eprintln!(
"{}",
format!("Fetching contextual data integration {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 = ContextualDataApi::new(&t.client);
Ok(api.get(&id).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, mut val) in report_errors_and_collect_successes(per_profile)? {
if let Some(inner) = val
.get("integrationDetail")
.and_then(|d| d.get("integration"))
{
let instances = val
.pointer("/integrationDetail/default/registered")
.cloned()
.unwrap_or_else(|| json!([]));
val = json!({
"id": inner.get("id"),
"name": inner.get("name"),
"description": inner.get("description"),
"instances": instances,
});
}
if include_profile {
render::tag_get_result(&mut val, &profile);
}
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::integrations_url(b)
})
.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 => {
render::render_get_text(
&all_results,
include_profile,
"Contextual data integration 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 contextual data integration...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| {
let body = body.clone();
async move {
let api = ContextualDataApi::new(&t.client);
Ok(api.save(&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",
"contextual data integration",
None,
resp.integration_id.as_deref(),
&profile,
);
let mut v = json!({ "id": resp.integration_id });
if include_profile {
if let Value::Object(ref mut m) = v {
m.insert("profile".to_string(), Value::String(profile.to_string()));
}
}
crate::execution::emit_console_link_for_profile(
targets,
&profile,
crate::console_url::integrations_url,
)
.await;
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)?;
let metadata = integration_metadata(&body)?;
eprintln!(
"{}",
format!("Updating contextual data integration {id}...").dimmed()
);
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let metadata = metadata.clone();
let id = id.clone();
async move {
let api = ContextualDataApi::new(&t.client);
Ok(api.update(&id, &metadata).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, val) in report_errors_and_collect_successes(per_profile)? {
eprintln!(
"{}",
format!("Updated contextual data integration {id} in profile '{profile}'.").green()
);
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::integrations_url(b)
})
.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 contextual data integration {id}...").dimmed()
);
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = ContextualDataApi::new(&t.client);
api.delete(&id).await?;
Ok(())
}
})
.await;
for (profile, ()) in report_errors_and_collect_successes(per_profile)? {
eprintln!(
"{}",
format!("Contextual data integration {id} deleted in profile '{profile}'.").green()
);
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::integrations_url(b)
})
.await;
}
Ok(())
}
pub async fn run_definition(
targets: &[Arc<ExecutionTarget>],
id: &str,
output: OutputFormat,
) -> Result<()> {
eprintln!(
"{}",
format!("Fetching contextual data integration definition {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 = ContextualDataApi::new(&t.client);
Ok(api.get_definition(&id).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, mut val) in report_errors_and_collect_successes(per_profile)? {
if include_profile {
render::tag_get_result(&mut val, &profile);
}
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::integrations_url(b)
})
.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 => {
render::render_get_text(
&all_results,
include_profile,
"Contextual data integration definition not found.",
None::<&dyn Fn(&Value)>,
)?;
}
}
Ok(())
}
pub async fn run_test(
targets: &[Arc<ExecutionTarget>],
id: &str,
output: OutputFormat,
) -> Result<()> {
eprintln!(
"{}",
format!("Testing contextual data integration {id}...").dimmed()
);
let id = id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = ContextualDataApi::new(&t.client);
Ok(api.test(&id).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, val) in report_errors_and_collect_successes(per_profile)? {
eprintln!(
"{}",
format!("Test completed in profile '{profile}'.").green()
);
crate::execution::emit_console_link_for_profile(targets, &profile, |b| {
crate::console_url::integrations_url(b)
})
.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 => {
for val in &all_results {
println!("{}", serde_json::to_string_pretty(val)?);
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn integration_metadata_unwraps_api_shape() {
let body = json!({ "metadata": { "integrationKey": "awssns", "version": "0.0.1",
"integrationParameters": { "parameters": [] } } });
assert_eq!(integration_metadata(&body).unwrap(), body["metadata"]);
}
#[test]
fn integration_metadata_normalizes_flat_shape() {
let body = json!({ "integrationId": "abc", "integrationKey": "awssns", "version": "0.0.1",
"parameters": [{ "key": "IntegrationName", "stringValue": "n" }] });
let m = integration_metadata(&body).unwrap();
assert_eq!(m["integrationKey"], "awssns");
assert_eq!(
m["integrationParameters"]["parameters"][0]["key"],
"IntegrationName"
);
assert!(m.get("parameters").is_none());
assert!(m.get("integrationId").is_none());
}
#[test]
fn integration_metadata_rejects_unknown_shape() {
assert!(integration_metadata(&json!({ "name": "x" })).is_err());
}
}