Skip to content

Commit

Permalink
[HHQ-3422] Add support for ControlAction and OpenNMSAction for AlertD…
Browse files Browse the repository at this point in the history
…efinitions.
  • Loading branch information
Ryan Morgan committed Sep 30, 2009
1 parent 5fe44ec commit 528e1b8
Show file tree
Hide file tree
Showing 6 changed files with 554 additions and 7 deletions.
4 changes: 2 additions & 2 deletions ChangeLog
Expand Up @@ -3,8 +3,8 @@ Changes in HQApi 2.3

*) [HQ-3076] Add ApplicationApi.

*) [HHQ-3422] Add support for ScriptAction AlertActions in the
AlertDefinition API.
*) [HHQ-3422] Add support for AlertActions in the AlertDefinition API.
Supported actions include ControlAction, ScriptAction and OpenNMSAction.

Changes in HQApi 2.2

Expand Down
66 changes: 62 additions & 4 deletions hqu/hqapi1/app/AlertdefinitionController.groovy
Expand Up @@ -165,8 +165,9 @@ public class AlertdefinitionController extends ApiController {
}

for (a in d.actions) {
// TODO: Only supporting ScriptAction for now.
if (a.className == "com.hyperic.hq.bizapp.server.action.control.ScriptAction") {
// TODO: User and Role notifications only handled through Escalation
if (a.className == "com.hyperic.hq.bizapp.server.action.control.ScriptAction" ||
a.className == "org.hyperic.hq.bizapp.server.action.integrate.OpenNMSAction") {
AlertAction(id: a.id,
className: a.className) {
def config = ConfigResponse.decode(a.config)
Expand All @@ -175,6 +176,33 @@ public class AlertdefinitionController extends ApiController {
value: config.getValue(key))
}
}
} else if (a.className == "com.hyperic.hq.bizapp.server.action.control.ControlAction") {
def config = ConfigResponse.decode(a.config)
def appdefType = config.getValue("appdefType")?.toInteger()
def appdefId = config.getValue("appdefId")?.toInteger()
def resource

if (appdefType == 1) {
resource = resourceHelper.find('platform':appdefId)
} else if (appdefType == 2) {
resource = resourceHelper.find('server':appdefId)
} else if (appdefType == 3) {
resource = resourceHelper.find('service':appdefId)
} else {
log.warn("Unable to find resource appdefType=" +
appdefType + " appdefId=" + appdefId)
continue // Skip this action
}

if (resource) {
AlertAction(id: a.id,
className: a.className) {
AlertActionConfig(key: 'resourceId',
value: resource.id)
AlertActionConfig(key: 'action',
value: config.getValue('action'))
}
}
}
}
}
Expand Down Expand Up @@ -524,8 +552,38 @@ public class AlertdefinitionController extends ApiController {
def className = xmlAction.'@className'

def cfg = [:]
for (xmlConfig in xmlAction['AlertActionConfig']) {
cfg[xmlConfig.'@key'] = xmlConfig.'@value'
// Special translation for ControlActions for Resource ids
if (className == "com.hyperic.hq.bizapp.server.action.control.ControlAction") {
def rId = xmlAction['AlertActionConfig'].find {
it.'@key' == 'resourceId'
}?.'@value'?.toInteger()

def action = xmlAction['AlertActionConfig'].find {
it.'@key' == 'action'
}?.'@value'

def cResource = getResource(rId)
if (cResource != null && action != null) {
def actions = cResource.getControlActions(user)
if (!actions.find { it == action }) {
log.warn("Resource " + cResource.name + " does not " +
"support action " + action)
continue
}

cfg['appdefType'] = Integer.toString(cResource.entityId.type)
cfg['appdefId'] = Integer.toString(cResource.entityId.id)
cfg['action'] = action
} else {
// If the resource is not found, don't add the action
log.warn("Ignoring invalid ControlAction config " +
xmlAction['AlertActionConfig'])
continue
}
} else {
for (xmlConfig in xmlAction['AlertActionConfig']) {
cfg[xmlConfig.'@key'] = xmlConfig.'@value'
}
}

ConfigResponse configResponse = new ConfigResponse(cfg)
Expand Down
57 changes: 56 additions & 1 deletion src/org/hyperic/hq/hqapi1/AlertDefinitionBuilder.java
Expand Up @@ -31,6 +31,7 @@
import org.hyperic.hq.hqapi1.types.AlertDefinition;
import org.hyperic.hq.hqapi1.types.AlertAction;
import org.hyperic.hq.hqapi1.types.AlertActionConfig;
import org.hyperic.hq.hqapi1.types.Resource;

/**
* This class is used to create {@link org.hyperic.hq.hqapi1.types.AlertCondition}s.
Expand Down Expand Up @@ -354,7 +355,7 @@ public static AlertCondition createConfigCondition(boolean required,
}

/**
* Create a ScriptAction
* Create a Script AlertAction
*
* @param script The script to execute when the alert fires.
* @return An {@link org.hyperic.hq.hqapi1.types.AlertAction} that can be
Expand All @@ -371,4 +372,58 @@ public static AlertAction createScriptAction(String script) {
a.getAlertActionConfig().add(cfg);
return a;
}

/**
* Create a ControlAction AlertAction
*
* @param r The {@link Resource} to run the action on
* @param action The control action to run
*
* @return An {@link org.hyperic.hq.hqapi1.types.AlertAction} that can be
* included in {@link org.hyperic.hq.hqapi1.types.AlertDefinition#getAlertAction()}.
*/
public static AlertAction createControlAction(Resource r, String action) {
AlertAction a = new AlertAction();
a.setClassName("com.hyperic.hq.bizapp.server.action.control.ControlAction");

AlertActionConfig resourceCfg = new AlertActionConfig();
resourceCfg.setKey("resourceId");
resourceCfg.setValue(Integer.toString(r.getId()));

AlertActionConfig actionCfg = new AlertActionConfig();
actionCfg.setKey("action");
actionCfg.setValue(action);

a.getAlertActionConfig().add(resourceCfg);
a.getAlertActionConfig().add(actionCfg);

return a;
}

/**
* Create an OpenNMS AlertAction
*
* @param server The address of the OpenNMS server
* @param port The port the OpenNMS server is listening on
*
* @return An {@link org.hyperic.hq.hqapi1.types.AlertAction} that can be
* included in {@link org.hyperic.hq.hqapi1.types.AlertDefinition#getAlertAction()}.
*/
public static AlertAction createOpenNMSAction(String server, int port) {
AlertAction a = new AlertAction();
a.setClassName("org.hyperic.hq.bizapp.server.action.integrate.OpenNMSAction");

AlertActionConfig serverCfg = new AlertActionConfig();
serverCfg.setKey("server");
serverCfg.setValue(server);

AlertActionConfig portCfg = new AlertActionConfig();
portCfg.setKey("port");
portCfg.setValue(Integer.toString(port));

a.getAlertActionConfig().add(serverCfg);
a.getAlertActionConfig().add(portCfg);

return a;
}
}
@@ -0,0 +1,91 @@
package org.hyperic.hq.hqapi1.test;

import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.AlertDefinitionApi;
import org.hyperic.hq.hqapi1.MetricApi;
import org.hyperic.hq.hqapi1.AlertDefinitionBuilder;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.MetricsResponse;
import org.hyperic.hq.hqapi1.types.Metric;
import org.hyperic.hq.hqapi1.types.AlertDefinition;
import org.hyperic.hq.hqapi1.types.AlertAction;
import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse;

import java.util.List;
import java.util.ArrayList;

public class AlertDefinitionSyncActions_test extends AlertDefinitionTestBase {

public AlertDefinitionSyncActions_test(String name) {
super(name);
}
public void testEmptyAlertAction() throws Exception {
HQApi api = getApi();
AlertDefinitionApi defApi = api.getAlertDefinitionApi();
MetricApi metricApi = api.getMetricApi();

Resource platform = getLocalPlatformResource(false, false);

MetricsResponse metricsResponse = metricApi.getMetrics(platform);
hqAssertSuccess(metricsResponse);
assertTrue("No metrics found for " + platform.getName(),
metricsResponse.getMetric().size() > 0);
Metric m = metricsResponse.getMetric().get(0);

AlertDefinition d = generateTestDefinition();
d.setResource(platform);
d.getAlertCondition().add(
AlertDefinitionBuilder.createChangeCondition(true, m.getName()));

AlertAction action = new AlertAction();
d.getAlertAction().add(action);

List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
definitions.add(d);
AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
hqAssertSuccess(response);

AlertDefinition def = response.getAlertDefinition().get(0);
validateDefinition(def);
assertEquals("Wrong number of actions found", 0, def.getAlertAction().size());

// Cleanup
cleanup(response.getAlertDefinition());
}

public void testBadClassName() throws Exception {

HQApi api = getApi();
AlertDefinitionApi defApi = api.getAlertDefinitionApi();
MetricApi metricApi = api.getMetricApi();

Resource platform = getLocalPlatformResource(false, false);

MetricsResponse metricsResponse = metricApi.getMetrics(platform);
hqAssertSuccess(metricsResponse);
assertTrue("No metrics found for " + platform.getName(),
metricsResponse.getMetric().size() > 0);
Metric m = metricsResponse.getMetric().get(0);

AlertDefinition d = generateTestDefinition();
d.setResource(platform);
d.getAlertCondition().add(
AlertDefinitionBuilder.createChangeCondition(true, m.getName()));

AlertAction action = new AlertAction();
action.setClassName("org.hyperic.bad.AlertAction");
d.getAlertAction().add(action);

List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
definitions.add(d);
AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
hqAssertSuccess(response);

AlertDefinition def = response.getAlertDefinition().get(0);
validateDefinition(def);
assertEquals("Wrong number of actions found", 0, def.getAlertAction().size());

// Cleanup
cleanup(response.getAlertDefinition());
}
}

0 comments on commit 528e1b8

Please sign in to comment.