-
Notifications
You must be signed in to change notification settings - Fork 4
/
users.go
117 lines (93 loc) · 2.61 KB
/
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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package grafana
import (
"fmt"
)
type UsersService struct {
client *Client
}
type Users []User
type User struct {
ID int `json:"id,omitempty"`
Email string `json:"email,omitempty"`
Name string `json:"name,omitempty"`
Login string `json:"login,omitempty"`
Theme string `json:"theme,omitempty"`
OrgID int `json:"orgId,omitempty"`
IsGrafanaAdmin bool `json:"isGrafanaAdmin,omitempty"`
}
func (s *UsersService) GetUserByID(id int) (*User, *Response, error) {
u := fmt.Sprintf("users/%d", id)
req, err := s.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
usr := new(User)
resp, err := s.client.Do(req, usr)
if err != nil {
return nil, resp, err
}
return usr, resp, err
}
func (s *UsersService) GetUserByLoginOrEmail(id string) (*User, *Response, error) {
u := fmt.Sprintf("users/lookup?loginOrEmail=%s", id)
req, err := s.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
usr := new(User)
resp, err := s.client.Do(req, usr)
if err != nil {
return nil, resp, err
}
return usr, resp, err
}
type CreateUserOptions struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Login string `json:"login,omitempty"`
Password string `json:"password,omitempty"`
}
//must be authenticated as admin to do this
func (s *UsersService) CreateUser(opt *CreateUserOptions) (*Response, error) {
req, err := s.client.NewRequest("POST", "admin/users", opt, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
type UpdateUserOptions struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Login string `json:"login,omitempty"`
Theme string `json:"theme,omitempty"`
}
func (s *UsersService) UpdateUserByID(id int, opt *UpdateUserOptions) (*Response, error) {
u := fmt.Sprintf("users/%d", id)
req, err := s.client.NewRequest("PUT", u, opt, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
//must be authenticated as admin to do this
func (s *UsersService) DeleteUserByID(id int) (*Response, error) {
u := fmt.Sprintf("admin/users/%d", id)
req, err := s.client.NewRequest("DELETE", u, nil, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
func (s *UsersService) GetUserTeams(id int) (*Teams, *Response, error) {
u := fmt.Sprintf("users/%d/teams", id)
req, err := s.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
t := new(Teams)
resp, err := s.client.Do(req, t)
if err != nil {
return nil, resp, err
}
return t, resp, err
}