-
Notifications
You must be signed in to change notification settings - Fork 33
/
host_metadata.go
63 lines (55 loc) · 1.82 KB
/
host_metadata.go
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
package mackerel
import (
"fmt"
"net/http"
"time"
)
// https://mackerel.io/ja/api-docs/entry/metadata
// HostMetaDataResp represents response for host metadata.
type HostMetaDataResp struct {
HostMetaData HostMetaData
LastModified time.Time
}
// HostMetaData represents host metadata body.
type HostMetaData interface{}
// GetHostMetaData gets a host metadata.
func (c *Client) GetHostMetaData(hostID, namespace string) (*HostMetaDataResp, error) {
path := fmt.Sprintf("/api/v0/hosts/%s/metadata/%s", hostID, namespace)
metadata, header, err := requestGetAndReturnHeader[HostMetaData](c, path)
if err != nil {
return nil, err
}
lastModified, err := http.ParseTime(header.Get("Last-Modified"))
if err != nil {
return nil, err
}
return &HostMetaDataResp{HostMetaData: *metadata, LastModified: lastModified}, nil
}
// GetHostMetaDataNameSpaces fetches namespaces of host metadata.
func (c *Client) GetHostMetaDataNameSpaces(hostID string) ([]string, error) {
data, err := requestGet[struct {
MetaDatas []struct {
NameSpace string `json:"namespace"`
} `json:"metadata"`
}](c, fmt.Sprintf("/api/v0/hosts/%s/metadata", hostID))
if err != nil {
return nil, err
}
namespaces := make([]string, len(data.MetaDatas))
for i, metadata := range data.MetaDatas {
namespaces[i] = metadata.NameSpace
}
return namespaces, nil
}
// PutHostMetaData puts a host metadata.
func (c *Client) PutHostMetaData(hostID, namespace string, metadata HostMetaData) error {
path := fmt.Sprintf("/api/v0/hosts/%s/metadata/%s", hostID, namespace)
_, err := requestPut[any](c, path, metadata)
return err
}
// DeleteHostMetaData deletes a host metadata.
func (c *Client) DeleteHostMetaData(hostID, namespace string) error {
path := fmt.Sprintf("/api/v0/hosts/%s/metadata/%s", hostID, namespace)
_, err := requestDelete[any](c, path)
return err
}