-
Notifications
You must be signed in to change notification settings - Fork 33
/
role_metadata.go
63 lines (55 loc) · 1.98 KB
/
role_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
// RoleMetaDataResp represents response for role metadata.
type RoleMetaDataResp struct {
RoleMetaData RoleMetaData
LastModified time.Time
}
// RoleMetaData represents role metadata body.
type RoleMetaData interface{}
// GetRoleMetaData gets a role metadata.
func (c *Client) GetRoleMetaData(serviceName, roleName, namespace string) (*RoleMetaDataResp, error) {
path := fmt.Sprintf("/api/v0/services/%s/roles/%s/metadata/%s", serviceName, roleName, 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 &RoleMetaDataResp{RoleMetaData: *metadata, LastModified: lastModified}, nil
}
// GetRoleMetaDataNameSpaces fetches namespaces of role metadata.
func (c *Client) GetRoleMetaDataNameSpaces(serviceName, roleName string) ([]string, error) {
data, err := requestGet[struct {
MetaDatas []struct {
NameSpace string `json:"namespace"`
} `json:"metadata"`
}](c, fmt.Sprintf("/api/v0/services/%s/roles/%s/metadata", serviceName, roleName))
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
}
// PutRoleMetaData puts a role metadata.
func (c *Client) PutRoleMetaData(serviceName, roleName, namespace string, metadata RoleMetaData) error {
path := fmt.Sprintf("/api/v0/services/%s/roles/%s/metadata/%s", serviceName, roleName, namespace)
_, err := requestPut[any](c, path, metadata)
return err
}
// DeleteRoleMetaData deletes a role metadata.
func (c *Client) DeleteRoleMetaData(serviceName, roleName, namespace string) error {
path := fmt.Sprintf("/api/v0/services/%s/roles/%s/metadata/%s", serviceName, roleName, namespace)
_, err := requestDelete[any](c, path)
return err
}