This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
group.go
259 lines (224 loc) · 6.76 KB
/
group.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
package tat
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
)
// Group struct
type Group struct {
ID string `bson:"_id" json:"_id"`
Name string `bson:"name" json:"name"`
Description string `bson:"description" json:"description"`
Users []string `bson:"users" json:"users,omitempty"`
AdminUsers []string `bson:"adminUsers" json:"adminUsers,omitempty"`
DateCreation int64 `bson:"dateCreation" json:"dateCreation,omitempty"`
}
// GroupCriteria is used by List all Groups
type GroupCriteria struct {
Skip int
Limit int
IDGroup string
Name string
NameRegex string
Description string
DateMinCreation string
DateMaxCreation string
UserUsername string
SortBy string
}
// CacheKey returns cacke key value
func (g *GroupCriteria) CacheKey() []string {
var s = []string{}
if g == nil {
return s
}
if g.Skip != 0 {
s = append(s, "skip="+strconv.Itoa(g.Skip))
}
if g.Limit != 0 {
s = append(s, "limit="+strconv.Itoa(g.Limit))
}
if g.IDGroup != "" {
s = append(s, "id_group="+g.IDGroup)
}
if g.Name != "" {
s = append(s, "name="+g.Name)
}
if g.NameRegex != "" {
s = append(s, "name_regex="+g.NameRegex)
}
if g.Description != "" {
s = append(s, "description="+g.Description)
}
if g.DateMinCreation != "" {
s = append(s, "date_min_creation="+g.DateMinCreation)
}
if g.DateMaxCreation != "" {
s = append(s, "date_max_creation="+g.DateMaxCreation)
}
if g.UserUsername != "" {
s = append(s, "user_username="+g.UserUsername)
}
if g.SortBy != "" {
s = append(s, "sort_by="+g.SortBy)
}
return s
}
// GroupsJSON is used by Tat Engine, for groups list
type GroupsJSON struct {
Count int `json:"count"`
Groups []Group `json:"groups"`
}
// ParamGroupUserJSON is used for add or remove user on a group
type ParamGroupUserJSON struct {
Groupname string `json:"groupname"`
Username string `json:"username"`
}
// GroupJSON contains name and description for a group
type GroupJSON struct {
Name string `json:"name" binding:"required"`
Description string `json:"description" binding:"required"`
}
// ParamTopicGroupJSON is used for manipulate a group on a topic
type ParamTopicGroupJSON struct {
Topic string `json:"topic"`
Groupname string `json:"groupname"`
Recursive bool `json:"recursive"`
}
// GroupList returns groups
func (c *Client) GroupList(criteria *GroupCriteria) (*GroupsJSON, error) {
if c == nil {
return nil, ErrClientNotInitiliazed
}
if criteria == nil {
criteria = &GroupCriteria{
Skip: 0,
Limit: 100,
}
}
v := url.Values{}
v.Set("skip", strconv.Itoa(criteria.Skip))
v.Set("limit", strconv.Itoa(criteria.Limit))
if criteria.IDGroup != "" {
v.Set("idGroup", criteria.IDGroup)
}
if criteria.Name != "" {
v.Set("name", criteria.Name)
}
if criteria.NameRegex != "" {
v.Set("nameRegex", criteria.NameRegex)
}
if criteria.Description != "" {
v.Set("description", criteria.Description)
}
if criteria.DateMinCreation != "" {
v.Set("dateMinCreation", criteria.DateMinCreation)
}
if criteria.DateMaxCreation != "" {
v.Set("dateMaxCreation", criteria.DateMaxCreation)
}
path := fmt.Sprintf("/groups?%s", v.Encode())
out, err := c.reqWant("GET", http.StatusOK, path, nil)
if err != nil {
ErrorLogFunc("Error while listing groups: %s", err)
return nil, err
}
groups := &GroupsJSON{}
if err := json.Unmarshal(out, groups); err != nil {
return nil, err
}
return groups, nil
}
//GroupCreate creates a group
func (c *Client) GroupCreate(g GroupJSON) (*Group, error) {
if c == nil {
return nil, ErrClientNotInitiliazed
}
b, err := json.Marshal(g)
if err != nil {
ErrorLogFunc("Error while marshal group: %s", err)
return nil, err
}
res, err := c.reqWant(http.MethodPost, http.StatusCreated, "/group", b)
if err != nil {
ErrorLogFunc("Error while marshal group for GroupCreate: %s", err)
return nil, err
}
DebugLogFunc("GroupCreate : %s", string(res))
newGroup := &Group{}
if err := json.Unmarshal(res, newGroup); err != nil {
return nil, err
}
return newGroup, nil
}
// GroupUpdate updates a group
func (c *Client) GroupUpdate(groupname, newGroupname, newDescription string) error {
m := GroupJSON{Name: newGroupname, Description: newDescription}
jsonStr, err := json.Marshal(m)
if err != nil {
return err
}
_, err = c.reqWant("PUT", http.StatusOK, "/group/edit/"+groupname, jsonStr)
if err != nil {
ErrorLogFunc("Error while updating group: %s", err)
return err
}
return nil
}
//GroupDelete delete a group
func (c *Client) GroupDelete(groupname string) error {
_, err := c.reqWant(http.MethodDelete, http.StatusOK, "/group/edit/"+groupname, nil)
if err != nil {
ErrorLogFunc("Error while deleting group: %s", err)
return err
}
return nil
}
// GroupAddUsers adds users on a group
func (c *Client) GroupAddUsers(groupname string, users []string) error {
return c.groupAddRemoveUsers("PUT", http.StatusCreated, "/group/add/user", groupname, users)
}
// GroupDeleteUsers deletes users from a group
func (c *Client) GroupDeleteUsers(groupname string, users []string) error {
return c.groupAddRemoveUsers("PUT", http.StatusOK, "/group/remove/user", groupname, users)
}
// GroupAddAdminUsers adds an admin user on a group
func (c *Client) GroupAddAdminUsers(groupname string, users []string) error {
return c.groupAddRemoveUsers("PUT", http.StatusCreated, "/group/add/adminuser", groupname, users)
}
// GroupDeleteAdminUsers removes admin users from a group
func (c *Client) GroupDeleteAdminUsers(groupname string, users []string) error {
return c.groupAddRemoveUsers("PUT", http.StatusOK, "/group/remove/adminuser", groupname, users)
}
func (c *Client) groupAddRemoveUsers(method string, httpStatus int, path, groupname string, users []string) error {
usersInError := map[string]string{}
for _, username := range users {
t := ParamGroupUserJSON{Groupname: groupname, Username: username}
b, err := json.Marshal(t)
if err != nil {
ErrorLogFunc("Error while marshal group: %s", err)
return err
}
_, err = c.reqWant(method, httpStatus, path, b)
// If an error is encountered while adding the current user, continue execution but keep trace of the user and its associated error
if err != nil {
usersInError[username] = err.Error()
continue
}
}
// If at least one user is in error, return an error
if len(usersInError) > 0 {
err := fmt.Errorf("errors on the following users were encountered\n")
// Log each user in error and its associated error
for key, value := range usersInError {
ErrorLogFunc("Error on user %s: %s", key, value)
err = fmt.Errorf("%sError on user %s: %s\n", err.Error(), key, value)
}
// Return all the errors as one error
return err
}
// If no users are in error everything went fine, return no errors
return nil
}