manta_server/server/handlers/
power.rs1use axum::{Json, extract::Path, http::StatusCode, response::IntoResponse};
17
18use super::{ErrorResponse, RequestCtx, SiteHeader, to_handler_error};
19use crate::service;
20
21pub use manta_shared::types::api::power::{
26 PowerAction, PowerRequest, PowerTargetType,
27};
28
29#[utoipa::path(post, path = "/power", tag = "power",
40 params(SiteHeader),
41 request_body = PowerRequest,
42 security(("bearerAuth" = [])),
43 responses(
44 (status = 200, description = "PCS transition started; returns TransitionStartOutput", body = serde_json::Value),
47 (status = 400, description = "Bad request", body = ErrorResponse),
48 (status = 401, description = "Unauthorized", body = ErrorResponse),
49 (status = 500, description = "Internal error", body = ErrorResponse),
50 )
51)]
52#[tracing::instrument(skip_all)]
53pub async fn post_power(
54 ctx: RequestCtx,
55 Json(body): Json<PowerRequest>,
56) -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> {
57 tracing::info!(
58 "post_power action={:?} target_type={:?}",
59 body.action,
60 body.target_type
61 );
62 let infra = ctx.infra();
63
64 let xnames = service::power::resolve_target_xnames(
65 &infra,
66 &ctx.token,
67 body.target_type,
68 &body.host_expression,
69 )
70 .await
71 .map_err(to_handler_error)?;
72
73 let params = service::power::ApplyPowerParams {
74 action: body.action,
75 xnames,
76 force: body.force,
77 };
78 let result = service::power::apply_power(&infra, &ctx.token, ¶ms)
79 .await
80 .map_err(to_handler_error)?;
81
82 Ok(Json(result))
83}
84
85#[utoipa::path(get, path = "/power/transitions/{id}", tag = "power",
93 params(SiteHeader),
94 security(("bearerAuth" = [])),
95 responses(
96 (status = 200, description = "Transition snapshot", body = serde_json::Value),
99 (status = 401, description = "Unauthorized", body = ErrorResponse),
100 (status = 404, description = "Unknown transition id",body = ErrorResponse),
101 (status = 500, description = "Internal error", body = ErrorResponse),
102 )
103)]
104#[tracing::instrument(skip_all)]
105pub async fn get_power_transition(
106 ctx: RequestCtx,
107 Path(id): Path<String>,
108) -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> {
109 tracing::debug!("get_power_transition id={id}");
110 let infra = ctx.infra();
111 let snapshot = service::power::get_power_transition(&infra, &ctx.token, &id)
112 .await
113 .map_err(to_handler_error)?;
114 Ok(Json(snapshot))
115}