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