pagerduty_api/
types.rs

1//! Types for our Pagerduty API client.
2
3use chrono::{DateTime, Utc};
4use parse_display::{Display, FromStr};
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8/// A list of services.
9#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
10pub struct ServiceListResponse {
11    /// The list of services.
12    #[serde(default, skip_serializing_if = "Vec::is_empty")]
13    pub services: Vec<Service>,
14    /// Echoes limit pagination property.
15    #[serde(default)]
16    pub limit: i64,
17    /// Echoes offset pagination property.
18    #[serde(default)]
19    pub offset: i64,
20    /// Indicates if there are additional records to return.
21    #[serde(default)]
22    pub more: bool,
23}
24
25/// A service object.
26#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
27pub struct ServiceObject {
28    /// The service.
29    #[serde(default)]
30    pub service: Service,
31}
32
33/// A service.
34#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
35pub struct Service {
36    /// The ID.
37    #[serde(
38        default,
39        skip_serializing_if = "String::is_empty",
40        deserialize_with = "deserialize_null_string::deserialize"
41    )]
42    pub id: String,
43    /// The name of the service.
44    #[serde(
45        default,
46        skip_serializing_if = "String::is_empty",
47        deserialize_with = "deserialize_null_string::deserialize"
48    )]
49    pub name: String,
50    /// The description of the service.
51    #[serde(
52        default,
53        skip_serializing_if = "String::is_empty",
54        deserialize_with = "deserialize_null_string::deserialize"
55    )]
56    pub description: String,
57    /// Time in seconds that an incident is automatically resolved if left open for that long. Value is null if the feature is disabled. Value must not be negative. Setting this field to 0, null (or unset in POST request) will disable the feature.
58    #[serde(default, skip_serializing_if = "Option::is_none")]
59    pub auto_resolve_timeout: Option<i64>,
60    /// Time in seconds that an incident changes to the Triggered State after being Acknowledged. Value is null if the feature is disabled. Value must not be negative. Setting this field to 0, null (or unset in POST request) will disable the feature.
61    #[serde(default, skip_serializing_if = "Option::is_none")]
62    pub acknowledgement_timeout: Option<i64>,
63    /// The current state of the Service. Valid statuses are:
64    ///
65    /// - `active`: The service is enabled and has no open incidents. This is the only status a service can be created with.
66    /// - `warning`: The service is enabled and has one or more acknowledged incidents.
67    /// - `critical`: The service is enabled and has one or more triggered incidents.
68    /// - `maintenance`: The service is under maintenance, no new incidents will be triggered during maintenance mode.
69    /// - `disabled`: The service is disabled and will not have any new triggered incidents.
70    #[serde(default)]
71    pub status: Status,
72    /// Escalation policy.
73    #[serde(default)]
74    pub escalation_policy: EscalationPolicy,
75    /// Incident urgency rule.
76    #[serde(default)]
77    pub incident_urgency_rule: IncidentUrgencyRule,
78    /// Support Hours.
79    #[serde(default)]
80    pub support_hours: Option<SupportHours>,
81    /// An array containing scheduled actions for the service.
82    #[serde(default)]
83    pub scheduled_actions: Vec<ScheduledAction>,
84    /// Whether a service creates only incidents, or both alerts and incidents. A service must create alerts in order to enable incident merging.
85    ///
86    /// - `create_incidents` - The service will create one incident and zero alerts for each incoming event.
87    /// - `create_alerts_and_incidents` - The service will create one incident and one associated alert for each incoming event.
88    #[serde(default)]
89    pub alert_creation: AlertCreation,
90    /// Defines how alerts on this service will be automatically grouped into incidents. Note that the alert grouping features are available only on certain plans.
91    #[serde(default)]
92    pub alert_grouping_parameters: AlertGroupingParameters,
93    /// Defines how alerts on this service are automatically suspended for a period of time before triggering, when identified as likely being transient. Note that automatically pausing notifications is only available on certain plans.
94    #[serde(default)]
95    pub auto_pause_notifications_parameters: AutoPauseNotificationsParameters,
96
97    /// An API link to itself.
98    #[serde(
99        default,
100        skip_serializing_if = "String::is_empty",
101        deserialize_with = "deserialize_null_string::deserialize",
102        rename = "self"
103    )]
104    pub self_: String,
105    /// An HTML link to itself.
106    #[serde(
107        default,
108        skip_serializing_if = "String::is_empty",
109        deserialize_with = "deserialize_null_string::deserialize"
110    )]
111    pub html_url: String,
112    /// The date/time when this service was created.
113    #[serde(default, skip_serializing_if = "Option::is_none")]
114    pub created_at: Option<DateTime<Utc>>,
115    /// The date/time when the most recent incident was created for this service.
116    #[serde(default, skip_serializing_if = "Option::is_none")]
117    pub last_incident_timestamp: Option<DateTime<Utc>>,
118}
119
120/// The state of the service.
121#[derive(Display, FromStr, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone)]
122#[display(style = "snake_case")]
123#[serde(rename_all = "snake_case")]
124pub enum Status {
125    /// Active.
126    Active,
127    /// Warning.
128    Warning,
129    /// Critical.
130    Critical,
131    /// Maintenance.
132    Maintenance,
133    /// Disabled.
134    Disabled,
135}
136
137impl Default for Status {
138    fn default() -> Self {
139        Status::Active
140    }
141}
142
143/// How a service creates incidents.
144#[derive(Display, FromStr, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone)]
145#[display(style = "snake_case")]
146#[serde(rename_all = "snake_case")]
147pub enum AlertCreation {
148    /// Create incidents.
149    CreateIncidents,
150    /// Create alerts and incidents.
151    CreateAlertsAndIncidents,
152}
153
154impl Default for AlertCreation {
155    fn default() -> Self {
156        AlertCreation::CreateAlertsAndIncidents
157    }
158}
159
160/// An escalation policy.
161#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
162pub struct EscalationPolicy {
163    /// The ID.
164    #[serde(
165        default,
166        skip_serializing_if = "String::is_empty",
167        deserialize_with = "deserialize_null_string::deserialize"
168    )]
169    pub id: String,
170    /// The type.
171    #[serde(default, skip_serializing_if = "Option::is_none", rename = "type")]
172    pub type_: Option<EscalationPolicyType>,
173    /// The summary.
174    #[serde(
175        default,
176        skip_serializing_if = "String::is_empty",
177        deserialize_with = "deserialize_null_string::deserialize"
178    )]
179    pub summary: String,
180    /// The name.
181    #[serde(
182        default,
183        skip_serializing_if = "String::is_empty",
184        deserialize_with = "deserialize_null_string::deserialize"
185    )]
186    pub name: String,
187    /// The description.
188    #[serde(
189        default,
190        skip_serializing_if = "String::is_empty",
191        deserialize_with = "deserialize_null_string::deserialize"
192    )]
193    pub description: String,
194    /// An API link to itself.
195    #[serde(
196        default,
197        skip_serializing_if = "String::is_empty",
198        deserialize_with = "deserialize_null_string::deserialize",
199        rename = "self"
200    )]
201    pub self_: String,
202    /// An HTML link to itself.
203    #[serde(
204        default,
205        skip_serializing_if = "String::is_empty",
206        deserialize_with = "deserialize_null_string::deserialize"
207    )]
208    pub html_url: String,
209}
210
211/// The type of escalation_policy.
212#[derive(Display, FromStr, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone)]
213#[display(style = "snake_case")]
214#[serde(rename_all = "snake_case")]
215pub enum EscalationPolicyType {
216    /// A reference to an escalation policy.
217    EscalationPolicyReference,
218    /// An escalation policy object.
219    EscalationPolicy,
220}
221
222impl Default for EscalationPolicyType {
223    fn default() -> Self {
224        EscalationPolicyType::EscalationPolicyReference
225    }
226}
227
228/// An incident urgency rule.
229#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
230pub struct IncidentUrgencyRule {
231    /// The type.
232    #[serde(default, rename = "type")]
233    pub type_: IncidentUrgencyRuleType,
234    /// The urgency.
235    #[serde(default)]
236    pub urgency: Option<Urgency>,
237    /// The rule to use during support hours.
238    #[serde(default, skip_serializing_if = "Option::is_none")]
239    pub during_support_hours: Option<Box<IncidentUrgencyRule>>,
240    /// The rule to use outside support hours.
241    #[serde(default, skip_serializing_if = "Option::is_none")]
242    pub outside_support_hours: Option<Box<IncidentUrgencyRule>>,
243}
244
245impl Default for IncidentUrgencyRule {
246    fn default() -> Self {
247        IncidentUrgencyRule {
248            type_: IncidentUrgencyRuleType::Constant,
249            urgency: Some(Urgency::High),
250            during_support_hours: None,
251            outside_support_hours: None,
252        }
253    }
254}
255
256/// The type of incident urgency rule.
257#[derive(Display, FromStr, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone)]
258#[display(style = "snake_case")]
259#[serde(rename_all = "snake_case")]
260pub enum IncidentUrgencyRuleType {
261    /// Constant.
262    Constant,
263    /// Use support hours.
264    UseSupportHours,
265}
266
267impl Default for IncidentUrgencyRuleType {
268    fn default() -> Self {
269        IncidentUrgencyRuleType::Constant
270    }
271}
272
273/// The incidents' urgency, if type is constant.
274#[derive(Display, FromStr, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone)]
275#[display(style = "snake_case")]
276#[serde(rename_all = "snake_case")]
277pub enum Urgency {
278    /// Low.
279    Low,
280    /// High.
281    High,
282    /// Severity based.
283    SeverityBased,
284}
285
286impl Default for Urgency {
287    fn default() -> Self {
288        Urgency::High
289    }
290}
291
292/// The support hours.
293#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
294pub struct SupportHours {
295    /// The type.
296    #[serde(default, rename = "type")]
297    pub type_: SupportHoursType,
298    /// The time zone for the support hours.
299    #[serde(
300        default,
301        skip_serializing_if = "String::is_empty",
302        deserialize_with = "deserialize_null_string::deserialize"
303    )]
304    pub time_zone: String,
305    /// The support hours' starting time of day (date portion is ignored).
306    #[serde(
307        deserialize_with = "serde_naive_time::deserialize",
308        serialize_with = "serde_naive_time::serialize"
309    )]
310    pub start_time: chrono::NaiveTime,
311    /// The support hours' ending time of day (date portion is ignored).
312    #[serde(
313        deserialize_with = "serde_naive_time::deserialize",
314        serialize_with = "serde_naive_time::serialize"
315    )]
316    pub end_time: chrono::NaiveTime,
317    /// The days of the week.
318    #[serde(default, skip_serializing_if = "Vec::is_empty")]
319    pub days_of_week: Vec<i32>,
320}
321
322/// The type of support hours.
323#[derive(Display, FromStr, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone)]
324#[display(style = "snake_case")]
325#[serde(rename_all = "snake_case")]
326pub enum SupportHoursType {
327    /// Fixed time per day.
328    FixedTimePerDay,
329}
330
331impl Default for SupportHoursType {
332    fn default() -> Self {
333        SupportHoursType::FixedTimePerDay
334    }
335}
336
337/// A scheduled action.
338#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
339pub struct ScheduledAction {
340    /// The type.
341    #[serde(default, rename = "type")]
342    pub type_: ScheduledActionType,
343    /// Represents when scheduled action will occur.
344    #[serde(default)]
345    pub at: ScheduledActionAt,
346    /// Urgency level. Must be set to high.
347    #[serde(default)]
348    pub to_urgency: Urgency,
349}
350
351/// The type of scheduled action.
352#[derive(Display, FromStr, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone)]
353#[display(style = "snake_case")]
354#[serde(rename_all = "snake_case")]
355pub enum ScheduledActionType {
356    /// Urgency change.
357    UrgencyChange,
358}
359
360impl Default for ScheduledActionType {
361    fn default() -> Self {
362        ScheduledActionType::UrgencyChange
363    }
364}
365
366/// Represents when scheduled action will occur.
367#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
368pub struct ScheduledActionAt {
369    /// The type.
370    #[serde(default, rename = "type")]
371    pub type_: ScheduledActionAtType,
372    /// Designates either the start or the end of support hours.
373    #[serde(default)]
374    pub name: ScheduledActionAtName,
375}
376
377/// The type of scheduled action at.
378#[derive(Display, FromStr, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone)]
379#[display(style = "snake_case")]
380#[serde(rename_all = "snake_case")]
381pub enum ScheduledActionAtType {
382    /// Named time.
383    NamedTime,
384}
385
386impl Default for ScheduledActionAtType {
387    fn default() -> Self {
388        ScheduledActionAtType::NamedTime
389    }
390}
391
392/// Designates either the start or the end of support hours.
393#[derive(Display, FromStr, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone)]
394#[display(style = "snake_case")]
395#[serde(rename_all = "snake_case")]
396pub enum ScheduledActionAtName {
397    /// Support hours start.
398    SupportHoursStart,
399    /// Support hours end.
400    SupportHoursEnd,
401}
402
403impl Default for ScheduledActionAtName {
404    fn default() -> Self {
405        ScheduledActionAtName::SupportHoursStart
406    }
407}
408
409/// Defines how alerts on this service will be automatically grouped into incidents. Note that the alert grouping features are available only on certain plans.
410#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
411pub struct AlertGroupingParameters {
412    /// The type.
413    #[serde(default, rename = "type")]
414    pub type_: Option<AlertGroupingType>,
415}
416
417/// The type of alert grouping.
418#[derive(Display, FromStr, PartialEq, Debug, JsonSchema, Deserialize, Serialize, Clone)]
419#[display(style = "snake_case")]
420#[serde(rename_all = "snake_case")]
421pub enum AlertGroupingType {
422    /// Time.
423    Time,
424    /// Intelligent.
425    Intelligent,
426    /// Content based.
427    ContentBased,
428}
429
430impl Default for AlertGroupingType {
431    fn default() -> Self {
432        AlertGroupingType::Intelligent
433    }
434}
435
436/// Defines how alerts on this service are automatically suspended for a period of time before triggering, when identified as likely being transient. Note that automatically pausing notifications is only available on certain plans.
437#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
438pub struct AutoPauseNotificationsParameters {
439    /// Indicates whether alerts should be automatically suspended when identified as transient.
440    #[serde(default)]
441    pub enabled: bool,
442    /// Indicates in seconds how long alerts should be suspended before triggering.
443    /// Allowed values: 120, 180, 300, 600, 900
444    #[serde(default)]
445    pub timeout: Option<i64>,
446}
447
448/// A list of escalation policies.
449#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
450pub struct EscalationPolicyListResponse {
451    /// The list of services.
452    #[serde(default, skip_serializing_if = "Vec::is_empty")]
453    pub escalation_policies: Vec<EscalationPolicy>,
454    /// Echoes limit pagination property.
455    #[serde(default)]
456    pub limit: i64,
457    /// Echoes offset pagination property.
458    #[serde(default)]
459    pub offset: i64,
460    /// Indicates if there are additional records to return.
461    #[serde(default)]
462    pub more: bool,
463}
464
465/// A module for deserializing a null string.
466pub mod deserialize_null_string {
467    use serde::Deserialize;
468
469    /// Deserialize a null string as an empty string.
470    pub fn deserialize<'de, D>(deserializer: D) -> Result<String, D::Error>
471    where
472        D: serde::Deserializer<'de>,
473    {
474        let s = String::deserialize(deserializer).unwrap_or_default();
475
476        Ok(s)
477    }
478}
479
480/// A module for deserializing a null string.
481pub mod serde_naive_time {
482    use chrono::NaiveTime;
483    use serde::{Deserialize, Serializer};
484
485    /// Deserialize a time to the right format.
486    pub fn deserialize<'de, D>(deserializer: D) -> Result<NaiveTime, D::Error>
487    where
488        D: serde::Deserializer<'de>,
489    {
490        let s = NaiveTime::deserialize(deserializer)?;
491
492        Ok(s)
493    }
494
495    /// Serialize a time to the right format.
496    pub fn serialize<S>(time: &NaiveTime, serializer: S) -> Result<S::Ok, S::Error>
497    where
498        S: Serializer,
499    {
500        let s = format!("{}", time.format("%H:%M:%S"));
501        serializer.serialize_str(&s)
502    }
503}