forked from kovetskiy/zabbixctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_users_groups.go
238 lines (202 loc) · 4.26 KB
/
handle_users_groups.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
package main
import (
"errors"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/zazab/hierr"
)
func handleUsersGroups(
zabbix *Zabbix,
config *Config,
args map[string]interface{},
) error {
var (
groups, pattern = parseSearchQuery(args["<pattern>"].([]string))
addUser, _ = args["--add"].(string)
removeUser, _ = args["--remove"].(string)
confirmation = !args["--noconfirm"].(bool)
table = tabwriter.NewWriter(os.Stdout, 1, 4, 2, ' ', 0)
)
var usersgroups []UserGroup
var err error
err = withSpinner(
":: Requesting information about users groups",
func() error {
usersgroups, err = getUsersGroups(zabbix, groups)
return err
},
)
if err != nil {
return hierr.Errorf(
err,
"can't obtain users groups %s", groups,
)
}
found := false
for _, group := range usersgroups {
names := []string{}
for _, user := range group.Users {
names = append(names, user.Alias)
}
line := fmt.Sprintf(
"%s\t%s\t%s",
group.GetStatus(), group.Name,
strings.Join(names, " "),
)
if pattern != "" && !matchPattern(pattern, line) {
continue
}
fmt.Fprintln(table, line)
found = true
}
table.Flush()
if !found || (addUser == "" && removeUser == "") {
return nil
}
switch {
case addUser != "":
if confirmation {
if !confirmAdding(addUser) {
return nil
}
}
user, err := getUser(zabbix, addUser)
if err != nil {
return hierr.Errorf(
err,
"can't obtain user '%s'", addUser,
)
}
err = withSpinner(
":: Requesting for adding user to specified groups",
func() error {
return zabbix.AddUserToGroups(usersgroups, user)
},
)
if err != nil {
return hierr.Errorf(
err,
"can't add user '%s' to specified users groups",
user.Alias,
)
}
case removeUser != "":
if confirmation {
if !confirmRemoving(removeUser) {
return nil
}
}
user, err := getUser(zabbix, removeUser)
if err != nil {
return hierr.Errorf(
err,
"can't obtain user '%s'", removeUser,
)
}
err = withSpinner(
":: Requesting for removing user from specified groups",
func() error {
return zabbix.RemoveUserFromGroups(usersgroups, user)
},
)
if err != nil {
return hierr.Errorf(
err,
"can't remove user '%s' from specified users groups",
removeUser,
)
}
}
return nil
}
func getUser(zabbix *Zabbix, username string) (User, error) {
users, err := zabbix.GetUsers(Params{
"search": Params{
"alias": username,
},
})
if err != nil {
return User{}, hierr.Errorf(
err, "can't obtain user with specified name",
)
}
if len(users) == 0 {
return User{}, errors.New("user with specified name not found")
}
return users[0], nil
}
func getUsersGroups(zabbix *Zabbix, groups []string) ([]UserGroup, error) {
var params Params
if len(groups) == 0 {
params = Params{
"selectUsers": "1",
}
} else {
params = Params{
"selectUsers": "1",
"search": Params{
"name": groups,
},
"searchWildcardsEnabled": "1",
}
}
usersgroups, err := zabbix.GetUsersGroups(params)
if err != nil {
return nil, hierr.Errorf(
err,
"can't obtain zabbix users groups",
)
}
var (
usersIdentifiers = []string{}
usersIdentifiersHash = map[string]User{}
)
for _, usersgroup := range usersgroups {
for _, user := range usersgroup.Users {
usersIdentifiers = append(usersIdentifiers, user.ID)
}
}
users, err := zabbix.GetUsers(
Params{
"userids": usersIdentifiers,
},
)
if err != nil {
return nil, hierr.Errorf(
err,
"can't obtain users from specified groups %q",
usersIdentifiers,
)
}
for _, user := range users {
usersIdentifiersHash[user.ID] = user
}
for _, usersgroup := range usersgroups {
for i, user := range usersgroup.Users {
usersgroup.Users[i] = usersIdentifiersHash[user.ID]
}
}
return usersgroups, nil
}
func confirmAdding(user string) bool {
var value string
fmt.Fprintf(
os.Stderr,
"\n:: Proceed with adding user %s to specified groups? [Y/n]: ",
user,
)
fmt.Scanln(&value)
return value == "" || value == "Y" || value == "y"
}
func confirmRemoving(user string) bool {
var value string
fmt.Fprintf(
os.Stderr,
"\n:: Proceed with removing user %s from specified groups? [Y/n]: ",
user,
)
fmt.Scanln(&value)
return value == "" || value == "Y" || value == "y"
}