Skip to content

Commit

Permalink
Merge branch 'hqapi-2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Mar 8, 2010
2 parents e688e1e + fea2fa5 commit c8698ea
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Expand Up @@ -59,6 +59,9 @@ Changes in HQApi 3.0

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.
Expand Down
79 changes: 75 additions & 4 deletions src/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +69,13 @@ public class AlertDefinitionCommand extends AbstractCommand {
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");
}
Expand Down Expand Up @@ -248,10 +257,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);

Expand All @@ -260,6 +284,53 @@ private void sync(String[] args) throws Exception {

List<AlertDefinition> 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;
Expand All @@ -273,16 +344,16 @@ 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 " +
(System.currentTimeMillis() - start) + " ms");

}
} else {
AlertDefinitionsResponse syncResponse = api.syncAlertDefinitions(definitions);
AlertDefinitionsResponse syncResponse = adApi.syncAlertDefinitions(definitions);
checkSuccess(syncResponse);
numSynced = definitions.size();
}
Expand Down

0 comments on commit c8698ea

Please sign in to comment.