sui_security_watchdog/
pagerduty.rs

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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use anyhow::anyhow;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tracing::info;

#[derive(Serialize, Deserialize, Debug)]
pub struct Service {
    pub id: String,
    #[serde(rename = "type")]
    pub r#type: String,
}

impl Default for Service {
    fn default() -> Self {
        Service {
            id: "".to_string(),
            r#type: "service_reference".to_string(),
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Body {
    #[serde(rename = "type")]
    pub r#type: String,
    pub details: String,
}

impl Default for Body {
    fn default() -> Self {
        Body {
            r#type: "incident_body".to_string(),
            details: "".to_string(),
        }
    }
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Incident {
    pub incident_key: String,
    #[serde(rename = "type")]
    pub r#type: String,
    pub title: String,
    pub service: Service,
    pub body: Body,
}

impl Default for Incident {
    fn default() -> Self {
        Incident {
            incident_key: "".to_string(),
            r#type: "incident".to_string(),
            title: "".to_string(),
            service: Service::default(),
            body: Body::default(),
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct CreateIncident {
    pub incident: Incident,
}

#[derive(Clone)]
pub struct Pagerduty {
    pub client: Arc<reqwest::Client>,
    pub api_key: String,
}

impl Pagerduty {
    pub fn new(api_key: String) -> Self {
        Pagerduty {
            client: Arc::new(reqwest::Client::new()),
            api_key,
        }
    }

    pub async fn create_incident(
        &self,
        from: &str,
        incident: CreateIncident,
    ) -> anyhow::Result<()> {
        let token = format!("Token token={}", self.api_key);

        let response = self
            .client
            .post("https://api.pagerduty.com/incidents")
            .header("Authorization", token)
            .header("Content-Type", "application/json")
            .header("Accept", "application/json")
            .header("From", from)
            .json(&incident)
            .send()
            .await?;
        // Check if the status code is in the range of 200-299
        if response.status().is_success() {
            info!(
                "Created incident with key: {:?}",
                incident.incident.incident_key
            );
            Ok(())
        } else {
            let status = response.status();
            let text = response.text().await?;
            if status.is_client_error()
                && text.contains(
                    "Open incident with matching dedup key already exists on this service",
                )
            {
                info!(
                    "Incident already exists with key: {}",
                    incident.incident.incident_key
                );
                Ok(())
            } else {
                Err(anyhow!("Failed to create incident: {}", text))
            }
        }
    }
}