From 53a09c31d0d2fd49b9134eca33cabd83a0efd09e Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Tue, 3 Aug 2010 11:15:45 -0700 Subject: [PATCH] [HHQ-4175] Add EmailAction support for alert definitions. --- ChangeLog | 2 + .../app/AlertdefinitionController.groovy | 66 ++- .../hq/hqapi1/AlertDefinitionBuilder.java | 187 +++++++ .../AlertDefinitionSyncEmailAction_test.java | 478 ++++++++++++++++++ .../hqapi1/tools/AlertDefinitionCommand.java | 54 +- 5 files changed, 785 insertions(+), 2 deletions(-) create mode 100644 src/org/hyperic/hq/hqapi1/test/AlertDefinitionSyncEmailAction_test.java diff --git a/ChangeLog b/ChangeLog index aee19700..e76acb0e 100644 --- a/ChangeLog +++ b/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. diff --git a/hqu/hqapi1/app/AlertdefinitionController.groovy b/hqu/hqapi1/app/AlertdefinitionController.groovy index 14ac9a83..70386ad3 100644 --- a/hqu/hqapi1/app/AlertdefinitionController.groovy +++ b/hqu/hqapi1/app/AlertdefinitionController.groovy @@ -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, @@ -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, @@ -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)) + } } } } @@ -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' diff --git a/src/org/hyperic/hq/hqapi1/AlertDefinitionBuilder.java b/src/org/hyperic/hq/hqapi1/AlertDefinitionBuilder.java index 74cbd800..9824cb20 100644 --- a/src/org/hyperic/hq/hqapi1/AlertDefinitionBuilder.java +++ b/src/org/hyperic/hq/hqapi1/AlertDefinitionBuilder.java @@ -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. @@ -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); + } } diff --git a/src/org/hyperic/hq/hqapi1/test/AlertDefinitionSyncEmailAction_test.java b/src/org/hyperic/hq/hqapi1/test/AlertDefinitionSyncEmailAction_test.java new file mode 100644 index 00000000..5fcafc44 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/AlertDefinitionSyncEmailAction_test.java @@ -0,0 +1,478 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.AlertDefinitionApi; +import org.hyperic.hq.hqapi1.AlertDefinitionBuilder; +import org.hyperic.hq.hqapi1.HQApi; +import org.hyperic.hq.hqapi1.types.AlertAction; +import org.hyperic.hq.hqapi1.types.AlertActionConfig; +import org.hyperic.hq.hqapi1.types.AlertDefinition; +import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse; +import org.hyperic.hq.hqapi1.types.Metric; +import org.hyperic.hq.hqapi1.types.MetricsResponse; +import org.hyperic.hq.hqapi1.types.Resource; +import org.hyperic.hq.hqapi1.types.Role; +import org.hyperic.hq.hqapi1.types.User; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class AlertDefinitionSyncEmailAction_test extends AlertDefinitionTestBase { + + public AlertDefinitionSyncEmailAction_test(String name) { + super(name); + } + + private AlertDefinition generateDefinition(HQApi api) throws Exception { + + Resource platform = getLocalPlatformResource(false, false); + + MetricsResponse metricsResponse = api.getMetricApi().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())); + + return d; + } + + public void testAddUserNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + List users = createTestUsers(1); + User u = users.get(0); + + AlertDefinitionBuilder.addEmailAction(d, users.toArray(new User[users.size()])); + List definitions = new ArrayList(); + 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()); + assertTrue("Wrong action class", + "com.hyperic.hq.bizapp.server.action.email.EmailAction". + equals(syncedDef.getAlertAction().get(0).getClassName())); + + for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) { + if (c.getKey().equals("names")) { + assertEquals("Wrong user name", u.getName(), c.getValue()); + } + } + + // 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()); + deleteTestUsers(users); + } + + public void testAddMultiUserNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + List users = createTestUsers(10); + + AlertDefinitionBuilder.addEmailAction(d, users.toArray(new User[users.size()])); + + List definitions = new ArrayList(); + 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()); + assertTrue("Wrong action class", + "com.hyperic.hq.bizapp.server.action.email.EmailAction". + equals(syncedDef.getAlertAction().get(0).getClassName())); + + for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) { + if (c.getKey().equals("names")) { + for (User u : users) { + assertTrue("User " + u.getName() + " not found in notify list!", + c.getValue().indexOf(u.getName()) != -1); + } + } + } + + // 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()); + deleteTestUsers(users); + } + + public void testAddNonExistantUserNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + User u = new User(); + u.setName("Non-existant user!"); + + AlertDefinitionBuilder.addEmailAction(d, new User[] { u }); + + List definitions = new ArrayList(); + definitions.add(d); + AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions); + hqAssertSuccess(response); + + cleanup(response.getAlertDefinition()); + } + + public void testAddNonExistantUsersNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + User u = new User(); + u.setName("Non-existant user!"); + User u2 = new User(); + u2.setName("Non-existant user!"); + + AlertDefinitionBuilder.addEmailAction(d, new User[] { u, u2 }); + + List definitions = new ArrayList(); + definitions.add(d); + AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions); + hqAssertSuccess(response); + + cleanup(response.getAlertDefinition()); + } + + public void testAddMultiUserSomeInvalidNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + List users = createTestUsers(10); + + User invalidUser = new User(); + invalidUser.setName("Invalid User!"); + + User[] userList = users.toArray(new User[users.size()+1]); + userList[users.size()] = invalidUser; + + AlertDefinitionBuilder.addEmailAction(d, userList); + + List definitions = new ArrayList(); + 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()); + assertTrue("Wrong action class", + "com.hyperic.hq.bizapp.server.action.email.EmailAction". + equals(syncedDef.getAlertAction().get(0).getClassName())); + + for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) { + if (c.getKey().equals("names")) { + for (User u : users) { + assertTrue("User " + u.getName() + " not found in notify list!", + c.getValue().indexOf(u.getName()) != -1); + } + } + } + + // 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()); + deleteTestUsers(users); + } + + public void testAddRoleNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + Role r = createRole(Collections.EMPTY_LIST, Collections.EMPTY_LIST); + + AlertDefinitionBuilder.addEmailAction(d, new Role[] {r}); + + List definitions = new ArrayList(); + 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()); + assertTrue("Wrong action class", + "com.hyperic.hq.bizapp.server.action.email.EmailAction". + equals(syncedDef.getAlertAction().get(0).getClassName())); + + for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) { + if (c.getKey().equals("names")) { + assertEquals("Wrong role name", r.getName(), c.getValue()); + } + } + + // 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()); + cleanupRole(r); + } + + public void testAddMultiRoleNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + Role r = createRole(Collections.EMPTY_LIST, Collections.EMPTY_LIST); + Role r2 = createRole(Collections.EMPTY_LIST, Collections.EMPTY_LIST); + + AlertDefinitionBuilder.addEmailAction(d, new Role[] {r, r2}); + + List definitions = new ArrayList(); + 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()); + assertTrue("Wrong action class", + "com.hyperic.hq.bizapp.server.action.email.EmailAction". + equals(syncedDef.getAlertAction().get(0).getClassName())); + + for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) { + if (c.getKey().equals("names")) { + assertTrue("Role name not found in list", c.getValue().indexOf(r.getName()) != -1); + assertTrue("Role name not found in list", c.getValue().indexOf(r2.getName()) != -1); + } + } + + // 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()); + cleanupRole(r); + cleanupRole(r2); + } + + public void testAddNonExistantRoleNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + Role r = new Role(); + r.setName("Non-existant role!"); + + AlertDefinitionBuilder.addEmailAction(d, new Role[] { r }); + + List definitions = new ArrayList(); + definitions.add(d); + AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions); + hqAssertSuccess(response); + + cleanup(response.getAlertDefinition()); + } + + public void testAddNonExistantRolesNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + Role r = new Role(); + r.setName("Non-existant role!"); + Role r2 = new Role(); + r2.setName("Non-existant role!"); + + AlertDefinitionBuilder.addEmailAction(d, new Role[] { r, r2 }); + + List definitions = new ArrayList(); + definitions.add(d); + AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions); + hqAssertSuccess(response); + + cleanup(response.getAlertDefinition()); + } + + public void testAddMultiRoleSomeInvalidNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + Role r = createRole(Collections.EMPTY_LIST, Collections.EMPTY_LIST); + Role r2 = createRole(Collections.EMPTY_LIST, Collections.EMPTY_LIST); + Role r3 = new Role(); + r3.setName("Non-existant role!"); + + AlertDefinitionBuilder.addEmailAction(d, new Role[] {r, r2, r3}); + + List definitions = new ArrayList(); + 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()); + assertTrue("Wrong action class", + "com.hyperic.hq.bizapp.server.action.email.EmailAction". + equals(syncedDef.getAlertAction().get(0).getClassName())); + + for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) { + if (c.getKey().equals("names")) { + assertTrue("Role name not found in list", c.getValue().indexOf(r.getName()) != -1); + assertTrue("Role name not found in list", c.getValue().indexOf(r2.getName()) != -1); + } + } + + // 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()); + cleanupRole(r); + cleanupRole(r2); + } + + public void testAddOtherRecipientNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + final String EMAIL = "hqapi@vmware.com"; + AlertDefinitionBuilder.addEmailAction(d, new String[] {EMAIL}); + + List definitions = new ArrayList(); + 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()); + assertTrue("Wrong action class", + "com.hyperic.hq.bizapp.server.action.email.EmailAction". + equals(syncedDef.getAlertAction().get(0).getClassName())); + + for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) { + if (c.getKey().equals("names")) { + assertEquals("Wrong email", EMAIL, c.getValue()); + } + } + + // 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()); + } + + public void testAddOtherRecipientsNotification() throws Exception { + HQApi api = getApi(); + AlertDefinitionApi defApi = api.getAlertDefinitionApi(); + + AlertDefinition d = generateDefinition(api); + + final String EMAIL = "hqapi@vmware.com"; + final String EMAIL2 = "test@vmare.com"; + + AlertDefinitionBuilder.addEmailAction(d, new String[] {EMAIL,EMAIL2}); + + List definitions = new ArrayList(); + 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()); + assertTrue("Wrong action class", + "com.hyperic.hq.bizapp.server.action.email.EmailAction". + equals(syncedDef.getAlertAction().get(0).getClassName())); + + for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) { + if (c.getKey().equals("names")) { + assertTrue("Email not found in list", c.getValue().indexOf(EMAIL) != -1); + assertTrue("Email not found in list", c.getValue().indexOf(EMAIL2) != -1); + } + } + + // 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()); + } +} diff --git a/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java b/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java index 0cd8af73..705953e9 100644 --- a/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java +++ b/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java @@ -38,15 +38,19 @@ 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.ResourcesResponse; +import org.hyperic.hq.hqapi1.types.Role; +import org.hyperic.hq.hqapi1.types.RoleResponse; import org.hyperic.hq.hqapi1.types.StatusResponse; import org.hyperic.hq.hqapi1.types.EscalationResponse; import org.hyperic.hq.hqapi1.types.Escalation; import org.hyperic.hq.hqapi1.types.AlertCondition; +import org.hyperic.hq.hqapi1.types.User; +import org.hyperic.hq.hqapi1.types.UserResponse; import java.io.InputStream; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Iterator; @@ -81,6 +85,9 @@ public class AlertDefinitionCommand extends Command { private static String OPT_ASSIGN_CONTROLACTION = "assignControlAction"; private static String OPT_CLEAR_ESC = "clearEscalation"; private static String OPT_CLEAR_ACTIONS = "clearActions"; + private static String OPT_ASSIGN_USER_NOTIFICATION = "assignUserNotification"; + private static String OPT_ASSIGN_ROLE_NOTIFICATION = "assignRoleNotification"; + private static String OPT_ASSIGN_OTHER_NOTIFICATION = "assignOtherNotification"; private void printUsage() { System.err.println("One of " + Arrays.toString(COMMANDS) + " required"); @@ -307,6 +314,15 @@ private void sync(String[] args) throws Exception { "all alert definitions in this sync"); p.accepts(OPT_CLEAR_ACTIONS, "If specified, clear alert actions from " + "all alert definitions in this sync"); + p.accepts(OPT_ASSIGN_USER_NOTIFICATION, "If specified, assign notification to the given " + + "comma separated list of users"). + withRequiredArg().ofType(String.class); + p.accepts(OPT_ASSIGN_ROLE_NOTIFICATION, "If specified, assign notification to the given " + + "comma separated list of roles"). + withRequiredArg().ofType(String.class); + p.accepts(OPT_ASSIGN_OTHER_NOTIFICATION, "If specified, assign notification to the given " + + "comma separated list of email addresses"). + withRequiredArg().ofType(String.class); OptionSet options = getOptions(p, args); @@ -368,6 +384,42 @@ private void sync(String[] args) throws Exception { } } + if (options.has(OPT_ASSIGN_USER_NOTIFICATION)) { + String[] users = ((String)getRequired(options, OPT_ASSIGN_USER_NOTIFICATION)).split(","); + List checkedUsers = new ArrayList(); + for (String u : users) { + UserResponse uResponse = api.getUserApi().getUser(u); + checkSuccess(uResponse); + checkedUsers.add(uResponse.getUser()); + } + + for (AlertDefinition def : definitions) { + AlertDefinitionBuilder.addEmailAction(def, checkedUsers.toArray(new User[checkedUsers.size()])); + } + } + + if (options.has(OPT_ASSIGN_ROLE_NOTIFICATION)) { + String[] roles = ((String)getRequired(options, OPT_ASSIGN_ROLE_NOTIFICATION)).split(","); + ArrayList checkedRoles = new ArrayList(); + for (String r : roles) { + RoleResponse rResponse = api.getRoleApi().getRole(r); + checkSuccess(rResponse); + checkedRoles.add(rResponse.getRole()); + } + + for (AlertDefinition def : definitions) { + AlertDefinitionBuilder.addEmailAction(def, checkedRoles.toArray(new Role[checkedRoles.size()])); + } + } + + if (options.has(OPT_ASSIGN_OTHER_NOTIFICATION)) { + String[] emails = ((String)getRequired(options, OPT_ASSIGN_OTHER_NOTIFICATION)).split(","); + + for (AlertDefinition def : definitions) { + AlertDefinitionBuilder.addEmailAction(def, emails); + } + } + System.out.println("Syncing " + definitions.size() + " alert definitions"); int numSynced = 0;