pub mod arm_client;
pub mod diff;
use anyhow::Result;
use crate::config::schema::CosmosConfig;
pub use arm_client::ArmCosmosClient;
pub use diff::{ContainerDiff, ContainerSpec, desired_containers, diff_containers};
pub struct CosmosPlan {
pub diffs: Vec<ContainerDiff>,
}
pub async fn plan(client: &ArmCosmosClient, cosmos: &CosmosConfig) -> Result<CosmosPlan> {
let want = desired_containers(cosmos);
let have = client.list_containers(&cosmos.database).await?;
Ok(CosmosPlan {
diffs: diff_containers(&want, &have),
})
}
pub async fn apply(client: &ArmCosmosClient, cosmos: &CosmosConfig) -> Result<()> {
client.ensure_database(&cosmos.database).await?;
let plan = plan(client, cosmos).await?;
for d in plan.diffs {
match d {
ContainerDiff::Match { .. } => continue,
ContainerDiff::Create(spec) => {
client.create_container(&cosmos.database, &spec).await?;
}
ContainerDiff::PartitionKeyMismatch { name, want, have } => {
anyhow::bail!(
"container '{name}' exists with partition key '{have}', want '{want}'. \
Quelch will not auto-recreate (would lose data). Resolve manually then retry."
);
}
}
}
Ok(())
}