forked from sensu/sensu-go
/
user.go
174 lines (135 loc) · 3.74 KB
/
user.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package client
import (
"encoding/json"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/types"
)
var usersPath = CreateBasePath(coreAPIGroup, coreAPIVersion, "users")
// AddGroupToUser makes "username" a member of "group".
func (client *RestClient) AddGroupToUser(username, group string) error {
path := usersPath(username, "groups", group)
res, err := client.R().Put(path)
if err != nil {
return err
}
if res.StatusCode() >= 400 {
return UnmarshalError(res)
}
return nil
}
// CreateUser creates new check on configured Sensu instance
func (client *RestClient) CreateUser(user *types.User) error {
path := usersPath("")
res, err := client.R().SetBody(user).Post(path)
if err != nil {
return err
}
if res.StatusCode() >= 400 {
return UnmarshalError(res)
}
return nil
}
// DisableUser disables a user on configured Sensu instance
func (client *RestClient) DisableUser(username string) error {
path := usersPath(username)
res, err := client.R().Delete(path)
if err != nil {
return err
}
if res.StatusCode() >= 400 {
return UnmarshalError(res)
}
return nil
}
// FetchUser retrieve the given user
func (client *RestClient) FetchUser(username string) (*types.User, error) {
user := &types.User{}
path := usersPath(username)
res, err := client.R().Get(path)
if err != nil {
return nil, err
}
if res.StatusCode() >= 400 {
return nil, UnmarshalError(res)
}
err = json.Unmarshal(res.Body(), user)
return user, err
}
// ListUsers fetches all users from configured Sensu instance
func (client *RestClient) ListUsers(options *ListOptions) ([]corev2.User, error) {
var users []corev2.User
if err := client.List(usersPath(), &users, options); err != nil {
return users, err
}
return users, nil
}
// ReinstateUser reinstates a disabled user on configured Sensu instance
func (client *RestClient) ReinstateUser(username string) error {
path := usersPath(username, "reinstate")
res, err := client.R().Put(path)
if err != nil {
return err
}
if res.StatusCode() >= 400 {
return UnmarshalError(res)
}
return nil
}
// RemoveGroupFromUser removes "username" from the given "group".
func (client *RestClient) RemoveGroupFromUser(username, group string) error {
path := usersPath(username, "groups", group)
res, err := client.R().Delete(path)
if err != nil {
return err
}
if res.StatusCode() >= 400 {
return UnmarshalError(res)
}
return nil
}
// RemoveGroupsFromUser removes all the groups for "username".
func (client *RestClient) RemoveAllGroupsFromUser(username string) error {
path := usersPath(username, "groups")
res, err := client.R().Delete(path)
if err != nil {
return err
}
if res.StatusCode() >= 400 {
return UnmarshalError(res)
}
return nil
}
// SetGroupsForUser sets the groups for "username" to "groups".
func (client *RestClient) SetGroupsForUser(username string, groups []string) error {
// Note: Instead of the implementation below, we can have the backend
// support receiving a list of groups on /rbac/users/{username}/groups
// Start by removing all the existing groups
if err := client.RemoveAllGroupsFromUser(username); err != nil {
return err
}
// Then add each group in the set one by one
for _, group := range groups {
if err := client.AddGroupToUser(username, group); err != nil {
return err
}
}
return nil
}
// UpdatePassword updates password of given user on configured Sensu instance
func (client *RestClient) UpdatePassword(username, pwd string) error {
bytes, err := json.Marshal(map[string]string{"password": pwd})
if err != nil {
return err
}
path := usersPath(username, "password")
res, err := client.R().
SetBody(bytes).
Put(path)
if err != nil {
return err
}
if res.StatusCode() >= 400 {
return UnmarshalError(res)
}
return nil
}