forked from grafana-tools/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-admin.go
63 lines (58 loc) · 1.43 KB
/
rest-admin.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
package sdk
import (
"encoding/json"
"fmt"
)
// CreateUser creates a new global user
// Only work with Basic Authentication
// It reflects POST /api/admin/users
func (r *Client) CreateUser(user User) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
if raw, err = json.Marshal(user); err != nil {
return StatusMessage{}, err
}
if raw, _, err = r.post("api/admin/users", nil, raw); err != nil {
return StatusMessage{}, err
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, err
}
return resp, nil
}
func (r *Client) CreateUserPermission(uid uint, isAdmin bool) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
var data Permission
data.IsGrafanaAdmin = isAdmin
if raw, err = json.Marshal(data); err != nil {
return StatusMessage{}, err
}
if raw, _, err = r.put(fmt.Sprintf("api/admin/users/%d/permissions", uid), nil, raw); err != nil {
return StatusMessage{}, err
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, err
}
return resp, nil
}
func (r *Client) SwitchUserContext(uid uint, oid uint) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
if raw, _, err = r.post(fmt.Sprintf("/api/users/%d/using/%d", uid, oid), nil, raw); err != nil {
return StatusMessage{}, err
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, err
}
return resp, nil
}