forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org_users.go
59 lines (50 loc) · 1.49 KB
/
org_users.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
package gapi
import (
"bytes"
"encoding/json"
"fmt"
)
// OrgUser represents a Grafana org user.
type OrgUser struct {
OrgId int64 `json:"orgId"`
UserId int64 `json:"userId"`
Email string `json:"email"`
Login string `json:"login"`
Role string `json:"role"`
}
// OrgUsers fetches and returns the users for the org whose ID it's passed.
func (c *Client) OrgUsers(orgID int64) ([]OrgUser, error) {
users := make([]OrgUser, 0)
err := c.request("GET", fmt.Sprintf("/api/orgs/%d/users", orgID), nil, nil, &users)
if err != nil {
return users, err
}
return users, err
}
// AddOrgUser adds a user to an org with the specified role.
func (c *Client) AddOrgUser(orgID int64, user, role string) error {
dataMap := map[string]string{
"loginOrEmail": user,
"role": role,
}
data, err := json.Marshal(dataMap)
if err != nil {
return err
}
return c.request("POST", fmt.Sprintf("/api/orgs/%d/users", orgID), nil, bytes.NewBuffer(data), nil)
}
// UpdateOrgUser updates and org user.
func (c *Client) UpdateOrgUser(orgID, userID int64, role string) error {
dataMap := map[string]string{
"role": role,
}
data, err := json.Marshal(dataMap)
if err != nil {
return err
}
return c.request("PATCH", fmt.Sprintf("/api/orgs/%d/users/%d", orgID, userID), nil, bytes.NewBuffer(data), nil)
}
// RemoveOrgUser removes a user from an org.
func (c *Client) RemoveOrgUser(orgID, userID int64) error {
return c.request("DELETE", fmt.Sprintf("/api/orgs/%d/users/%d", orgID, userID), nil, nil, nil)
}