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
use client;
use errors::*;
use reqwest::Method;

/// A graph annotation
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct GraphAnnotation {
    pub id: Option<String>,
    pub title: String,
    pub description: String,
    pub from: u64,
    pub to: u64,
    pub service: String,
    pub roles: Option<Vec<String>>,
}

#[cfg(test)]
mod tests {
    use graph_annotation::*;
    use serde_json;

    fn graph_annotation_example() -> GraphAnnotation {
        GraphAnnotation {
            id: Some("abcde1".to_string()),
            title: "Deploy application".to_string(),
            description: "Graph Annotation Example\nhttps://example.com".to_string(),
            from: 1484000000,
            to: 1484000030,
            service: "ExampleService".to_string(),
            roles: Some(vec!["ExampleRole1".to_string(), "ExampleRole2".to_string()]),
        }
    }

    fn json_example() -> serde_json::Value {
        json!({
            "id": "abcde1",
            "title": "Deploy application",
            "description": "Graph Annotation Example\nhttps://example.com",
            "from": 1484000000,
            "to": 1484000030,
            "service": "ExampleService",
            "roles": ["ExampleRole1", "ExampleRole2"]
        })
    }

    #[test]
    fn serialize_graph_annotation() {
        assert_eq!(
            json_example(),
            serde_json::to_value(&graph_annotation_example()).unwrap()
        );
    }

    #[test]
    fn deserialize_graph_annotation() {
        assert_eq!(
            graph_annotation_example(),
            serde_json::from_value(json_example()).unwrap()
        );
    }
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct ListGraphAnnotationsResponse {
    graph_annotations: Vec<GraphAnnotation>,
}

impl client::Client {
    /// Fetches graph annotations.
    ///
    /// See https://mackerel.io/api-docs/entry/graph-annotations#get.
    pub fn list_graph_annotations(
        &self,
        service: &str,
        from: u64,
        to: u64,
    ) -> Result<Vec<GraphAnnotation>> {
        self.request(
            Method::GET,
            "/api/v0/graph-annotations",
            vec![
                ("service", vec![service]),
                ("from", vec![&from.to_string()]),
                ("to", vec![&to.to_string()]),
            ],
            client::empty_body(),
            |res: ListGraphAnnotationsResponse| res.graph_annotations,
        )
    }

    /// Creates a new graph annotation.
    ///
    /// See https://mackerel.io/api-docs/entry/graph-annotations#create.
    pub fn create_graph_annotation(
        &self,
        graph_annotation: GraphAnnotation,
    ) -> Result<GraphAnnotation> {
        self.request(
            Method::POST,
            "/api/v0/graph-annotations",
            vec![],
            Some(graph_annotation),
            |graph_annotation| graph_annotation,
        )
    }

    /// Updates a graph annotation.
    ///
    /// See https://mackerel.io/api-docs/entry/graph-annotations#update.
    pub fn update_graph_annotation(
        &self,
        graph_annotation: GraphAnnotation,
    ) -> Result<GraphAnnotation> {
        let graph_annotation_id: String = graph_annotation
            .clone()
            .id
            .ok_or("specify the id to update a graph_annotation")?;
        self.request(
            Method::PUT,
            format!("/api/v0/graph-annotations/{}", graph_annotation_id),
            vec![],
            Some(graph_annotation),
            |graph_annotation| graph_annotation,
        )
    }

    /// Deletes a graph annotation.
    ///
    /// See https://mackerel.io/api-docs/entry/graph-annotations#delete.
    pub fn delete_graph_annotation(&self, graph_annotation_id: String) -> Result<GraphAnnotation> {
        self.request(
            Method::DELETE,
            format!("/api/v0/graph-annotations/{}", graph_annotation_id),
            vec![],
            client::empty_body(),
            |graph_annotation| graph_annotation,
        )
    }
}