Skip to content

Commit

Permalink
Add --delete option to command line group syncing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Feb 10, 2010
1 parent 51a1f89 commit 43d94b1
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions src/org/hyperic/hq/hqapi1/tools/GroupCommand.java
Expand Up @@ -20,6 +20,9 @@
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;

Expand All @@ -43,6 +46,7 @@ public class GroupCommand extends Command {
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");
Expand Down Expand Up @@ -116,6 +120,8 @@ private void sync(String[] args) throws Exception {
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);

Expand All @@ -140,13 +146,14 @@ private void sync(String[] args) throws Exception {
}

// Helper function to unroll a resource and it's children into a single list.
private List<Resource> getFlattenResources(List<Resource> resources) {
List<Resource> result = new ArrayList<Resource>();
private Map<Integer,Resource> getFlattenResources(Collection<Resource> resources) {

Map<Integer,Resource> result = new HashMap<Integer,Resource>();

for (Resource r : resources) {
result.add(r);
result.put(r.getId(), r);
if (r.getResource().size() > 0) {
result.addAll(getFlattenResources(r.getResource()));
result.putAll(getFlattenResources(r.getResource()));
}
}
return result;
Expand All @@ -164,10 +171,11 @@ private void syncViaCommandLineArgs(OptionSet s) throws Exception
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);

List<Resource> resources;
Map<Integer,Resource> resources = new HashMap<Integer,Resource>();

if (prototype != null && platform != null) {
System.err.println("Only one of " + OPT_PROTOTYPE + " or " +
Expand All @@ -185,13 +193,15 @@ private void syncViaCommandLineArgs(OptionSet s) throws Exception
ResourcesResponse resourcesResponse = api.getResourceApi().
getResources(protoResponse.getResourcePrototype(), false, children);
checkSuccess(resourcesResponse);
resources = resourcesResponse.getResource();
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 = new ArrayList<Resource>();
resources.add(resourceResponse.getResource());
resources.put(resourceResponse.getResource().getId(),
resourceResponse.getResource());
} else {
System.err.println("One of " + OPT_PROTOTYPE + " or " +
OPT_PLATFORM + " is required.");
Expand All @@ -201,7 +211,7 @@ private void syncViaCommandLineArgs(OptionSet s) throws Exception
// Filter based on regex, if given.
if (regex != null) {
Pattern pattern = Pattern.compile(regex);
for (Iterator<Resource> i = resources.iterator(); i.hasNext(); ) {
for (Iterator<Resource> i = resources.values().iterator(); i.hasNext(); ) {
Resource r = i.next();
Matcher m = pattern.matcher(r.getName());
if (!m.matches()) {
Expand All @@ -225,6 +235,13 @@ private void syncViaCommandLineArgs(OptionSet s) throws Exception
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) {
Expand All @@ -240,8 +257,19 @@ private void syncViaCommandLineArgs(OptionSet s) throws Exception
group.setDescription((String)s.valueOf(OPT_DESC));
}

List<Resource> flattenedResources = getFlattenResources(resources);
group.getResource().addAll(flattenedResources);
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);
Expand Down

0 comments on commit 43d94b1

Please sign in to comment.