Skip to content

Commit

Permalink
[HHQ-4175] Add EmailAction support for alert definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Aug 3, 2010
1 parent 47084a5 commit 53a09c3
Show file tree
Hide file tree
Showing 5 changed files with 785 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
@@ -1,6 +1,8 @@

Changes in HQApi 2.6

*) [HHQ-4175] Add EmailAction support for alert definitions.

*) [HHQ-4151] Add new API to ResourceApi to find a platform Resource
based on the passed resource id. Extended support for this into the
CLI using the --parentPlatform flag to resource list.
Expand Down
66 changes: 65 additions & 1 deletion hqu/hqapi1/app/AlertdefinitionController.groovy
Expand Up @@ -26,6 +26,34 @@ public class AlertdefinitionController extends ApiController {
private eventBoss = EventsBoss.one
private aMan = AMan.one

private EMAIL_NOTIFY_TYPE = [1:"email", 2:"users", 3:"roles"]

private String getNotificationNames(type, id) {
if (type == 1) {
return id
} else if (type == 2) {
def ids = id.split(",")
return ids.collect { getUser(it.toInteger(), null)?.name }.join(",")
} else if (type == 3) {
def ids = id.split(",")
return ids.collect { getRole(it.toInteger(), null)?.name }.join(",")
}
return null
}

private String getNotificationIds(type, name) {
if (type == 1) {
return name
} else if (type == 2) {
def names = name.split(",")
return names.collect {getUser(null, it)?.id}.findAll {it != null}.join(",")
} else if (type == 3) {
def names = name.split(",")
return names.collect {getRole(null, it)?.id}.findAll {it != null}.join(",")
}
return null
}

private EVENT_LEVEL_TO_NUM = [
ANY: -1,
ERR : LogTrackPlugin.LOGLEVEL_ERROR,
Expand Down Expand Up @@ -170,7 +198,6 @@ public class AlertdefinitionController extends ApiController {
}

for (a in d.actions) {
// 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,
Expand Down Expand Up @@ -208,6 +235,18 @@ public class AlertdefinitionController extends ApiController {
value: config.getValue('action'))
}
}
} else if (a.className == "com.hyperic.hq.bizapp.server.action.email.EmailAction") {
def config = ConfigResponse.decode(a.config)
def names = config.getValue("names")
def listType = config.getValue("listType")?.toInteger()

AlertAction(id: a.id,
className: a.className) {
AlertActionConfig(key: 'notifyType',
value: EMAIL_NOTIFY_TYPE[listType])
AlertActionConfig(key: 'names',
value: getNotificationNames(listType,names))
}
}
}
}
Expand Down Expand Up @@ -668,6 +707,31 @@ public class AlertdefinitionController extends ApiController {
xmlAction['AlertActionConfig'])
continue
}
} else if (className == "com.hyperic.hq.bizapp.server.action.email.EmailAction") {
def typeName = xmlAction['AlertActionConfig'].find {
it.'@key' == 'notifyType'
}?.'@value'

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

def type = EMAIL_NOTIFY_TYPE.find { it.value == typeName }?.key

if (!type) {
log.warn("Ignoring invalid EmailAction type " + typeName)
continue
}

def notificationIds = getNotificationIds(type, names)
if (notificationIds == null || notificationIds.length() == 0) {
log.warn("Ignoring invalid EmailAction notification=" + names)
continue
}

cfg['listType'] = type.toString()
cfg['names'] = notificationIds
cfg['sms'] = 'false' // XXX: Legacy a presume..
} else {
for (xmlConfig in xmlAction['AlertActionConfig']) {
cfg[xmlConfig.'@key'] = xmlConfig.'@value'
Expand Down
187 changes: 187 additions & 0 deletions src/org/hyperic/hq/hqapi1/AlertDefinitionBuilder.java
Expand Up @@ -32,6 +32,8 @@
import org.hyperic.hq.hqapi1.types.AlertAction;
import org.hyperic.hq.hqapi1.types.AlertActionConfig;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.Role;
import org.hyperic.hq.hqapi1.types.User;

/**
* This class is used to create {@link org.hyperic.hq.hqapi1.types.AlertCondition}s.
Expand Down Expand Up @@ -426,4 +428,189 @@ public static AlertAction createOpenNMSAction(String server, int port) {

return a;
}

/**
* Add the list of {@link User}s to the list of notifications for this alert definition.
*
* @param d The {@link org.hyperic.hq.hqapi1.types.AlertDefinition} to modify.
* @param users The list of Users to notify.
*/
public static void addEmailAction(AlertDefinition d, User[] users) {

for (AlertAction a : d.getAlertAction()) {
if (a.getClassName().equals("com.hyperic.hq.bizapp.server.action.email.EmailAction")) {
boolean actionExists = false;
AlertActionConfig names = null;
for (AlertActionConfig cfg : a.getAlertActionConfig()) {
if (cfg.getKey().equals("notifyType") &&
cfg.getValue().equals("users")) {
actionExists = true;
}
if (cfg.getKey().equals("names")) {
names = cfg;
}
}

if (actionExists && names != null) {
// Set to the new value
StringBuffer nameList = new StringBuffer();
for (int i = 0; i < users.length; i++) {
nameList.append(users[i].getName());
if (i + 1 < users.length) {
nameList.append(",");
}
}
names.setValue(nameList.toString());
return;
}
}
}

// Action does not exist, create it.
AlertAction a = new AlertAction();
a.setClassName("com.hyperic.hq.bizapp.server.action.email.EmailAction");

AlertActionConfig type = new AlertActionConfig();
type.setKey("notifyType");
type.setValue("users");

StringBuffer nameList = new StringBuffer();
for (int i = 0; i < users.length; i++) {
nameList.append(users[i].getName());
if (i + 1 < users.length) {
nameList.append(",");
}
}
AlertActionConfig names = new AlertActionConfig();
names.setKey("names");
names.setValue(nameList.toString());

a.getAlertActionConfig().add(type);
a.getAlertActionConfig().add(names);

d.getAlertAction().add(a);
}

/**
* Add the list of {@link Role}s to the list of notifications for this alert definition.
*
* @param d The {@link org.hyperic.hq.hqapi1.types.AlertDefinition} to modify.
* @param roles The list of Roles to notify.
*/
public static void addEmailAction(AlertDefinition d, Role[] roles) {
for (AlertAction a : d.getAlertAction()) {
if (a.getClassName().equals("com.hyperic.hq.bizapp.server.action.email.EmailAction")) {
boolean actionExists = false;
AlertActionConfig names = null;
for (AlertActionConfig cfg : a.getAlertActionConfig()) {
if (cfg.getKey().equals("notifyType") &&
cfg.getValue().equals("roles")) {
actionExists = true;
}
if (cfg.getKey().equals("names")) {
names = cfg;
}
}

if (actionExists && names != null) {
// Set to the new value
StringBuffer nameList = new StringBuffer();
for (int i = 0; i < roles.length; i++) {
nameList.append(roles[i].getName());
if (i + 1 < roles.length) {
nameList.append(",");
}
}
names.setValue(nameList.toString());
return;
}
}
}

// Action does not exist, create it.
AlertAction a = new AlertAction();
a.setClassName("com.hyperic.hq.bizapp.server.action.email.EmailAction");

AlertActionConfig type = new AlertActionConfig();
type.setKey("notifyType");
type.setValue("roles");

StringBuffer nameList = new StringBuffer();
for (int i = 0; i < roles.length; i++) {
nameList.append(roles[i].getName());
if (i+1 < roles.length) {
nameList.append(",");
}
}
AlertActionConfig names = new AlertActionConfig();
names.setKey("names");
names.setValue(nameList.toString());

a.getAlertActionConfig().add(type);
a.getAlertActionConfig().add(names);

d.getAlertAction().add(a);
}

/**
* Add the list of email addresses to the list of notifications for this alert definition.
*
* @param d The {@link org.hyperic.hq.hqapi1.types.AlertDefinition} to modify.
* @param otherRecipients The list of email addresses to notify.
*/
public static void addEmailAction(AlertDefinition d, String[] otherRecipients) {

for (AlertAction a : d.getAlertAction()) {
if (a.getClassName().equals("com.hyperic.hq.bizapp.server.action.email.EmailAction")) {
boolean actionExists = false;
AlertActionConfig names = null;
for (AlertActionConfig cfg : a.getAlertActionConfig()) {
if (cfg.getKey().equals("notifyType") &&
cfg.getValue().equals("email")) {
actionExists = true;
}
if (cfg.getKey().equals("names")) {
names = cfg;
}
}

if (actionExists && names != null) {
// Set to the new value
StringBuffer nameList = new StringBuffer();
for (int i = 0; i < otherRecipients.length; i++) {
nameList.append(otherRecipients[i]);
if (i + 1 < otherRecipients.length) {
nameList.append(",");
}
}
names.setValue(nameList.toString());
return;
}
}
}

// Action does not exist, create it.
AlertAction a = new AlertAction();
a.setClassName("com.hyperic.hq.bizapp.server.action.email.EmailAction");

AlertActionConfig type = new AlertActionConfig();
type.setKey("notifyType");
type.setValue("email");

StringBuffer nameList = new StringBuffer();
for (int i = 0; i < otherRecipients.length; i++) {
nameList.append(otherRecipients[i]);
if (i+1 < otherRecipients.length) {
nameList.append(",");
}
}
AlertActionConfig names = new AlertActionConfig();
names.setKey("names");
names.setValue(nameList.toString());

a.getAlertActionConfig().add(type);
a.getAlertActionConfig().add(names);

d.getAlertAction().add(a);
}
}

0 comments on commit 53a09c3

Please sign in to comment.