praiya 0.4.0

An async PagerDuty API client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Method, error and parameter types for the Escalation Policies endpoint.

use futures_core::Stream;
use futures_util::StreamExt;
use http::header::FROM;
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::errors::Error;
use crate::models::*;
use crate::praiya::{NoopParams, PraiyaCustomHeaders};
use crate::{
    BaseOption, BaseRequest, PaginatedResponse, PaginationQueryComponent, Praiya, SingleResponse,
    SubSystem, DEFAULT_PAGERDUTY_API_LIMIT,
};

pub const API_ENDPOINT: &str = "https://api.pagerduty.com";

/// A client for the PagerDuty incidents API
pub struct EscalationPoliciesClient {
    pub(crate) api_endpoint: String,
    pub(crate) client: Praiya,
    pub(crate) from_email: Option<String>,
}

impl Praiya {
    pub fn escalation_policies(&self, from_email: Option<String>) -> EscalationPoliciesClient {
        EscalationPoliciesClient {
            api_endpoint: std::env::var("PAGERDUTY_API_ENDPOINT")
                .unwrap_or_else(|_| String::from(API_ENDPOINT)),
            client: Praiya::clone(self),
            from_email,
        }
    }
}

single_response_type!(EscalationPolicy, escalation_policy, CreateEscalationPolicy);

single_response_type!(EscalationPolicy, escalation_policy, GetEscalationPolicy);

list_response_type!(ListEscalationPolicy, escalation_policies, EscalationPolicy);

#[derive(praiya_macro::PraiyaParamsBuilder)]
#[doc = "[EscalationPoliciesClient::get_escalation_policy]"]
#[allow(dead_code)]
struct GetEscalationPolicy {
    include: Vec<String>,
}

#[derive(praiya_macro::PraiyaParamsBuilder)]
#[doc = "[EscalationPoliciesClient::list_escalation_policies"]
#[allow(dead_code)]
struct ListEscalationPolicies {
    query: String,
    include: Vec<String>,
    sort_by: String,
    team_ids: Vec<String>,
    user_ids: Vec<String>,
}

#[derive(praiya_macro::PraiyaParamsBuilder)]
#[doc = "[EscalationPoliciesClient::list_escalation_policy_audit_records]"]
#[allow(dead_code)]
struct ListEscalationPolicyAuditRecords {
    since: chrono::DateTime<chrono::FixedOffset>,
    until: chrono::DateTime<chrono::FixedOffset>,
}

single_response_type!(EscalationPolicy, escalation_policy, UpdateEscalationPolicy);

impl EscalationPoliciesClient {
    /// ---
    ///
    /// # Create an escalation policy
    ///
    /// Creates a new escalation policy. At least one escalation rule must be provided.
    ///
    /// ---
    pub async fn create_escalation_policy(
        &self,
        body: CreateEscalationPolicy,
    ) -> Result<EscalationPolicy, Error> {
        let url = Praiya::parse_url(&self.api_endpoint, "/escalation_policies", None)?;

        let mut builder = http::request::Builder::new();
        if let Some(from) = &self.from_email {
            builder = builder.header(FROM, String::clone(from));
        }
        let req = self.client.build_request(
            url,
            builder.method(http::method::Method::POST),
            Praiya::serialize_payload(body)?,
        );

        self.client
            .process_into_value::<_, CreateEscalationPolicyResponse>(req)
            .await
    }

    /// ---
    ///
    /// # Delete an escalation policy
    ///
    /// Deletes an existing escalation policy and rules. The escalation policy must not be in use by any services.
    ///
    /// ---
    pub async fn delete_escalation_policy(&self, id: &str) -> Result<(), Error> {
        let url = Praiya::parse_url(
            &self.api_endpoint,
            &format!("/escalation_policies/{}", &id),
            None,
        )?;

        let req = self.client.build_request(
            url,
            http::request::Builder::new().method(http::method::Method::DELETE),
            hyper::Body::empty(),
        );

        self.client.process_into_unit(req).await
    }

