use aion_core::ClusterCommand;
use aion_proto::WireError;
use axum::{
Json,
extract::State,
response::{IntoResponse, Response},
};
use super::auth::HttpCaller;
use super::error::HttpWireError;
use crate::ServerState;
use crate::namespace::CallerIdentity;
pub(crate) async fn cluster_command(
State(state): State<ServerState>,
HttpCaller(caller): HttpCaller,
Json(command): Json<ClusterCommand>,
) -> Result<Response, HttpWireError> {
deploy_gate(&caller)?;
match command {
ClusterCommand::RequestClusterSnapshot {} => {
let snapshot = crate::stream::cluster_stream::build_snapshot(&state, &caller)
.map_err(|error| HttpWireError(error.to_wire_error()))?;
Ok(Json(snapshot).into_response())
}
ClusterCommand::CancelWorkflow { .. }
| ClusterCommand::ReopenWorkflow { .. }
| ClusterCommand::RedriveOutboxRow { .. }
| ClusterCommand::DrainNode { .. }
| ClusterCommand::PlannedHandoff { .. }
| ClusterCommand::ChaosKillNode { .. } => Err(HttpWireError(WireError::backend_with_type(
"Unimplemented",
"this cluster command is part of the ADR-020 seam but is not implemented in Phase 1",
))),
}
}
fn deploy_gate(caller: &CallerIdentity) -> Result<(), HttpWireError> {
if caller.deploy_granted() {
Ok(())
} else {
Err(HttpWireError(WireError::deploy_denied(
"cluster commands require the deployment-wide deploy grant",
)))
}
}