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

/// A metadata
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Metadata {
    pub namespace: String,
}

#[derive(Deserialize)]
struct ListMetadataResponse {
    metadata: Vec<Metadata>,
}

impl client::Client {
    /// Retrieves a host metadata.
    ///
    /// See https://mackerel.io/api-docs/entry/metadata#get.
    pub fn get_metadata(&self, host_id: &str, namespace: &str) -> Result<serde_json::Value> {
        self.request(
            Method::GET,
            format!("/api/v0/hosts/{}/metadata/{}", host_id, namespace),
            vec![],
            client::empty_body(),
            |res| res,
        )
    }

    /// Creates/Updatates a host metadata.
    ///
    /// See https://mackerel.io/api-docs/entry/metadata#put.
    pub fn put_metadata(
        &self,
        host_id: &str,
        namespace: &str,
        metadata: serde_json::Value,
    ) -> Result<()> {
        self.request(
            Method::PUT,
            format!("/api/v0/hosts/{}/metadata/{}", host_id, namespace),
            vec![],
            Some(metadata),
            |_: serde_json::Value| (),
        )
    }

    /// Deletes a host metadata.
    ///
    /// See https://mackerel.io/api-docs/entry/metadata#delete.
    pub fn delete_metadata(&self, host_id: &str, namespace: &str) -> Result<()> {
        self.request(
            Method::DELETE,
            format!("/api/v0/hosts/{}/metadata/{}", host_id, namespace),
            vec![],
            client::empty_body(),
            |_: serde_json::Value| (),
        )
    }

    /// Lists host metadata.
    ///
    /// See https://mackerel.io/api-docs/entry/metadata#list.
    pub fn list_metadata(&self, host_id: &str) -> Result<Vec<Metadata>> {
        self.request(
            Method::GET,
            format!("/api/v0/hosts/{}/metadata", host_id),
            vec![],
            client::empty_body(),
            |res: ListMetadataResponse| res.metadata,
        )
    }
}