forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.go
73 lines (60 loc) · 1.76 KB
/
role.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
64
65
66
67
68
69
70
71
72
73
package gapi
import (
"bytes"
"encoding/json"
"fmt"
)
type Role struct {
Version int64 `json:"version"`
UID string `json:"uid,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Global bool `json:"global"`
Permissions []Permission `json:"permissions,omitempty"`
}
type Permission struct {
Action string `json:"action"`
Scope string `json:"scope"`
}
// GetRole gets a role with permissions for the given UID. Available only in Grafana Enterprise 8.+.
func (c *Client) GetRole(uid string) (*Role, error) {
r := &Role{}
err := c.request("GET", buildURL(uid), nil, nil, r)
if err != nil {
return nil, err
}
return r, nil
}
// NewRole creates a new role with permissions. Available only in Grafana Enterprise 8.+.
func (c *Client) NewRole(role Role) (*Role, error) {
data, err := json.Marshal(role)
if err != nil {
return nil, err
}
r := &Role{}
err = c.request("POST", "/api/access-control/roles", nil, bytes.NewBuffer(data), &r)
if err != nil {
return nil, err
}
return r, err
}
// UpdateRole updates the role and permissions. Available only in Grafana Enterprise 8.+.
func (c *Client) UpdateRole(role Role) error {
data, err := json.Marshal(role)
if err != nil {
return err
}
err = c.request("PUT", buildURL(role.UID), nil, bytes.NewBuffer(data), nil)
return err
}
// DeleteRole deletes the role with it's permissions. Available only in Grafana Enterprise 8.+.
func (c *Client) DeleteRole(uid string, global bool) error {
qp := map[string][]string{
"global": {fmt.Sprint(global)},
}
return c.request("DELETE", buildURL(uid), qp, nil, nil)
}
func buildURL(uid string) string {
const rootURL = "/api/access-control/roles"
return fmt.Sprintf("%s/%s", rootURL, uid)
}