    /// ---
    ///
    /// # Get an escalation policy
    ///
    /// Get information about an existing escalation policy and its rules.
    ///
    /// ---
    pub async fn get_escalation_policy(
        &self,
        id: &str,
        query_params: GetEscalationPolicyParams,
    ) -> Result<EscalationPolicy, Error> {
        let url = Praiya::parse_url(
            &self.api_endpoint,
            &format!("/escalation_policies/{}", &id),
            Some(&query_params.qs),
        )?;

        let req = self.client.build_request(
            url,
            http::request::Builder::new().method(http::method::Method::GET),
            hyper::Body::empty(),
        );

        self.client
            .process_into_value::<_, GetEscalationPolicyResponse>(req)
            .await
    }

    /// ---
    ///
    /// # List escalation policies
    ///
    /// List all of the existing escalation policies.
    ///
    /// ---
    pub fn list_escalation_policies(
        &self,
        query_params: ListEscalationPoliciesParams,
    ) -> impl Stream<Item = Result<EscalationPolicy, Error>> + '_ {
        self.client
            .list_request::<_, _, ListEscalationPolicyResponse>(
                &self.api_endpoint,
                "/escalation_policies",
                query_params,
                PraiyaCustomHeaders::None,
            )
    }

    /// ---
    ///
    /// # List audit records for an escalation policy
    ///
    /// The returned records are sorted by the `execution_time` from newest to oldest.
    ///
    /// ---
    pub fn list_escalation_policy_audit_records(
        &self,
        id: &str,
        query_params: ListEscalationPolicyAuditRecordsParams,
    ) -> impl Stream<Item = Result<AuditRecord, Error>> + '_ {
        let base_request = BaseRequest {
            host: String::from(&self.api_endpoint),
            method: http::Method::GET,
            options: std::sync::Arc::new(query_params),
            path: format!("/escalation_policies/{}/audit/records", &id),
            headers: std::collections::HashMap::new(),
        };

        self.client.process_into_paginated_stream::<AuditRecord, crate::praiya::PaginatedCursorResponse, crate::praiya::PaginatedCursorPosition, crate::praiya::PaginationCursorQueryComponent>(
            base_request,
            std::sync::Arc::new(crate::praiya::PaginationCursorQueryComponent {
                cursor: None,
                limit: DEFAULT_PAGERDUTY_API_LIMIT,
            }),
        )
        .boxed()
    }

    /// ---
    ///
    /// # Update an escalation policy
    ///
    /// Updates an existing escalation policy and rules.
    ///
    /// ---
    pub async fn update_escalation_policy(
        &self,
        id: &str,
        body: UpdateEscalationPolicy,
    ) -> Result<EscalationPolicy, Error> {
        let url = Praiya::parse_url(
            &self.api_endpoint,
            &format!("/escalation_policies/{}", &id),
            None,
        )?;

        let req = self.client.build_request(
            url,
            http::request::Builder::new().method(http::method::Method::PUT),
            Praiya::serialize_payload(body)?,
        );

        self.client
            .process_into_value::<_, UpdateEscalationPolicyResponse>(req)
            .await
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::models::*;
    use crate::praiya::ParamsBuilder;
    use futures_util::TryStreamExt;

    #[tokio::test]
    async fn test_create_escalation_policy() {
        let pagerduty = crate::Praiya::new("test");
        let create_escalation_policy = CreateEscalationPolicy {
            escalation_policy: EscalationPolicy {
                name: Some(String::from("Engineering Escalation Policy")),
                escalation_rules: Some(vec![EscalationRule {
                    escalation_delay_in_minutes: 30,
                    targets: vec![EscalationTargetReference {
                        id: Some(String::from("PEYSGVF")),
                        _type: EscalationTargetReferenceTypeEnum::USER_REFERENCE,
                        ..Default::default()
                    }],
                    ..Default::default()
                }]),
                services: Some(vec![Service {
                    id: Some(String::from("PIJ90N7")),
                    _type: ServiceTypeEnum::SERVICE_REFERENCE,
                    ..Default::default()
                }]),
                num_loops: Some(2),
                on_call_handoff_notifications: Some(
                    EscalationPolicyOnCallHandoffNotificationsEnum::IF_HAS_SERVICES,
                ),
                teams: Some(vec![Team {
                    id: Some(String::from("PQ9K7I8")),
                    _type: TeamTypeEnum::TEAM_REFERENCE,
                    ..Default::default()
                }]),
                description: Some(String::from("Here is the ep for the engineering team.")),
                ..Default::default()
            },
        };
        let escalation_policy = pagerduty
            .escalation_policies(Some(String::from("from@example.com")))
            .create_escalation_policy(create_escalation_policy)
            .await
            .unwrap();

        assert_eq!(escalation_policy.id, Some(String::from("PT20YPA")));
    }

    #[tokio::test]
    async fn test_delete_escalation_policy() {
        let pagerduty = crate::Praiya::new("test");
        let unit = pagerduty
            .escalation_policies(Some(String::from("from@example.com")))
            .delete_escalation_policy("PIJ90N7")
            .await
            .unwrap();

        assert_eq!(unit, ());
    }

    #[tokio::test]
    async fn test_get_escalation_policy() {
        let pagerduty = crate::Praiya::new("test");

        let mut opts_builder = super::GetEscalationPolicyParamsBuilder::new();
        opts_builder.include(vec![]);
        let opts = opts_builder.build();

        let escalation_policy = pagerduty
            .escalation_policies(Some(String::from("from@example.com")))
            .get_escalation_policy("PIJ90N7", opts)
            .await
            .unwrap();

        assert_eq!(escalation_policy.id, Some(String::from("PT20YPA")));
    }

    #[tokio::test]
    async fn test_list_escalation_policies() {
        let pagerduty = crate::Praiya::new("test");

        let mut opts_builder = super::ListEscalationPoliciesParamsBuilder::new();
        opts_builder.include(vec![]);
        opts_builder.query("eng");
        opts_builder.sort_by("name:asc");
        opts_builder.team_ids(vec![]);
        opts_builder.user_ids(vec![]);
        let opts = opts_builder.build();

        let escalation_policy: Option<EscalationPolicy> = pagerduty
            .escalation_policies(Some(String::from("from@example.com")))
            .list_escalation_policies(opts)
            .try_next()
            .await
            .unwrap();

        assert_eq!(
            escalation_policy.unwrap().id.as_ref().unwrap(),
            &String::from("PANZZEQ")
        );
    }

    #[tokio::test]
    async fn test_list_escalation_policy_audit_records() {
        let pagerduty = crate::Praiya::new("test");

        let mut opts_builder = super::ListEscalationPolicyAuditRecordsParamsBuilder::new();
        let now = chrono::Utc::now();
        let since = now - chrono::Duration::days(1);
        opts_builder.since(&since);
        opts_builder.until(&now);
        let opts = opts_builder.build();

        let record: Option<AuditRecord> = pagerduty
            .escalation_policies(Some(String::from("from@example.com")))
            .list_escalation_policy_audit_records("PIJ90N7", opts)
            .try_next()
            .await
            .unwrap();

        assert_eq!(
            record.unwrap().id,
            String::from("PD_ASSIGN_TEAM_TO_ESCALATION_POLICY")
        );
    }

    #[tokio::test]
    async fn test_update_escalation_policy() {
        let pagerduty = crate::Praiya::new("test");

        let update_escalation_policy = UpdateEscalationPolicy {
            escalation_policy: EscalationPolicy {
                name: Some(String::from("Engineering Escalation Policy")),
                escalation_rules: Some(vec![EscalationRule {
                    escalation_delay_in_minutes: 30,
                    targets: vec![EscalationTargetReference {
                        id: Some(String::from("PEYSGVF")),
                        _type: EscalationTargetReferenceTypeEnum::USER_REFERENCE,
                        ..Default::default()
                    }],
                    ..Default::default()
                }]),
                services: Some(vec![Service {
                    id: Some(String::from("PIJ90N7")),
                    _type: ServiceTypeEnum::SERVICE_REFERENCE,
                    ..Default::default()
                }]),
                num_loops: Some(2),
                on_call_handoff_notifications: Some(
                    EscalationPolicyOnCallHandoffNotificationsEnum::IF_HAS_SERVICES,
                ),
                teams: Some(vec![Team {
                    id: Some(String::from("PQ9K7I8")),
                    _type: TeamTypeEnum::TEAM_REFERENCE,
                    ..Default::default()
                }]),
                description: Some(String::from("Here is the ep for the engineering team.")),
                ..Default::default()
            },
        };

        let service = pagerduty
            .escalation_policies(Some(String::from("from@example.com")))
            .update_escalation_policy("PIJ90N7", update_escalation_policy)
            .await
            .unwrap();

        assert_eq!(service.id, Some(String::from("PT20YPA")));
    }
}