Skip to content

Commit

Permalink
Merge branch 'hqapi-1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed May 26, 2009
2 parents 50af2b6 + 932a5f0 commit fa863e7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 66 deletions.
29 changes: 28 additions & 1 deletion hqu/hqapi1/app/AlertdefinitionController.groovy
Expand Up @@ -164,6 +164,10 @@ public class AlertdefinitionController extends ApiController {

def listDefinitions(params) {

def alertNameFilter = params.getOne('alertNameFilter')
def resourceNameFilter = params.getOne('resourceNameFilter')
def groupName = params.getOne('groupName')

def excludeTypeBased = params.getOne('excludeTypeBased')?.toBoolean()
if (excludeTypeBased == null) {
excludeTypeBased = false;
Expand Down Expand Up @@ -192,7 +196,30 @@ public class AlertdefinitionController extends ApiController {
definitions = alertHelper.findDefinitions(AlertSeverity.LOW, null,
excludeTypeBased)
}


// Filter
try {
if (alertNameFilter) {
definitions = definitions.findAll { it.name ==~ alertNameFilter }
}
if (resourceNameFilter) {
definitions = definitions.findAll { it.resource.name ==~ resourceNameFilter }
}
} catch (java.util.regex.PatternSyntaxException e) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Invalid syntax: " + e.getMessage())
}
if (groupName) {
def group = getGroup(null, groupName)
if (!group) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Unable to find group with name " + groupName)
} else {
def resources = group.resources
definitions = definitions.findAll { resources.contains(it.resource) }
}
}

renderXml() {
out << AlertDefinitionsResponse() {
if (failureXml) {
Expand Down
15 changes: 15 additions & 0 deletions hqu/hqapi1/app/ApiController.groovy
Expand Up @@ -105,6 +105,21 @@ class ApiController extends BaseController {
}
}

/**
* Get a group by id or name
*
* @return The group with the given id or name. If the passed in id is null,
* then the group with the given name is returned. If no group could be found
* for either the id or name, null is returned.
*/
protected getGroup(Integer id, String name) {
if (id != null) {
return resourceHelper.findGroup(id)
} else {
return resourceHelper.findGroupByName(name)
}
}

def dispatchRequest() {

long start = System.currentTimeMillis()
Expand Down
8 changes: 0 additions & 8 deletions hqu/hqapi1/app/GroupController.groovy
Expand Up @@ -25,14 +25,6 @@ class GroupController extends ApiController {
}
}

private getGroup(Integer id, String name) {
if (id != null) {
return resourceHelper.findGroup(id)
} else {
return resourceHelper.findGroupByName(name)
}
}

def get(params) {
def id = params.getOne('id')?.toInteger()
def name = params.getOne('name')
Expand Down
41 changes: 39 additions & 2 deletions src/org/hyperic/hq/hqapi1/AlertDefinitionApi.java
Expand Up @@ -58,22 +58,59 @@ public class AlertDefinitionApi extends BaseApi {
*
* @param excludeTypeBased Flag to control whether instances of type based
* alerts will be included.
*
* @param alertNameFilter Filter returned definitions by definition name
* using the given regular expression. A value of null will result in no
* filtering being performed.
* @param resourceNameFilter Filter returned definitions by resource name
* using the given regular expression. A value of null will result in no
* filtering being performed.
* @param groupName Filter returned definitions such that only definitions
* on resources belonging to the given group are returned.
*
* @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS},
* a list of AlertDefinitions are returned.
*
* @throws java.io.IOException If a network error occurs while making the request.
*/
public AlertDefinitionsResponse getAlertDefinitions(boolean excludeTypeBased)
public AlertDefinitionsResponse getAlertDefinitions(boolean excludeTypeBased,
String alertNameFilter,
String resourceNameFilter,
String groupName)
throws IOException
{
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("excludeTypeBased", new String[] { Boolean.toString(excludeTypeBased)});
if (alertNameFilter != null) {
params.put("alertNameFilter", new String[] { alertNameFilter });
}
if (resourceNameFilter != null) {
params.put("resourceNameFilter", new String[] { resourceNameFilter });
}
if (groupName != null) {
params.put("groupName", new String[] { groupName });
}

return doGet("alertdefinition/listDefinitions.hqu", params,
AlertDefinitionsResponse.class);
}

/**
* Find all {@link org.hyperic.hq.hqapi1.types.AlertDefinition}s in the system.
*
* @param excludeTypeBased Flag to control whether instances of type based
* alerts will be included.
*
* @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS},
* a list of AlertDefinitions are returned.
*
* @throws java.io.IOException If a network error occurs while making the request.
*/
public AlertDefinitionsResponse getAlertDefinitions(boolean excludeTypeBased)
throws IOException
{
return getAlertDefinitions(excludeTypeBased, null, null, null);
}

/**
* Find all {@link org.hyperic.hq.hqapi1.types.AlertDefinition}s based on
* the given parent resource type alert
Expand Down
62 changes: 7 additions & 55 deletions src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java
Expand Up @@ -32,20 +32,13 @@
import org.hyperic.hq.hqapi1.AlertDefinitionApi;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.XmlUtil;
import org.hyperic.hq.hqapi1.GroupApi;
import org.hyperic.hq.hqapi1.types.AlertDefinition;
import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.GroupResponse;
import org.hyperic.hq.hqapi1.types.Resource;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class AlertDefinitionCommand extends Command {

Expand Down Expand Up @@ -112,7 +105,6 @@ private void list(String[] args) throws Exception {

HQApi api = getApi(options);
AlertDefinitionApi definitionApi = api.getAlertDefinitionApi();
GroupApi groupApi = api.getGroupApi();

AlertDefinitionsResponse alertDefs;

Expand All @@ -133,55 +125,15 @@ private void list(String[] args) throws Exception {
" when " + OPT_TYPEALERTS + " is specified.");
System.exit(-1);
}
alertDefs = definitionApi.getAlertDefinitions(excludeTypeAlerts);
}

// Filter on group if necessary
if (options.has(OPT_GROUP)) {
GroupResponse group =
groupApi.getGroup((String)options.valueOf(OPT_GROUP));
checkSuccess(group);

List<Integer> includedResources = new ArrayList<Integer>();
for (Resource r : group.getGroup().getResource()) {
includedResources.add(r.getId());
}

for (Iterator<AlertDefinition> i = alertDefs.getAlertDefinition().iterator();
i.hasNext(); ) {
AlertDefinition d = i.next();
Integer rid = d.getResource().getId();
if (!includedResources.contains(rid)) {
i.remove();
}
}
}
String alertNameFilter = (String)options.valueOf(OPT_ALERT_NAME);
String resourceNameFilter = (String)options.valueOf(OPT_RESOURCE_NAME);
String groupNameFilter = (String)options.valueOf(OPT_GROUP);

// Filter on resource name if necessary
if (options.has(OPT_RESOURCE_NAME)) {
Pattern pattern = Pattern.compile((String)options.valueOf(OPT_RESOURCE_NAME));

for (Iterator<AlertDefinition> i = alertDefs.getAlertDefinition().iterator();
i.hasNext(); ) {
AlertDefinition d = i.next();
Matcher m = pattern.matcher(d.getResource().getName());
if (!m.matches()) {
i.remove();
}
}
}

if (options.has(OPT_ALERT_NAME)) {
Pattern pattern = Pattern.compile((String)options.valueOf(OPT_ALERT_NAME));

for (Iterator<AlertDefinition> i = alertDefs.getAlertDefinition().iterator();
i.hasNext(); ) {
AlertDefinition d = i.next();
Matcher m = pattern.matcher(d.getName());
if (!m.matches()) {
i.remove();
}
}
alertDefs = definitionApi.getAlertDefinitions(excludeTypeAlerts,
alertNameFilter,
resourceNameFilter,
groupNameFilter);
}

checkSuccess(alertDefs);
Expand Down

0 comments on commit fa863e7

Please sign in to comment.