forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard_permissions.go
61 lines (52 loc) · 2.08 KB
/
dashboard_permissions.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
package gapi
import (
"bytes"
"encoding/json"
"fmt"
)
// DashboardPermission has information such as a dashboard, user, team, role and permission.
type DashboardPermission struct {
DashboardID int64 `json:"dashboardId"`
DashboardUID string `json:"uid"`
UserID int64 `json:"userId"`
TeamID int64 `json:"teamId"`
Role string `json:"role"`
IsFolder bool `json:"isFolder"`
Inherited bool `json:"inherited"`
// Permission levels are
// 1 = View
// 2 = Edit
// 4 = Admin
Permission int64 `json:"permission"`
PermissionName string `json:"permissionName"`
}
// DashboardPermissions fetches and returns the permissions for the dashboard whose ID it's passed.
func (c *Client) DashboardPermissions(id int64) ([]*DashboardPermission, error) {
permissions := make([]*DashboardPermission, 0)
err := c.request("GET", fmt.Sprintf("/api/dashboards/id/%d/permissions", id), nil, nil, &permissions)
return permissions, err
}
// UpdateDashboardPermissions remove existing permissions if items are not included in the request.
func (c *Client) UpdateDashboardPermissions(id int64, items *PermissionItems) error {
path := fmt.Sprintf("/api/dashboards/id/%d/permissions", id)
data, err := json.Marshal(items)
if err != nil {
return err
}
return c.request("POST", path, nil, bytes.NewBuffer(data), nil)
}
// DashboardPermissionsByUID fetches and returns the permissions for the dashboard whose UID it's passed.
func (c *Client) DashboardPermissionsByUID(uid string) ([]*DashboardPermission, error) {
permissions := make([]*DashboardPermission, 0)
err := c.request("GET", fmt.Sprintf("/api/dashboards/uid/%s/permissions", uid), nil, nil, &permissions)
return permissions, err
}
// UpdateDashboardPermissionsByUID remove existing permissions if items are not included in the request.
func (c *Client) UpdateDashboardPermissionsByUID(uid string, items *PermissionItems) error {
path := fmt.Sprintf("/api/dashboards/uid/%s/permissions", uid)
data, err := json.Marshal(items)
if err != nil {
return err
}
return c.request("POST", path, nil, bytes.NewBuffer(data), nil)
}