forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_role_assignments.go
58 lines (47 loc) · 1.55 KB
/
builtin_role_assignments.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
package gapi
import (
"bytes"
"encoding/json"
"fmt"
)
const baseURL = "/api/access-control/builtin-roles"
type BuiltInRoleAssignment struct {
BuiltinRole string `json:"builtInRole"`
RoleUID string `json:"roleUid"`
Global bool `json:"global"`
}
// GetBuiltInRoleAssignments gets all built-in role assignments. Available only in Grafana Enterprise 8.+.
func (c *Client) GetBuiltInRoleAssignments() (map[string][]*Role, error) {
br := make(map[string][]*Role)
err := c.request("GET", baseURL, nil, nil, &br)
if err != nil {
return nil, err
}
return br, nil
}
// NewBuiltInRoleAssignment creates a new built-in role assignment. Available only in Grafana Enterprise 8.+.
func (c *Client) NewBuiltInRoleAssignment(builtInRoleAssignment BuiltInRoleAssignment) (*BuiltInRoleAssignment, error) {
body, err := json.Marshal(builtInRoleAssignment)
if err != nil {
return nil, err
}
br := &BuiltInRoleAssignment{}
err = c.request("POST", baseURL, nil, bytes.NewBuffer(body), &br)
if err != nil {
return nil, err
}
return br, err
}
// DeleteBuiltInRoleAssignment remove the built-in role assignments. Available only in Grafana Enterprise 8.+.
func (c *Client) DeleteBuiltInRoleAssignment(builtInRole BuiltInRoleAssignment) error {
data, err := json.Marshal(builtInRole)
if err != nil {
return err
}
qp := map[string][]string{
"global": {fmt.Sprint(builtInRole.Global)},
}
url := fmt.Sprintf("%s/%s/roles/%s", baseURL, builtInRole.BuiltinRole, builtInRole.RoleUID)
err = c.request("DELETE", url, qp, bytes.NewBuffer(data), nil)
return err
}