Permalink
Browse files

Fix merge conflicts.

  • Loading branch information...
2 parents fa97b00 + 53a09c3 commit 2c7901e6c0bf48bb437a4280c0be35d63bbe707e Ryan Morgan committed Aug 3, 2010
View
@@ -92,6 +92,8 @@ Changes in HQApi 3.0
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.
@@ -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,
@@ -176,7 +204,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" ||
a.className == "com.hyperic.hq.bizapp.server.action.alert.SnmpAction") {
@@ -215,6 +242,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))
+ }
} else if (showAllActions) {
AlertAction(id: a.id,
className: a.className)
@@ -717,6 +756,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'
@@ -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.
@@ -469,4 +471,189 @@ public static AlertAction createSnmpAction(String address,
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);
+ }
}
Oops, something went wrong.

0 comments on commit 2c7901e

Please sign in to comment.