forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_permissions.go
83 lines (72 loc) · 2.83 KB
/
resource_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package gapi
import (
"encoding/json"
"fmt"
)
type ResourcePermission struct {
ID int64 `json:"id"`
RoleName string `json:"roleName"`
IsManaged bool `json:"isManaged"`
IsInherited bool `json:"isInherited"`
IsServiceAccount bool `json:"isServiceAccount"`
UserID int64 `json:"userId,omitempty"`
UserLogin string `json:"userLogin,omitempty"`
Team string `json:"team,omitempty"`
TeamID int64 `json:"teamId,omitempty"`
BuiltInRole string `json:"builtInRole,omitempty"`
Actions []string `json:"actions"`
Permission string `json:"permission"`
}
type SetResourcePermissionsBody struct {
Permissions []SetResourcePermissionItem `json:"permissions"`
}
type SetResourcePermissionBody struct {
Permission SetResourcePermissionItem `json:"permission"`
}
type SetResourcePermissionItem struct {
UserID int64 `json:"userId,omitempty"`
TeamID int64 `json:"teamId,omitempty"`
BuiltinRole string `json:"builtInRole,omitempty"`
Permission string `json:"permission"`
}
type SetResourcePermissionsResponse struct {
Message string `json:"message"`
}
func (c *Client) listResourcePermissions(resource string, ident ResourceIdent) ([]*ResourcePermission, error) {
path := fmt.Sprintf("/api/access-control/%s/%s", resource, ident.String())
result := make([]*ResourcePermission, 0)
if err := c.request("GET", path, nil, nil, &result); err != nil {
return nil, fmt.Errorf("error getting %s resource permissions at %s: %w", resource, path, err)
}
return result, nil
}
func (c *Client) setResourcePermissions(resource string, ident ResourceIdent, body SetResourcePermissionsBody) (*SetResourcePermissionsResponse, error) {
path := fmt.Sprintf("/api/access-control/%s/%s", resource, ident.String())
data, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("marshal err: %w", err)
}
result := SetResourcePermissionsResponse{}
if err := c.request("POST", path, nil, data, &result); err != nil {
return nil, fmt.Errorf("error setting %s resource permissions at %s: %w", resource, path, err)
}
return &result, nil
}
func (c *Client) setResourcePermissionByAssignment(
resource string,
ident ResourceIdent,
assignmentKind string,
assignmentIdent ResourceIdent,
body SetResourcePermissionBody,
) (*SetResourcePermissionsResponse, error) {
path := fmt.Sprintf("/api/access-control/%s/%s/%s/%s", resource, ident.String(), assignmentKind, assignmentIdent.String())
data, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("marshal err: %w", err)
}
result := SetResourcePermissionsResponse{}
if err := c.request("POST", path, nil, data, &result); err != nil {
return nil, fmt.Errorf("error setting %s resource permissions for %s at %s: %w", resource, assignmentKind, path, err)
}
return &result, nil
}