/
GroupCommand.java
305 lines (246 loc) · 11 KB
/
GroupCommand.java
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
package org.hyperic.hq.hqapi1.tools;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import org.hyperic.hq.hqapi1.GroupApi;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.XmlUtil;
import org.hyperic.hq.hqapi1.types.Group;
import org.hyperic.hq.hqapi1.types.GroupsResponse;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.GroupResponse;
import org.hyperic.hq.hqapi1.types.ResponseStatus;
import org.hyperic.hq.hqapi1.types.ResourcesResponse;
import org.hyperic.hq.hqapi1.types.ResourcePrototypeResponse;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.ResourceResponse;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Collection;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class GroupCommand extends Command {
private static String CMD_LIST = "list";
private static String CMD_SYNC = "sync";
private static String CMD_DELETE = "delete";
private static String[] COMMANDS = { CMD_LIST, CMD_SYNC, CMD_DELETE };
private static String OPT_ID = "id";
private static String OPT_COMPAT = "compatible";
private static String OPT_MIXED = "mixed";
// Additional sync commands when syncing via command line options.
private static String OPT_NAME = "name";
private static String OPT_PROTOTYPE = "prototype";
private static String OPT_PLATFORM = "platform";
private static String OPT_REGEX = "regex";
private static String OPT_DELETEMISSING = "deleteMissing";
private static String OPT_DESC = "description";
private static String OPT_CHILDREN = "children";
private static String OPT_DELETE = "delete";
private void printUsage() {
System.err.println("One of " + Arrays.toString(COMMANDS) + " required");
}
protected void handleCommand(String[] args) throws Exception {
if (args.length == 0) {
printUsage();
System.exit(-1);
}
if (args[0].equals(CMD_LIST)) {
list(trim(args));
} else if (args[0].equals(CMD_SYNC)) {
sync(trim(args));
} else if (args[0].equals(CMD_DELETE)) {
delete(trim(args));
} else {
printUsage();
System.exit(-1);
}
}
private void list(String[] args) throws Exception {
OptionParser p = getOptionParser();
p.accepts(OPT_COMPAT, "List only compatible groups");
p.accepts(OPT_MIXED, "List only mixed groups");
OptionSet options = getOptions(p, args);
HQApi api = getApi(options);
GroupApi groupApi = api.getGroupApi();
if (options.has(OPT_COMPAT) && options.has(OPT_MIXED)) {
System.err.println("Only one of " + OPT_COMPAT + " and " + OPT_MIXED +
" is allowed.");
System.exit(-1);
}
GroupsResponse groups;
if (options.has(OPT_COMPAT)) {
groups = groupApi.getCompatibleGroups();
} else if (options.has(OPT_MIXED)) {
groups = groupApi.getMixedGroups();
} else {
groups = groupApi.getGroups();
}
XmlUtil.serialize(groups, System.out, Boolean.TRUE);
}
private void sync(String[] args) throws Exception {
OptionParser p = getOptionParser();
p.accepts(OPT_NAME, "The group name to sync").
withRequiredArg().ofType(String.class);
p.accepts(OPT_PROTOTYPE, "The resource type to query for group membership").
withRequiredArg().ofType(String.class);
p.accepts(OPT_PLATFORM, "The platform to query for group membership").
withRequiredArg().ofType(String.class);
p.accepts(OPT_REGEX, "The regular expression to apply to the " + OPT_PROTOTYPE +
" flag").withRequiredArg().ofType(String.class);
p.accepts(OPT_DELETEMISSING, "Remove resources in the group not included in " +
"the " + OPT_PROTOTYPE + " and " + OPT_REGEX);
p.accepts(OPT_COMPAT, "If specified, attempt to make the group compatible");
p.accepts(OPT_DESC, "If specified, set the description for the group").
withRequiredArg().ofType(String.class);
p.accepts(OPT_CHILDREN, "If specified, include child resources of the " +
"specified prototype and regex");
p.accepts(OPT_DELETE, "If specifed, remove the specified resources from " +
"the given group");
OptionSet options = getOptions(p, args);
if (options.hasArgument(OPT_NAME)) {
syncViaCommandLineArgs(options);
return;
}
HQApi api = getApi(options);
GroupApi groupApi = api.getGroupApi();
InputStream is = getInputStream(options);
GroupsResponse resp = XmlUtil.deserialize(GroupsResponse.class, is);
List<Group> groups = resp.getGroup();
GroupsResponse syncResponse = groupApi.syncGroups(groups);
checkSuccess(syncResponse);
System.out.println("Successfully synced " + groups.size() + " groups.");
}
// Helper function to unroll a resource and it's children into a single list.
private Map<Integer,Resource> getFlattenResources(Collection<Resource> resources) {
Map<Integer,Resource> result = new HashMap<Integer,Resource>();
for (Resource r : resources) {
result.put(r.getId(), r);
if (r.getResource().size() > 0) {
result.putAll(getFlattenResources(r.getResource()));
}
}
return result;
}
private void syncViaCommandLineArgs(OptionSet s) throws Exception
{
// Required args
String name = (String)getRequired(s, OPT_NAME);
String prototype = (String)s.valueOf(OPT_PROTOTYPE);
String platform = (String)s.valueOf(OPT_PLATFORM);
// Optional
String regex = (String)s.valueOf(OPT_REGEX);
boolean deleteMissing = s.has(OPT_DELETEMISSING);
boolean compatible = s.has(OPT_COMPAT);
boolean children = s.has(OPT_CHILDREN);
boolean delete = s.has(OPT_DELETE);
HQApi api = getApi(s);
Map<Integer,Resource> resources = new HashMap<Integer,Resource>();
if (prototype != null && platform != null) {
System.err.println("Only one of " + OPT_PROTOTYPE + " or " +
OPT_PLATFORM + " is allowed.");
return;
}
if (prototype != null) {
// Get prototype
ResourcePrototypeResponse protoResponse =
api.getResourceApi().getResourcePrototype(prototype);
checkSuccess(protoResponse);
// Query resources
ResourcesResponse resourcesResponse = api.getResourceApi().
getResources(protoResponse.getResourcePrototype(), false, children);
checkSuccess(resourcesResponse);
for (Resource r : resourcesResponse.getResource()) {
resources.put(r.getId(), r);
}
} else if (platform != null) {
ResourceResponse resourceResponse = api.getResourceApi().
getPlatformResource(platform, false, children);
checkSuccess(resourceResponse);
resources.put(resourceResponse.getResource().getId(),
resourceResponse.getResource());
} else {
System.err.println("One of " + OPT_PROTOTYPE + " or " +
OPT_PLATFORM + " is required.");
return;
}
// Filter based on regex, if given.
if (regex != null) {
Pattern pattern = Pattern.compile(regex);
for (Iterator<Resource> i = resources.values().iterator(); i.hasNext(); ) {
Resource r = i.next();
Matcher m = pattern.matcher(r.getName());
if (!m.matches()) {
i.remove();
}
}
}
System.out.println(name + ": Found " + resources.size() + " matching resources");
// Check for existing group
Group group;
GroupResponse groupResponse = api.getGroupApi().getGroup(name);
if (groupResponse.getStatus().equals(ResponseStatus.SUCCESS)) {
group = groupResponse.getGroup();
System.out.println(name + ": Syncing existing group (" +
group.getResource().size() + " members)");
if (deleteMissing) {
System.out.println(name + ": Clearing existing members");
group.getResource().clear();
}
} else {
if (delete) {
System.err.println("Option " + OPT_DELETE + " not applicable for " +
"new groups");
return;
}
group = new Group();
group.setName(name);
if (prototype != null && compatible) {
ResourcePrototypeResponse protoResponse =
api.getResourceApi().getResourcePrototype(prototype);
checkSuccess(protoResponse);
group.setResourcePrototype(protoResponse.getResourcePrototype());
}
System.out.println(name + ": Creating new group");
}
if (s.hasArgument(OPT_DESC)) {
group.setDescription((String)s.valueOf(OPT_DESC));
}
Map<Integer,Resource> flattenedResources = getFlattenResources(resources.values());
if (delete) {
for(Iterator<Resource> i = group.getResource().iterator(); i.hasNext();) {
Resource r = i.next();
if (flattenedResources.containsKey(r.getId())) {
i.remove();
}
}
} else {
// TODO: could be more efficent here, server side will prune dups
group.getResource().addAll(flattenedResources.values());
}
List<Group> groups = new ArrayList<Group>();
groups.add(group);
GroupsResponse syncResponse = api.getGroupApi().syncGroups(groups);
checkSuccess(syncResponse);
System.out.println(name + ": Success (" +
syncResponse.getGroup().get(0).getResource().size() + " members)");
}
private void delete(String[] args) throws Exception {
OptionParser p = getOptionParser();
p.accepts(OPT_ID, "The resource id to delete").
withRequiredArg().ofType(Integer.class);
OptionSet options = getOptions(p, args);
if (!options.has(OPT_ID)) {
System.err.println("Required argument " + OPT_ID + " not given");
System.exit(-1);
}
GroupApi groupApi = getApi(options).getGroupApi();
Integer id = (Integer)options.valueOf(OPT_ID);
StatusResponse response = groupApi.deleteGroup(id);
checkSuccess(response);
System.out.println("Successfully deleted group id " + id);
}
}