manta_server/server/handlers/
template.rs1use axum::{
4 Json,
5 extract::{Path, Query},
6 http::StatusCode,
7 response::IntoResponse,
8};
9
10use super::{
11 ErrorResponse, RequestCtx, SiteHeader, serialize_or_500, to_handler_error,
12};
13use crate::service;
14
15pub use manta_shared::types::api::queries::TemplateQuery;
20
21#[utoipa::path(get, path = "/templates", tag = "templates",
23 params(TemplateQuery, SiteHeader),
24 security(("bearerAuth" = [])),
25 responses(
26 (status = 200, description = "List of session templates", body = serde_json::Value),
29 (status = 401, description = "Unauthorized", body = ErrorResponse),
30 (status = 500, description = "Internal error", body = ErrorResponse),
31 )
32)]
33#[tracing::instrument(skip_all)]
34pub async fn get_templates(
35 ctx: RequestCtx,
36 Query(q): Query<TemplateQuery>,
37) -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> {
38 let infra = ctx.infra();
39
40 let params = service::template::GetTemplateParams {
41 name: q.name,
42 group_name: q.hsm_group,
43 settings_group_name: None,
44 limit: q.limit,
45 };
46
47 let templates = service::template::get_templates(&infra, &ctx.token, ¶ms)
48 .await
49 .map_err(to_handler_error)?;
50
51 Ok(Json(templates))
52}
53
54pub use manta_shared::types::api::template::{
59 BosOperation, PostTemplateSessionRequest,
60};
61
62#[utoipa::path(post, path = "/templates/{name}/sessions", tag = "templates",
64 params(("name" = String, Path, description = "Template name"), SiteHeader),
65 request_body = PostTemplateSessionRequest,
66 security(("bearerAuth" = [])),
67 responses(
68 (status = 200, description = "Dry run preview", body = serde_json::Value),
70 (status = 201, description = "Session created", body = serde_json::Value),
73 (status = 400, description = "Bad request", body = ErrorResponse),
74 (status = 401, description = "Unauthorized", body = ErrorResponse),
75 (status = 500, description = "Internal error", body = ErrorResponse),
76 )
77)]
78#[tracing::instrument(skip_all)]
79pub async fn post_template_session(
80 ctx: RequestCtx,
81 Path(name): Path<String>,
82 Json(body): Json<PostTemplateSessionRequest>,
83) -> Result<impl IntoResponse, (StatusCode, Json<ErrorResponse>)> {
84 tracing::info!(
85 "post_template_session template={} op={:?} dry_run={}",
86 name,
87 body.operation,
88 body.dry_run
89 );
90 let infra = ctx.infra();
91
92 let params = service::template::ApplyTemplateParams {
93 bos_session_name: body.session_name,
94 bos_sessiontemplate_name: name,
95 bos_session_operation: body.operation.as_str().to_string(),
96 limit: body.limit,
97 include_disabled: body.include_disabled,
98 };
99
100 let (bos_session, _) =
101 service::template::validate_and_prepare_template_session(
102 &infra, &ctx.token, ¶ms,
103 )
104 .await
105 .map_err(to_handler_error)?;
106
107 if body.dry_run {
108 return Ok((StatusCode::OK, Json(serialize_or_500(&bos_session)?)));
109 }
110
111 let created =
112 service::template::create_bos_session(&infra, &ctx.token, bos_session)
113 .await
114 .map_err(to_handler_error)?;
115
116 Ok((StatusCode::CREATED, Json(serialize_or_500(&created)?)))
117}