From b417716eb5db4805de6ffb3b9f94b676562357a6 Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Mon, 8 Mar 2010 16:08:23 -0800 Subject: [PATCH] [HHQ-3231,HHQ-3786] Allow alert definitions to be listed by platform. --- ChangeLog | 2 ++ .../app/AlertdefinitionController.groovy | 14 +++++++++ .../hyperic/hq/hqapi1/AlertDefinitionApi.java | 22 ++++++++++++++ .../AlertDefinitionGetByResource_test.java | 29 +++++++++++++++++++ .../hqapi1/tools/AlertDefinitionCommand.java | 15 ++++++++++ xsd/HQApi1.wadl | 8 +++++ 6 files changed, 90 insertions(+) create mode 100644 src/org/hyperic/hq/hqapi1/test/AlertDefinitionGetByResource_test.java diff --git a/ChangeLog b/ChangeLog index ce478635..a8f829fb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,8 @@ Changes in HQApi 2.4 + *) [HHQ-3231,HHQ-3786] Allow alert definitions to be listed by platform. + *) [HHQ-3768] Add --id and --name options to the group list CLI command. *) [HHQ-3785] Add command lines options to alertdefinition sync command diff --git a/hqu/hqapi1/app/AlertdefinitionController.groovy b/hqu/hqapi1/app/AlertdefinitionController.groovy index 9c709d76..086db91d 100644 --- a/hqu/hqapi1/app/AlertdefinitionController.groovy +++ b/hqu/hqapi1/app/AlertdefinitionController.groovy @@ -215,6 +215,7 @@ public class AlertdefinitionController extends ApiController { def resourceNameFilter = params.getOne('resourceNameFilter') def groupName = params.getOne('groupName') def escalationId = params.getOne('escalationId')?.toInteger() + def resourceId = params.getOne('resourceId')?.toInteger() def excludeTypeBased = params.getOne('excludeTypeBased')?.toBoolean() if (excludeTypeBased == null) { @@ -253,6 +254,19 @@ public class AlertdefinitionController extends ApiController { definitions = definitions.findAll { it.parent == null } } } + } else if (resourceId != null) { + def resource = getResource(resourceId) + if (!resource) { + failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND, + "Resource with id = " + resourceId + + " not found") + } else { + // TODO: Add to alert helper + definitions = aMan.findRelatedAlertDefinitions(user, resource) + if (excludeTypeBased) { + definitions = definitions.findAll { it.parent == null } + } + } } else { definitions = alertHelper.findDefinitions(AlertSeverity.LOW, null, excludeTypeBased) diff --git a/src/org/hyperic/hq/hqapi1/AlertDefinitionApi.java b/src/org/hyperic/hq/hqapi1/AlertDefinitionApi.java index e4d5e9e0..fcc1acaf 100644 --- a/src/org/hyperic/hq/hqapi1/AlertDefinitionApi.java +++ b/src/org/hyperic/hq/hqapi1/AlertDefinitionApi.java @@ -28,6 +28,7 @@ package org.hyperic.hq.hqapi1; import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse; +import org.hyperic.hq.hqapi1.types.Resource; import org.hyperic.hq.hqapi1.types.StatusResponse; import org.hyperic.hq.hqapi1.types.AlertDefinition; import org.hyperic.hq.hqapi1.types.AlertDefinitionsRequest; @@ -139,6 +140,27 @@ public AlertDefinitionsResponse getAlertDefinitions(AlertDefinition parent) AlertDefinitionsResponse.class); } + /** + * Find all {@link org.hyperic.hq.hqapi1.types.AlertDefinition}s on the + * given {@link Resource} including all descendant resources. + * + * @param r The Resource to query for alert definitions + * + * @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(Resource r) + throws IOException { + + Map params = new HashMap(); + params.put("resourceId", new String[] { Integer.toString(r.getId()) }); + + return doGet("alertdefinition/listDefinitions.hqu", params, + AlertDefinitionsResponse.class); + } + /** * Find all type based {@link org.hyperic.hq.hqapi1.types.AlertDefinition}s in the system. * diff --git a/src/org/hyperic/hq/hqapi1/test/AlertDefinitionGetByResource_test.java b/src/org/hyperic/hq/hqapi1/test/AlertDefinitionGetByResource_test.java new file mode 100644 index 00000000..c754a651 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/AlertDefinitionGetByResource_test.java @@ -0,0 +1,29 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.AlertDefinitionApi; +import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse; +import org.hyperic.hq.hqapi1.types.Resource; + +public class AlertDefinitionGetByResource_test extends AlertDefinitionTestBase { + + public AlertDefinitionGetByResource_test(String name) { + super(name); + } + + public void testGetByResource() throws Exception { + AlertDefinitionApi api = getApi().getAlertDefinitionApi(); + + Resource localPlatform = getLocalPlatformResource(false, false); + AlertDefinitionsResponse response = api.getAlertDefinitions(localPlatform); + hqAssertSuccess(response); + } + + public void testGetByInvalidResource() throws Exception { + AlertDefinitionApi api = getApi().getAlertDefinitionApi(); + + Resource r = new Resource(); + r.setId(Integer.MAX_VALUE); + AlertDefinitionsResponse response = api.getAlertDefinitions(r); + hqAssertFailureObjectNotFound(response); + } +} diff --git a/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java b/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java index eb87ade0..32c5d0da 100644 --- a/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java +++ b/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java @@ -32,11 +32,14 @@ import org.hyperic.hq.hqapi1.AlertDefinitionApi; import org.hyperic.hq.hqapi1.AlertDefinitionBuilder; import org.hyperic.hq.hqapi1.HQApi; +import org.hyperic.hq.hqapi1.ResourceApi; import org.hyperic.hq.hqapi1.XmlUtil; import org.hyperic.hq.hqapi1.EscalationApi; import org.hyperic.hq.hqapi1.types.AlertAction; import org.hyperic.hq.hqapi1.types.AlertDefinition; import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse; +import org.hyperic.hq.hqapi1.types.Resource; +import org.hyperic.hq.hqapi1.types.ResourceResponse; import org.hyperic.hq.hqapi1.types.StatusResponse; import org.hyperic.hq.hqapi1.types.EscalationResponse; import org.hyperic.hq.hqapi1.types.Escalation; @@ -67,6 +70,7 @@ public class AlertDefinitionCommand extends Command { private static String OPT_COND_COUNT = "conditionCount"; private static String OPT_COND_INCLUDE = "conditionTypeInclude"; private static String OPT_COND_EXCLUDE = "conditionTypeExclude"; + private static String OPT_PLATFORM = "platform"; // Command line syncing options private static String OPT_ASSIGN_ESC = "assignEscalation"; @@ -111,6 +115,9 @@ private void list(String[] args) throws Exception { p.accepts(OPT_GROUP, "If specified, only show alert definitions for " + "resources that belong to the specified group."). withRequiredArg().ofType(String.class); + p.accepts(OPT_PLATFORM, "Return all alerts on the given platform and " + + "all descendant children"). + withRequiredArg().ofType(String.class); p.accepts(OPT_RESOURCE_NAME, "If specified, only show alert definitions " + "belonging to a resource with the given " + "resource name regex."). @@ -139,6 +146,7 @@ private void list(String[] args) throws Exception { HQApi api = getApi(options); AlertDefinitionApi definitionApi = api.getAlertDefinitionApi(); EscalationApi escalationApi = api.getEscalationApi(); + ResourceApi rApi = api.getResourceApi(); AlertDefinitionsResponse alertDefs; @@ -149,6 +157,13 @@ private void list(String[] args) throws Exception { } alertDefs = definitionApi.getTypeAlertDefinitions(excludeIds); + } else if (options.has(OPT_PLATFORM)) { + String platformName = (String)getRequired(options, OPT_PLATFORM); + ResourceResponse resourceResponse = + rApi.getPlatformResource(platformName, false, false); + checkSuccess(resourceResponse); + + alertDefs = definitionApi.getAlertDefinitions(resourceResponse.getResource()); } else { boolean excludeTypeAlerts = false; if (options.has(OPT_EXCLUDE_TYPEALERTS)) { diff --git a/xsd/HQApi1.wadl b/xsd/HQApi1.wadl index cb40d2b2..bde08aa6 100644 --- a/xsd/HQApi1.wadl +++ b/xsd/HQApi1.wadl @@ -820,6 +820,14 @@ contained in the specified Group. + + + If specified, only return AlertDefinitions that are + assigned to the specified Resource or any descendent + Resource. + +