forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource_permissions.go
95 lines (79 loc) · 3.13 KB
/
datasource_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
84
85
86
87
88
89
90
91
92
93
94
95
package gapi
import (
"bytes"
"encoding/json"
"fmt"
)
type DatasourcePermissionType int
const (
_ DatasourcePermissionType = iota // 0 is not a valid permission
DatasourcePermissionQuery
)
// DatasourcePermission has information such as a datasource, user, team, role and permission.
type DatasourcePermission struct {
ID int64 `json:"id"`
DatasourceID int64 `json:"datasourceId"`
UserID int64 `json:"userId"`
UserEmail string `json:"userEmail"`
TeamID int64 `json:"teamId"`
// Permission levels are
// 1 = Query
Permission DatasourcePermissionType `json:"permission"`
PermissionName string `json:"permissionName"`
}
type DatasourcePermissionsResponse struct {
DatasourceID int64 `json:"datasourceId"`
Enabled bool `json:"enabled"`
Permissions []*DatasourcePermission `json:"permissions"`
}
type DatasourcePermissionAddPayload struct {
UserID int64 `json:"userId"`
TeamID int64 `json:"teamId"`
Permission DatasourcePermissionType `json:"permission"`
}
// EnableDatasourcePermissions enables the datasource permissions (this is a datasource setting)
func (c *Client) EnableDatasourcePermissions(id int64) error {
path := fmt.Sprintf("/api/datasources/%d/enable-permissions", id)
if err := c.request("POST", path, nil, nil, nil); err != nil {
return fmt.Errorf("error enabling permissions at %s: %w", path, err)
}
return nil
}
// DisableDatasourcePermissions disables the datasource permissions (this is a datasource setting)
func (c *Client) DisableDatasourcePermissions(id int64) error {
path := fmt.Sprintf("/api/datasources/%d/disable-permissions", id)
if err := c.request("POST", path, nil, nil, nil); err != nil {
return fmt.Errorf("error disabling permissions at %s: %w", path, err)
}
return nil
}
// DatasourcePermissions fetches and returns the permissions for the datasource whose ID it's passed.
func (c *Client) DatasourcePermissions(id int64) (*DatasourcePermissionsResponse, error) {
path := fmt.Sprintf("/api/datasources/%d/permissions", id)
var out *DatasourcePermissionsResponse
err := c.request("GET", path, nil, nil, &out)
if err != nil {
return out, fmt.Errorf("error getting permissions at %s: %w", path, err)
}
return out, nil
}
// AddDatasourcePermission adds the given permission item
func (c *Client) AddDatasourcePermission(id int64, item *DatasourcePermissionAddPayload) error {
path := fmt.Sprintf("/api/datasources/%d/permissions", id)
data, err := json.Marshal(item)
if err != nil {
return fmt.Errorf("marshal err: %w", err)
}
if err = c.request("POST", path, nil, bytes.NewBuffer(data), nil); err != nil {
return fmt.Errorf("error adding permissions at %s: %w", path, err)
}
return nil
}
// RemoveDatasourcePermission removes the permission with the given id
func (c *Client) RemoveDatasourcePermission(id, permissionID int64) error {
path := fmt.Sprintf("/api/datasources/%d/permissions/%d", id, permissionID)
if err := c.request("DELETE", path, nil, nil, nil); err != nil {
return fmt.Errorf("error deleting permissions at %s: %w", path, err)
}
return nil
}