Skip to content

Commit

Permalink
[HHQ-3709] Allow group syncing via command line arguments to include …
Browse files Browse the repository at this point in the history
…child resources.
  • Loading branch information
Ryan Morgan committed Feb 3, 2010
1 parent f8382d4 commit e99e706
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/org/hyperic/hq/hqapi1/tools/GroupCommand.java
Expand Up @@ -40,6 +40,7 @@ public class GroupCommand extends Command {
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 void printUsage() {
System.err.println("One of " + Arrays.toString(COMMANDS) + " required");
Expand Down Expand Up @@ -109,6 +110,8 @@ private void sync(String[] args) throws Exception {
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");

OptionSet options = getOptions(p, args);

Expand All @@ -132,6 +135,19 @@ private void sync(String[] args) throws Exception {
System.out.println("Successfully synced " + groups.size() + " groups.");
}

// 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>();

for (Resource r : resources) {
result.add(r);
if (r.getResource().size() > 0) {
result.addAll(getFlattenResources(r.getResource()));
}
}
return result;
}

private void syncViaCommandLineArgs(OptionSet s) throws Exception
{
// Required args
Expand All @@ -143,6 +159,7 @@ private void syncViaCommandLineArgs(OptionSet s) throws Exception
String description = (String)s.valueOf(OPT_DESC);
boolean deleteMissing = s.has(OPT_DELETEMISSING);
boolean compatible = s.has(OPT_COMPAT);
boolean children = s.has(OPT_CHILDREN);

HQApi api = getApi(s);

Expand All @@ -153,7 +170,7 @@ private void syncViaCommandLineArgs(OptionSet s) throws Exception

// Query resources
ResourcesResponse resourceResponse = api.getResourceApi().
getResources(protoResponse.getResourcePrototype(), false, false);
getResources(protoResponse.getResourcePrototype(), false, children);
checkSuccess(resourceResponse);

List<Resource> resources = resourceResponse.getResource();
Expand Down Expand Up @@ -196,8 +213,9 @@ private void syncViaCommandLineArgs(OptionSet s) throws Exception
if (s.hasArgument(OPT_DESC)) {
group.setDescription((String)s.valueOf(OPT_DESC));
}

group.getResource().addAll(resources);

List<Resource> flattenedResources = getFlattenResources(resources);
group.getResource().addAll(flattenedResources);
List<Group> groups = new ArrayList<Group>();
groups.add(group);
GroupsResponse syncResponse = api.getGroupApi().syncGroups(groups);
Expand Down

0 comments on commit e99e706

Please sign in to comment.