mas_handlers/admin/v1/policy_data/
set.rs1use std::sync::Arc;
6
7use aide::{NoApi, OperationIo, transform::TransformOperation};
8use axum::{Json, extract::State, response::IntoResponse};
9use hyper::StatusCode;
10use mas_policy::PolicyFactory;
11use mas_storage::BoxRng;
12use schemars::JsonSchema;
13use serde::Deserialize;
14
15use crate::{
16 admin::{
17 call_context::CallContext,
18 model::PolicyData,
19 response::{ErrorResponse, SingleResponse},
20 },
21 impl_from_error_for_route,
22};
23
24#[derive(Debug, thiserror::Error, OperationIo)]
25#[aide(output_with = "Json<ErrorResponse>")]
26pub enum RouteError {
27 #[error("Failed to instanciate policy with the provided data")]
28 InvalidPolicyData(#[from] mas_policy::LoadError),
29
30 #[error(transparent)]
31 Internal(Box<dyn std::error::Error + Send + Sync + 'static>),
32}
33
34impl_from_error_for_route!(mas_storage::RepositoryError);
35
36impl IntoResponse for RouteError {
37 fn into_response(self) -> axum::response::Response {
38 let error = ErrorResponse::from_error(&self);
39 let status = match self {
40 RouteError::InvalidPolicyData(_) => StatusCode::BAD_REQUEST,
41 RouteError::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
42 };
43 (status, Json(error)).into_response()
44 }
45}
46
47fn data_example() -> serde_json::Value {
48 serde_json::json!({
49 "hello": "world",
50 "foo": 42,
51 "bar": true
52 })
53}
54
55#[derive(Deserialize, JsonSchema)]
57#[serde(rename = "SetPolicyDataRequest")]
58pub struct SetPolicyDataRequest {
59 #[schemars(example = "data_example")]
60 pub data: serde_json::Value,
61}
62
63pub fn doc(operation: TransformOperation) -> TransformOperation {
64 operation
65 .id("setPolicyData")
66 .summary("Set the current policy data")
67 .tag("policy-data")
68 .response_with::<201, Json<SingleResponse<PolicyData>>, _>(|t| {
69 let [sample, ..] = PolicyData::samples();
70 let response = SingleResponse::new_canonical(sample);
71 t.description("Policy data was successfully set")
72 .example(response)
73 })
74 .response_with::<400, Json<ErrorResponse>, _>(|t| {
75 let error = ErrorResponse::from_error(&RouteError::InvalidPolicyData(
76 mas_policy::LoadError::invalid_data_example(),
77 ));
78 t.description("Invalid policy data").example(error)
79 })
80}
81
82#[tracing::instrument(name = "handler.admin.v1.policy_data.set", skip_all, err)]
83pub async fn handler(
84 CallContext {
85 mut repo, clock, ..
86 }: CallContext,
87 NoApi(mut rng): NoApi<BoxRng>,
88 State(policy_factory): State<Arc<PolicyFactory>>,
89 Json(request): Json<SetPolicyDataRequest>,
90) -> Result<(StatusCode, Json<SingleResponse<PolicyData>>), RouteError> {
91 let policy_data = repo
92 .policy_data()
93 .set(&mut rng, &clock, request.data)
94 .await?;
95
96 policy_factory.set_dynamic_data(policy_data.clone()).await?;
98
99 repo.save().await?;
100
101 Ok((
102 StatusCode::CREATED,
103 Json(SingleResponse::new_canonical(policy_data.into())),
104 ))
105}
106
107#[cfg(test)]
108mod tests {
109 use hyper::{Request, StatusCode};
110 use insta::assert_json_snapshot;
111 use sqlx::PgPool;
112
113 use crate::test_utils::{RequestBuilderExt, ResponseExt, TestState, setup};
114
115 #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
116 async fn test_create(pool: PgPool) {
117 setup();
118 let mut state = TestState::from_pool(pool).await.unwrap();
119 let token = state.token_with_scope("urn:mas:admin").await;
120
121 let request = Request::post("/api/admin/v1/policy-data")
122 .bearer(&token)
123 .json(serde_json::json!({
124 "data": {
125 "hello": "world"
126 }
127 }));
128 let response = state.request(request).await;
129 response.assert_status(StatusCode::CREATED);
130 let body: serde_json::Value = response.json();
131 assert_json_snapshot!(body, @r###"
132 {
133 "data": {
134 "type": "policy-data",
135 "id": "01FSHN9AG0MZAA6S4AF7CTV32E",
136 "attributes": {
137 "created_at": "2022-01-16T14:40:00Z",
138 "data": {
139 "hello": "world"
140 }
141 },
142 "links": {
143 "self": "/api/admin/v1/policy-data/01FSHN9AG0MZAA6S4AF7CTV32E"
144 }
145 },
146 "links": {
147 "self": "/api/admin/v1/policy-data/01FSHN9AG0MZAA6S4AF7CTV32E"
148 }
149 }
150 "###);
151 }
152}