This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroups_service.go
314 lines (287 loc) · 6.6 KB
/
groups_service.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package databricks
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// GroupsService is a service for interacting with the DBFS.
type GroupsService struct {
client Client
}
// AddMember adds a user or group to a group. This call returns an error
// RESOURCE_DOES_NOT_EXIST if a user or group with the given name does not
// exist, or if a group with the given parent name does not exist.
func (s *GroupsService) AddMember(
ctx context.Context,
userName, groupName, parentName string,
) error {
raw, err := json.Marshal(struct {
UserName string `json:"user_name,omitempty"`
GroupName string `json:"group_name,omitempty"`
ParentName string `json:"parent_name"`
}{
userName,
groupName,
parentName,
})
if err != nil {
return err
}
req, err := http.NewRequest(
http.MethodPost,
s.client.url+"2.0/groups/add-member",
bytes.NewBuffer(raw),
)
if err != nil {
return err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return fmt.Errorf(
"Failed to returns 2XX response: %d", res.StatusCode)
}
return nil
}
// Create a new group with the given name. This call returns an error
// RESOURCE_ALREADY_EXISTS if a group with the given name already exists.
func (s *GroupsService) Create(
ctx context.Context,
groupName string,
) error {
raw, err := json.Marshal(struct {
GroupName string `json:"group_name"`
}{
groupName,
})
if err != nil {
return err
}
req, err := http.NewRequest(
http.MethodPost,
s.client.url+"2.0/groups/create",
bytes.NewBuffer(raw),
)
if err != nil {
return err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return fmt.Errorf(
"Failed to returns 2XX response: %d", res.StatusCode)
}
return nil
}
// Members returns all of the members of a particular group. This call returns
// an error RESOURCE_DOES_NOT_EXIST if a group with the given name does not
// exist.
func (s *GroupsService) Members(
ctx context.Context,
groupName string,
) ([]PrincipalName, error) {
req, err := http.NewRequest(
http.MethodGet,
s.client.url+"2.0/groups/list-members",
nil,
)
if err != nil {
return []PrincipalName{}, err
}
req = req.WithContext(ctx)
q := req.URL.Query()
q.Add("group_name", groupName)
req.URL.RawQuery = q.Encode()
res, err := s.client.client.Do(req)
if err != nil {
return []PrincipalName{}, err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return []PrincipalName{}, fmt.Errorf(
"Failed to returns 2XX response: %d", res.StatusCode)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
membersRes := struct {
Members []PrincipalName
}{[]PrincipalName{}}
err = decoder.Decode(&membersRes)
return membersRes.Members, err
}
// Groups returns all of the groups in an organization.
func (s *GroupsService) Groups(
ctx context.Context,
) ([]string, error) {
req, err := http.NewRequest(
http.MethodGet,
s.client.url+"2.0/groups/list",
nil,
)
if err != nil {
return []string{}, err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return []string{}, err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return []string{}, fmt.Errorf(
"Failed to returns 2XX response: %d", res.StatusCode)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
groupRes := struct {
GroupNames []string `json:"group_names"`
}{[]string{}}
err = decoder.Decode(&groupRes)
return groupRes.GroupNames, err
}
// UserParents returns all of the Parent groups of a user.
func (s *GroupsService) UserParents(
ctx context.Context,
userName string,
) ([]string, error) {
return s.parents(ctx, "", userName)
}
// GroupParents returns all of the Parent groups of a group.
func (s *GroupsService) GroupParents(
ctx context.Context,
groupName string,
) ([]string, error) {
return s.parents(ctx, groupName, "")
}
func (s *GroupsService) parents(
ctx context.Context,
groupName, userName string,
) ([]string, error) {
if len(groupName) > 0 && len(userName) > 0 {
return []string{}, fmt.Errorf(
"Must specify either group_name OR user_name")
}
req, err := http.NewRequest(
http.MethodGet,
s.client.url+"2.0/groups/list-parents",
nil,
)
if err != nil {
return []string{}, err
}
req = req.WithContext(ctx)
q := req.URL.Query()
if len(groupName) > 0 {
q.Add("group_name", groupName)
}
if len(userName) > 0 {
q.Add("user_name", userName)
}
req.URL.RawQuery = q.Encode()
res, err := s.client.client.Do(req)
if err != nil {
return []string{}, err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return []string{}, fmt.Errorf(
"Failed to returns 2XX response: %d", res.StatusCode)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
groupRes := struct {
GroupNames []string `json:"group_names"`
}{[]string{}}
err = decoder.Decode(&groupRes)
return groupRes.GroupNames, err
}
// RemoveUser removes a User from a group.
func (s *GroupsService) RemoveUser(
ctx context.Context,
userName string,
) error {
raw, err := json.Marshal(struct {
UserName string `json:"user_name"`
}{
userName,
})
if err != nil {
return err
}
return s.remove(ctx, raw)
}
// RemoveGroup removes a Group from a group.
func (s *GroupsService) RemoveGroup(
ctx context.Context,
groupName string,
) error {
raw, err := json.Marshal(struct {
GroupName string `json:"group_name"`
}{
groupName,
})
if err != nil {
return err
}
return s.remove(ctx, raw)
}
func (s *GroupsService) remove(
ctx context.Context,
raw []byte,
) error {
req, err := http.NewRequest(
http.MethodPost,
s.client.url+"2.0/groups/remove-member",
bytes.NewBuffer(raw),
)
if err != nil {
return err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return fmt.Errorf(
"Failed to returns 2XX response: %d", res.StatusCode)
}
return nil
}
// Delete is used to delete a group.
func (s *GroupsService) Delete(
ctx context.Context,
groupName string,
) error {
raw, err := json.Marshal(struct {
GroupName string `json:"group_name"`
}{
groupName,
})
if err != nil {
return err
}
req, err := http.NewRequest(
http.MethodPost,
s.client.url+"2.0/groups/delete",
bytes.NewBuffer(raw),
)
if err != nil {
return err
}
req = req.WithContext(ctx)
res, err := s.client.client.Do(req)
if err != nil {
return err
}
if res.StatusCode >= 300 || res.StatusCode <= 199 {
return fmt.Errorf(
"Failed to returns 2XX response: %d", res.StatusCode)
}
return nil
}