Permalink
Browse files

Merge branch 'hqapi-1.0'

  • Loading branch information...
2 parents 50af2b6 + 932a5f0 commit fa863e7b491620a27ddf1eda7142aa6af658f1b4 Ryan Morgan committed May 26, 2009
@@ -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;
@@ -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) {
@@ -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()
@@ -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')
@@ -58,23 +58,60 @@
*
* @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
*
@@ -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 {
@@ -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;
@@ -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);

0 comments on commit fa863e7

Please sign in to comment.