Skip to main content

fiberplane_models/
pagerduty.rs

1//! PagerDuty related models.
2//!
3//! A PagerDuty receiver is a resource that is defined in Fiberplane. It works
4//! unison with a PagerDuty webhook, which is defined in PagerDuty. A PagerDuty
5//! webhook will have its target URL set to the receiver.
6//!
7//! Once a incident is created in PagerDuty, PagerDuty will send a webhook to a
8//! webhook receiver. Depending on the configuration of the receiver this will
9//! create a new notebook based on a template, it can also update any
10//! front-matter values defined in any notebook.
11//!
12//! A shared security-key should be set in the PagerDuty webhook customer header
13//! which will verified by Fiberplane. If the security-key does not match, the
14//! request will be dropped. The header should be <..> and contain the
15//! security-key as-is.
16
17use crate::auth::AuthError;
18use crate::names::Name;
19use crate::sorting::SortField;
20use crate::timestamps::Timestamp;
21use serde::{Deserialize, Serialize};
22use strum_macros::IntoStaticStr;
23use thiserror::Error;
24use typed_builder::TypedBuilder;
25
26#[cfg(feature = "fp-bindgen")]
27use fp_bindgen::prelude::Serializable;
28
29#[cfg(feature = "axum_06")]
30use {
31    axum_06::http::StatusCode,
32    axum_06::response::{IntoResponse, Response},
33};
34
35/// A new PagerDuty receiver. This will be used in the create endpoint.
36#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, TypedBuilder)]
37#[non_exhaustive]
38#[serde(rename_all = "camelCase")]
39pub struct NewPagerDutyReceiver {
40    /// A reference to a template that will be expanded when a incident is
41    /// created. If this is empty then no template will be expanded.
42    #[builder(default, setter())]
43    pub incident_created_template_name: Option<Name>,
44}
45
46/// PagerDutyReceiver represents a single PagerDuty receiver in Fiberplane.
47#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, TypedBuilder)]
48#[non_exhaustive]
49#[serde(rename_all = "camelCase")]
50pub struct PagerDutyReceiver {
51    /// Unique identifier for the PagerDuty receiver for a workspace.
52    pub name: Name,
53
54    /// A reference to a template that will be expanded when a incident is
55    /// created. If this is empty then no template will be expanded.
56    #[builder(default, setter())]
57    pub incident_created_template_name: Option<Name>,
58
59    /// A shared security-key that should be set in the PagerDuty webhook
60    /// customer header.
61    pub security_key: String,
62
63    /// The URL that should be set in the PagerDuty webhook.
64    pub webhook_url: String,
65
66    /// Timestamp that the PagerDuty receiver was created.
67    #[builder(setter(into))]
68    pub created_at: Timestamp,
69
70    /// Timestamp that the PagerDuty receiver was last updated.
71    #[builder(setter(into))]
72    pub updated_at: Timestamp,
73}
74
75#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq, TypedBuilder)]
76#[non_exhaustive]
77#[serde(rename_all = "camelCase")]
78pub struct UpdatePagerDutyReceiver {
79    /// A reference to a template that will be expanded when a incident is
80    /// created. If this is empty then no template will be expanded.
81    #[builder(default, setter())]
82    #[serde(
83        default,
84        deserialize_with = "crate::deserialize_some",
85        skip_serializing_if = "Option::is_none"
86    )]
87    pub incident_created_template_name: Option<Option<Name>>,
88
89    /// If this value is set to true, then a new random security-key will be
90    /// generated. This new value will be part of the response.
91    #[builder(default, setter())]
92    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
93    pub regenerate_security_key: bool,
94}
95
96/// Errors that can occur when creating a new PagerDuty receiver.
97#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, Error)]
98#[cfg_attr(
99    feature = "fp-bindgen",
100    derive(Serializable),
101    fp(rust_module = "fiberplane_models::pagerduty")
102)]
103#[non_exhaustive]
104#[serde(tag = "error", rename_all = "snake_case")]
105pub enum PagerDutyReceiverCreateError {
106    #[error("Name of the PagerDuty receiver is already in use")]
107    DuplicateName,
108
109    #[error("Referenced creation template does not exist")]
110    CreationTemplateNotFound,
111
112    #[error("Unknown error occurred")]
113    InternalServerError,
114
115    /// Common auth errors.
116    #[serde(untagged)]
117    #[error(transparent)]
118    Auth(AuthError),
119}
120
121impl From<AuthError> for PagerDutyReceiverCreateError {
122    fn from(value: AuthError) -> Self {
123        PagerDutyReceiverCreateError::Auth(value)
124    }
125}
126
127#[cfg(feature = "axum_06")]
128impl IntoResponse for PagerDutyReceiverCreateError {
129    fn into_response(self) -> Response {
130        let body = serde_json::to_string(&self).expect("should never fail!");
131        let status_code = match self {
132            PagerDutyReceiverCreateError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
133            PagerDutyReceiverCreateError::CreationTemplateNotFound => StatusCode::BAD_REQUEST,
134            PagerDutyReceiverCreateError::Auth(AuthError::Unauthenticated) => {
135                StatusCode::UNAUTHORIZED
136            }
137            PagerDutyReceiverCreateError::Auth(AuthError::Unauthorized) => StatusCode::FORBIDDEN,
138            PagerDutyReceiverCreateError::DuplicateName => StatusCode::BAD_REQUEST,
139        };
140
141        (status_code, body).into_response()
142    }
143}
144
145/// Errors that can occur when retrieving a PagerDuty receiver.
146#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, Error)]
147#[cfg_attr(
148    feature = "fp-bindgen",
149    derive(Serializable),
150    fp(rust_module = "fiberplane_models::pagerduty")
151)]
152#[non_exhaustive]
153#[serde(tag = "error", rename_all = "snake_case")]
154pub enum PagerDutyReceiverGetError {
155    #[error("PagerDuty receiver not found")]
156    NotFound,
157
158    #[error("Unknown error occurred")]
159    InternalServerError,
160
161    /// Common auth errors.
162    #[serde(untagged)]
163    #[error(transparent)]
164    Auth(AuthError),
165}
166
167impl From<AuthError> for PagerDutyReceiverGetError {
168    fn from(value: AuthError) -> Self {
169        PagerDutyReceiverGetError::Auth(value)
170    }
171}
172
173#[cfg(feature = "axum_06")]
174impl IntoResponse for PagerDutyReceiverGetError {
175    fn into_response(self) -> Response {
176        let body = serde_json::to_string(&self).expect("should never fail!");
177        let status_code = match self {
178            PagerDutyReceiverGetError::NotFound => StatusCode::NOT_FOUND,
179            PagerDutyReceiverGetError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
180            PagerDutyReceiverGetError::Auth(AuthError::Unauthenticated) => StatusCode::UNAUTHORIZED,
181            PagerDutyReceiverGetError::Auth(AuthError::Unauthorized) => StatusCode::FORBIDDEN,
182        };
183
184        (status_code, body).into_response()
185    }
186}
187
188/// Errors that can occur when updating a PagerDuty receiver.
189#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, Error)]
190#[cfg_attr(
191    feature = "fp-bindgen",
192    derive(Serializable),
193    fp(rust_module = "fiberplane_models::pagerduty")
194)]
195#[non_exhaustive]
196#[serde(tag = "error", rename_all = "snake_case")]
197pub enum PagerDutyReceiverUpdateError {
198    #[error("PagerDuty receiver not found")]
199    NotFound,
200
201    #[error("Referenced creation template does not exist")]
202    CreationTemplateNotFound,
203
204    #[error("Unknown error occurred")]
205    InternalServerError,
206
207    /// Common auth errors.
208    #[serde(untagged)]
209    #[error(transparent)]
210    Auth(AuthError),
211}
212
213impl From<AuthError> for PagerDutyReceiverUpdateError {
214    fn from(value: AuthError) -> Self {
215        PagerDutyReceiverUpdateError::Auth(value)
216    }
217}
218
219#[cfg(feature = "axum_06")]
220impl IntoResponse for PagerDutyReceiverUpdateError {
221    fn into_response(self) -> Response {
222        let body = serde_json::to_string(&self).expect("should never fail!");
223        let status_code = match self {
224            PagerDutyReceiverUpdateError::NotFound => StatusCode::NOT_FOUND,
225            PagerDutyReceiverUpdateError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
226            PagerDutyReceiverUpdateError::CreationTemplateNotFound => StatusCode::BAD_REQUEST,
227            PagerDutyReceiverUpdateError::Auth(AuthError::Unauthenticated) => {
228                StatusCode::UNAUTHORIZED
229            }
230            PagerDutyReceiverUpdateError::Auth(AuthError::Unauthorized) => StatusCode::FORBIDDEN,
231        };
232
233        (status_code, body).into_response()
234    }
235}
236
237/// Errors that can occur when deleting a PagerDuty receiver.
238#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, Error)]
239#[cfg_attr(
240    feature = "fp-bindgen",
241    derive(Serializable),
242    fp(rust_module = "fiberplane_models::pagerduty")
243)]
244#[non_exhaustive]
245#[serde(tag = "error", rename_all = "snake_case")]
246pub enum PagerDutyReceiverDeleteError {
247    #[error("PagerDuty receiver not found")]
248    NotFound,
249
250    #[error("Unknown error occurred")]
251    InternalServerError,
252
253    /// Common auth errors.
254    #[serde(untagged)]
255    #[error(transparent)]
256    Auth(AuthError),
257}
258
259impl From<AuthError> for PagerDutyReceiverDeleteError {
260    fn from(value: AuthError) -> Self {
261        PagerDutyReceiverDeleteError::Auth(value)
262    }
263}
264
265#[cfg(feature = "axum_06")]
266impl IntoResponse for PagerDutyReceiverDeleteError {
267    fn into_response(self) -> Response {
268        let body = serde_json::to_string(&self).expect("should never fail!");
269        let status_code = match self {
270            PagerDutyReceiverDeleteError::NotFound => StatusCode::NOT_FOUND,
271            PagerDutyReceiverDeleteError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
272            PagerDutyReceiverDeleteError::Auth(AuthError::Unauthenticated) => {
273                StatusCode::UNAUTHORIZED
274            }
275            PagerDutyReceiverDeleteError::Auth(AuthError::Unauthorized) => StatusCode::FORBIDDEN,
276        };
277
278        (status_code, body).into_response()
279    }
280}
281
282/// Errors that can occur when listing PagerDuty receivers.
283#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, Error)]
284#[cfg_attr(
285    feature = "fp-bindgen",
286    derive(Serializable),
287    fp(rust_module = "fiberplane_models::pagerduty")
288)]
289#[non_exhaustive]
290#[serde(tag = "error", rename_all = "snake_case")]
291pub enum PagerDutyReceiverListError {
292    #[error("Unknown error occurred")]
293    InternalServerError,
294
295    /// Common auth errors.
296    #[serde(untagged)]
297    #[error(transparent)]
298    Auth(AuthError),
299}
300
301impl From<AuthError> for PagerDutyReceiverListError {
302    fn from(value: AuthError) -> Self {
303        PagerDutyReceiverListError::Auth(value)
304    }
305}
306
307#[cfg(feature = "axum_06")]
308impl IntoResponse for PagerDutyReceiverListError {
309    fn into_response(self) -> Response {
310        let body = serde_json::to_string(&self).expect("should never fail!");
311        let status_code = match self {
312            PagerDutyReceiverListError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
313            PagerDutyReceiverListError::Auth(AuthError::Unauthenticated) => {
314                StatusCode::UNAUTHORIZED
315            }
316            PagerDutyReceiverListError::Auth(AuthError::Unauthorized) => StatusCode::FORBIDDEN,
317        };
318
319        (status_code, body).into_response()
320    }
321}
322
323#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq, IntoStaticStr)]
324#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
325#[cfg_attr(
326    feature = "fp-bindgen",
327    derive(Serializable),
328    fp(rust_module = "fiberplane_models::pagerduty")
329)]
330#[non_exhaustive]
331#[serde(rename_all = "snake_case")]
332#[strum(serialize_all = "snake_case")]
333pub enum PagerDutyReceiverListSortFields {
334    Name,
335    CreatedAt,
336    UpdatedAt,
337}
338
339impl SortField for PagerDutyReceiverListSortFields {
340    #[inline]
341    fn default_sort_field() -> Self {
342        Self::Name
343    }
344}