From fea2fa5335e794304ea7293afad50e303297a8e9 Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Mon, 8 Mar 2010 12:46:33 -0800 Subject: [PATCH] [HHQ-3785] Add command lines options to alertdefinition sync command to allow for escalations and alert actions to be set and reset. --- ChangeLog | 3 + .../hqapi1/tools/AlertDefinitionCommand.java | 79 ++++++++++++++++++- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2d39561f..58442da1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,9 @@ Changes in HQApi 2.4 + *) [HHQ-3785] Add command lines options to alertdefinition sync command + to allow for escalations and alert actions to be set and reset. + *) [HHQ-3740] Allow hqapi.sh to be executed from a symlinked directory. *) Add support for --platform argument to command line group syncing. diff --git a/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java b/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java index a5dc64e0..eb87ade0 100644 --- a/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java +++ b/src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java @@ -30,9 +30,11 @@ import joptsimple.OptionParser; import joptsimple.OptionSet; import org.hyperic.hq.hqapi1.AlertDefinitionApi; +import org.hyperic.hq.hqapi1.AlertDefinitionBuilder; import org.hyperic.hq.hqapi1.HQApi; import org.hyperic.hq.hqapi1.XmlUtil; import org.hyperic.hq.hqapi1.EscalationApi; +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.StatusResponse; @@ -66,6 +68,13 @@ public class AlertDefinitionCommand extends Command { private static String OPT_COND_INCLUDE = "conditionTypeInclude"; private static String OPT_COND_EXCLUDE = "conditionTypeExclude"; + // Command line syncing options + private static String OPT_ASSIGN_ESC = "assignEscalation"; + private static String OPT_ASSIGN_SCRIPTACTION = "assignScriptAction"; + private static String OPT_ASSIGN_CONTROLACTION = "assignControlAction"; + private static String OPT_CLEAR_ESC = "clearEscalation"; + private static String OPT_CLEAR_ACTIONS = "clearActions"; + private void printUsage() { System.err.println("One of " + Arrays.toString(COMMANDS) + " required"); } @@ -242,10 +251,25 @@ private void sync(String[] args) throws Exception { p.accepts(OPT_BATCH_SIZE, "Process the sync in batches of the given size"). withRequiredArg().ofType(Integer.class); + p.accepts(OPT_ASSIGN_ESC, "If specified, assign the given Escalation " + + "to all alert definitions in this sync"). + withRequiredArg().ofType(String.class); + p.accepts(OPT_ASSIGN_SCRIPTACTION, "If specified, assign the given Escalation " + + "to all alert definitions in this sync"). + withRequiredArg().ofType(String.class); + p.accepts(OPT_ASSIGN_CONTROLACTION, "If specified, assign the given Escalation " + + "to all alert definitions in this sync"). + withRequiredArg().ofType(String.class); + p.accepts(OPT_CLEAR_ESC, "If specified, clear the assigned escalation from " + + "all alert definitions in this sync"); + p.accepts(OPT_CLEAR_ACTIONS, "If specified, clear alert actions from " + + "all alert definitions in this sync"); OptionSet options = getOptions(p, args); - AlertDefinitionApi api = getApi(options).getAlertDefinitionApi(); + HQApi api = getApi(options); + AlertDefinitionApi adApi = api.getAlertDefinitionApi(); + EscalationApi escApi = api.getEscalationApi(); InputStream is = getInputStream(options); @@ -254,6 +278,53 @@ private void sync(String[] args) throws Exception { List definitions = resp.getAlertDefinition(); + if (options.has(OPT_ASSIGN_ESC)) { + String esc = (String)getRequired(options, OPT_ASSIGN_ESC); + EscalationResponse escResponse = escApi.getEscalation(esc); + checkSuccess(escResponse); + System.out.println("Assigning escalation '" + esc + "' to all alert definitions"); + + for (AlertDefinition a : definitions) { + a.setEscalation(escResponse.getEscalation()); + } + } + + if (options.has(OPT_ASSIGN_SCRIPTACTION)) { + String script = (String)getRequired(options, OPT_ASSIGN_SCRIPTACTION); + AlertAction a = AlertDefinitionBuilder.createScriptAction(script); + System.out.println("Assigning script action '" + script + "' to all alert definitions"); + + for (AlertDefinition def : definitions) { + def.getAlertAction().add(a); + } + } + + if (options.has(OPT_ASSIGN_CONTROLACTION)) { + String action = (String)getRequired(options, OPT_ASSIGN_CONTROLACTION); + System.out.println("Assigning control action '" + action + "' to all alert definitions"); + + for (AlertDefinition def : definitions) { + AlertAction a = AlertDefinitionBuilder.createControlAction(def.getResource(), action); + def.getAlertAction().add(a); + } + } + + if (options.has(OPT_CLEAR_ESC)) { + System.out.println("Clearing escalations for all alert definitions"); + + for (AlertDefinition def : definitions) { + def.setEscalation(null); + } + } + + if (options.has(OPT_CLEAR_ACTIONS)) { + System.out.println("Clearing alert actions for all alert definitions"); + + for (AlertDefinition def : definitions) { + def.getAlertAction().clear(); + } + } + System.out.println("Syncing " + definitions.size() + " alert definitions"); int numSynced = 0; @@ -267,8 +338,8 @@ private void sync(String[] args) throws Exception { int toIndex = (fromIndex + batchSize) > definitions.size() ? definitions.size() : (fromIndex + batchSize); AlertDefinitionsResponse syncResponse = - api.syncAlertDefinitions(definitions.subList(fromIndex, - toIndex)); + adApi.syncAlertDefinitions(definitions.subList(fromIndex, + toIndex)); checkSuccess(syncResponse); numSynced += (toIndex - fromIndex); System.out.println("Synced batch " + (i + 1) + " of " + numBatches + " in " + @@ -276,7 +347,7 @@ private void sync(String[] args) throws Exception { } } else { - AlertDefinitionsResponse syncResponse = api.syncAlertDefinitions(definitions); + AlertDefinitionsResponse syncResponse = adApi.syncAlertDefinitions(definitions); checkSuccess(syncResponse); numSynced = definitions.size(); }