Skip to content

Commit

Permalink
Merge branch 'hqapi-2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Feb 10, 2010
2 parents 1e88536 + f901a97 commit 522a1de
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion hqu/hqapi1/app/GroupController.groovy
Expand Up @@ -285,4 +285,4 @@ class GroupController extends ApiController {
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/org/hyperic/hq/hqapi1/ErrorCode.java
Expand Up @@ -49,10 +49,10 @@ public enum ErrorCode {
INVALID_PARAMETERS("InvalidParameters",
"The given parameters are incorrect"),
/**
* An unexpected error occured.
* An unexpected error occurred.
*/
UNEXPECTED_ERROR("UnexpectedError",
"An unexpected error occured"),
"An unexpected error occurred"),
/**
* The current user does not have permission for this operation.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/org/hyperic/hq/hqapi1/HQConnection.java
Expand Up @@ -267,7 +267,7 @@ private <T> T runMethod(HttpMethodBase method, String uri,
// Some other server blow up.
error = new ServiceError();
error.setErrorCode("UnexpectedError");
error.setReasonText("An unexpected error occured");
error.setReasonText("An unexpected error occurred");
return getErrorResponse(resultClass, error);
}
} catch (SocketException e) {
Expand Down
50 changes: 39 additions & 11 deletions src/org/hyperic/hq/hqapi1/tools/GroupCommand.java
Expand Up @@ -47,6 +47,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 @@ -70,6 +73,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 @@ -143,6 +147,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 @@ -167,13 +173,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 @@ -191,10 +198,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 @@ -212,13 +220,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 @@ -228,7 +238,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 @@ -252,6 +262,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 @@ -267,8 +284,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 522a1de

Please sign in to comment.