Skip to content

Commit

Permalink
[HHQ-3422] Add support for ScriptAction in AlertDefinition list/sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Sep 21, 2009
1 parent 8de4296 commit 290ebe3
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
32 changes: 32 additions & 0 deletions hqu/hqapi1/app/AlertdefinitionController.groovy
Expand Up @@ -8,11 +8,13 @@ import org.hyperic.hq.appdef.shared.AppdefEntityTypeID
import org.hyperic.hq.bizapp.server.session.EventsBossEJBImpl as EventsBoss
import org.hyperic.hq.events.AlertSeverity
import org.hyperic.hq.events.EventConstants
import org.hyperic.hq.events.shared.ActionValue
import org.hyperic.hq.events.shared.AlertConditionValue
import org.hyperic.hq.events.shared.AlertDefinitionValue
import org.hyperic.hq.events.server.session.AlertDefinitionManagerEJBImpl as AMan
import org.hyperic.hq.measurement.shared.ResourceLogEvent
import org.hyperic.hq.product.LogTrackPlugin
import org.hyperic.util.config.ConfigResponse
import ApiController

public class AlertdefinitionController extends ApiController {
Expand Down Expand Up @@ -155,6 +157,20 @@ public class AlertdefinitionController extends ApiController {
// Write it out
AlertCondition(conditionAttrs)
}

for (a in d.actions) {
// TODO: Only supporting ScriptAction for now.
if (a.className == "com.hyperic.hq.bizapp.server.action.control.ScriptAction") {
AlertAction(id: a.id,
className: a.className) {
def config = ConfigResponse.decode(a.config)
for (key in config.keys) {
AlertActionConfig(key: key,
value: config.getValue(key))
}
}
}
}
}
}
}
Expand Down Expand Up @@ -497,6 +513,22 @@ public class AlertdefinitionController extends ApiController {

def isRecovery = false

for (xmlAction in xmlDef['AlertAction']) {
def actionId = xmlAction.'@id'?.toInteger()
def className = xmlAction.'@className'

def cfg = [:]
for (xmlConfig in xmlAction['AlertActionConfig']) {
cfg[xmlConfig.'@key'] = xmlConfig.'@value'
}

ConfigResponse configResponse = new ConfigResponse(cfg)
ActionValue action = new ActionValue(actionId, className,
configResponse.encode(),
null)
adv.addAction(action)
}

for (xmlCond in xmlDef['AlertCondition']) {
AlertConditionValue acv = new AlertConditionValue()
def acError
Expand Down
21 changes: 21 additions & 0 deletions src/org/hyperic/hq/hqapi1/AlertDefinitionBuilder.java
Expand Up @@ -2,6 +2,8 @@

import org.hyperic.hq.hqapi1.types.AlertCondition;
import org.hyperic.hq.hqapi1.types.AlertDefinition;
import org.hyperic.hq.hqapi1.types.AlertAction;
import org.hyperic.hq.hqapi1.types.AlertActionConfig;

/**
* This class is used to create {@link org.hyperic.hq.hqapi1.types.AlertCondition}s.
Expand Down Expand Up @@ -323,4 +325,23 @@ public static AlertCondition createConfigCondition(boolean required,
c.setConfigMatch(matches);
return c;
}

/**
* Create a ScriptAction
*
* @param script The script to execute when the alert fires.
* @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 createScriptAction(String script) {
AlertAction a = new AlertAction();
a.setClassName("com.hyperic.hq.bizapp.server.action.control.ScriptAction");

AlertActionConfig cfg = new AlertActionConfig();
cfg.setKey("script");
cfg.setValue(script);

a.getAlertActionConfig().add(cfg);
return a;
}
}
@@ -0,0 +1,114 @@
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.AlertDefinitionsResponse;
import org.hyperic.hq.hqapi1.types.AlertAction;
import org.hyperic.hq.hqapi1.types.AlertActionConfig;

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

public class AlertDefinitionSyncScriptAction_test extends AlertDefinitionTestBase {

public AlertDefinitionSyncScriptAction_test(String name) {
super(name);
}

public void testSyncScriptAction() throws Exception {

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

final String SCRIPT = "/usr/bin/true";

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 = AlertDefinitionBuilder.createScriptAction(SCRIPT);
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", 1, def.getAlertAction().size());
AlertAction syncedAction = def.getAlertAction().get(0);
assertEquals("Wrong action class", "com.hyperic.hq.bizapp.server.action.control.ScriptAction",
syncedAction.getClassName());
assertEquals("Wrong number of configuration options",
1, syncedAction.getAlertActionConfig().size());
AlertActionConfig cfg = syncedAction.getAlertActionConfig().get(0);
assertEquals("Wrong path to script", SCRIPT, cfg.getValue());

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

public void testSyncScriptActionRemove() throws Exception {

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

final String SCRIPT = "/usr/bin/true";

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 = AlertDefinitionBuilder.createScriptAction(SCRIPT);
d.getAlertAction().add(action);

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

AlertDefinition syncedDef = response.getAlertDefinition().get(0);
assertEquals("Wrong number of actions", 1, syncedDef.getAlertAction().size());

// Clear the action's and resync

syncedDef.getAlertAction().clear();

definitions.clear();
definitions.add(syncedDef);
response = defApi.syncAlertDefinitions(definitions);
hqAssertSuccess(response);

syncedDef = response.getAlertDefinition().get(0);
assertEquals("Alert actions not cleared!", 0, syncedDef.getAlertAction().size());

cleanup(response.getAlertDefinition());
}
}
14 changes: 14 additions & 0 deletions xsd/AlertDefinition.xsd
Expand Up @@ -6,6 +6,19 @@
<xs:include schemaLocation="Escalation.xsd"/>
<xs:include schemaLocation="Resource.xsd"/>

<xs:complexType name="AlertActionConfig">
<xs:attribute name="value" type="xs:string" use="required"/>
<xs:attribute name="key" type="xs:string" use="required"/>
</xs:complexType>

<xs:complexType name="AlertAction">
<xs:sequence>
<xs:element name="AlertActionConfig" type="AlertActionConfig" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="className" type="xs:string" use="required"/>
<xs:attribute name="id" type="xs:integer" use="optional"/>
</xs:complexType>

<!-- Since conditions must be ordered and inheritence does not work so
well in JAXB we must define all options -->
<xs:complexType name="AlertCondition">
Expand Down Expand Up @@ -44,6 +57,7 @@
<xs:element name="Escalation" type="Escalation" maxOccurs="1"/>
<!-- The ordering of this this is important -->
<xs:element name="AlertCondition" type="AlertCondition" maxOccurs="unbounded"/>
<xs:element name="AlertAction" type="AlertAction" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:int" use="optional" />
<xs:attribute name="name" type="xs:string" use="required" />
Expand Down

0 comments on commit 290ebe3

Please sign in to comment.