Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add command line group sync ala mass [HHQ-2870]

  • Loading branch information...
commit a7c754a0ad5b3b74861430c6fc115de010d55f6d 1 parent 1205aac
Ryan Morgan authored
Showing with 101 additions and 0 deletions.
  1. +101 −0 src/org/hyperic/hq/hqapi1/tools/GroupCommand.java
View
101 src/org/hyperic/hq/hqapi1/tools/GroupCommand.java
@@ -8,10 +8,19 @@
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 java.io.InputStream;
import java.util.Arrays;
import java.util.List;
+import java.util.Iterator;
+import java.util.ArrayList;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
public class GroupCommand extends Command {
@@ -25,6 +34,12 @@
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_REGEX = "regex";
+ private static String OPT_DELETEMISSING = "deleteMissing";
+
private void printUsage() {
System.err.println("One of " + Arrays.toString(COMMANDS) + " required");
}
@@ -81,8 +96,24 @@ private void list(String[] args) throws Exception {
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_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");
+
OptionSet options = getOptions(p, args);
+ if (options.hasArgument(OPT_NAME)) {
+ syncViaCommandLineArgs(options);
+ return;
+ }
+
HQApi api = getApi(options);
GroupApi groupApi = api.getGroupApi();
@@ -98,6 +129,76 @@ private void sync(String[] args) throws Exception {
System.out.println("Successfully synced " + groups.size() + " groups.");
}
+ private void syncViaCommandLineArgs(OptionSet s) throws Exception
+ {
+ // Required args
+ String name = (String)getRequired(s, OPT_NAME);
+ String prototype = (String)getRequired(s, OPT_PROTOTYPE);
+
+ // Optional
+ String regex = (String)s.valueOf(OPT_REGEX);
+ boolean deleteMissing = s.has(OPT_DELETEMISSING);
+ boolean compatible = s.has(OPT_COMPAT);
+
+ HQApi api = getApi(s);
+
+ // Get prototype
+ ResourcePrototypeResponse protoResponse =
+ api.getResourceApi().getResourcePrototype(prototype);
+ checkSuccess(protoResponse);
+
+ // Query resources
+ ResourcesResponse resourceResponse = api.getResourceApi().
+ getResources(protoResponse.getResourcePrototype(), false, false);
+ checkSuccess(resourceResponse);
+
+ List<Resource> resources = resourceResponse.getResource();
+
+ // Filter based on regex, if given.
+ if (regex != null) {
+ Pattern pattern = Pattern.compile(regex);
+ for (Iterator<Resource> i = resources.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 {
+ group = new Group();
+ group.setName(name);
+ if (compatible) {
+ group.setResourcePrototype(protoResponse.getResourcePrototype());
+ }
+ System.out.println(name + ": Creating new group");
+ }
+
+ group.getResource().addAll(resources);
+ 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();
Please sign in to comment.
Something went wrong with that request. Please try again.