Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/hyperic/hqapi
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Nguyen committed Apr 29, 2010
2 parents 1368498 + 8f78e3e commit 5da05cd
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 5 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Expand Up @@ -79,6 +79,8 @@ Changes in HQApi 3.0

Changes in HQApi 2.4

*) [HHQ-3924] Add agent ping CLI command.

*) [HHQ-3786] Allow for listing of alerts from the CLI based on resource
description.

Expand Down
13 changes: 12 additions & 1 deletion hqu/hqapi1/app/ResourceController.groovy
Expand Up @@ -661,8 +661,19 @@ class ResourceController extends ApiController {
}
}

def existingConfig = resource.getConfig()
// 2nd pass over configuration to unset any variables that
// may already exist in the existing config, but are empty in
// the configuration being set.
xmlResource['ResourceConfig'].each {
if ((it.'@value' && it.'@value'.length() > 0) ||
(it.'@value' != null && !it.'@value'.equals(existingConfig[it.'@key']?.value))) {
config[it.'@key'] = it.'@value'
}
}

// Update
if (!configsEqual(resource.getConfig(), config)) {
if (!configsEqual(existingConfig, config)) {
try {
resource.setConfig(config, user)
} catch (Throwable t) {
Expand Down
73 changes: 72 additions & 1 deletion src/org/hyperic/hq/hqapi1/test/ResourceUpdate_test.java
Expand Up @@ -95,15 +95,86 @@ public void testUpdateConfig() throws Exception {
hqAssertSuccess(updateResponse);

ResourceResponse getResponse = api.getResource(createdResource.getId(),
false, false);
true, false);
hqAssertSuccess(getResponse);

Resource updatedResource = getResponse.getResource();
assertTrue("No configuration found for " + updatedResource.getName(),
updatedResource.getResourceConfig().size() > 0);
boolean foundHostname = false;
for (ResourceConfig c : updatedResource.getResourceConfig()) {
if (c.getKey().equals("hostname")) {
assertEquals(c.getValue(), UPDATED_HOSTNAME);
foundHostname = true;
}
}
assertTrue("Unable to find hostname configuration for " + updatedResource.getName(),
foundHostname);

// Cannot delete resources soon after modifying them..
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// Ignore
}

// Cleanup
StatusResponse deleteResponse = api.deleteResource(updatedResource.getId());
hqAssertSuccess(deleteResponse);
}

public void testUpdateResetConfig() throws Exception {
ResourceApi api = getApi().getResourceApi();
Resource createdResource = createTestHTTPService();

// Add pattern attribute
ResourceConfig patternConfig = new ResourceConfig();
patternConfig.setKey("pattern");
patternConfig.setValue("html");
createdResource.getResourceConfig().add(patternConfig);

StatusResponse updateResponse = api.updateResource(createdResource);
hqAssertSuccess(updateResponse);

ResourceResponse getResponse = api.getResource(createdResource.getId(),
true, false);
hqAssertSuccess(getResponse);

Resource updatedResource = getResponse.getResource();
assertTrue("No configuration found for " + updatedResource.getName(),
updatedResource.getResourceConfig().size() > 0);
boolean foundPattern = false;
for (ResourceConfig c : updatedResource.getResourceConfig()) {
if (c.getKey().equals("pattern")) {
assertEquals(c.getValue(), "html");
// Reset to ""
c.setValue("");
foundPattern = true;
}
}

assertTrue("Unable to find pattern configuration for " + updatedResource.getName(),
foundPattern);

updateResponse = api.updateResource(updatedResource);
hqAssertSuccess(updateResponse);

getResponse = api.getResource(createdResource.getId(), true, false);
hqAssertSuccess(getResponse);

updatedResource = getResponse.getResource();
assertTrue("No configuration found for " + updatedResource.getName(),
updatedResource.getResourceConfig().size() > 0);
foundPattern = false;
for (ResourceConfig c : updatedResource.getResourceConfig()) {
if (c.getKey().equals("pattern")) {
assertEquals("pattern is not empty", c.getValue(), "");
foundPattern = true;
}
}

assertTrue("Unable to find pattern configuration for " + updatedResource.getName(),
foundPattern);

pauseTest();

Expand Down
86 changes: 83 additions & 3 deletions src/org/hyperic/hq/hqapi1/tools/AgentCommand.java
Expand Up @@ -31,17 +31,30 @@
import joptsimple.OptionSet;
import org.hyperic.hq.hqapi1.AgentApi;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.ResourceApi;
import org.hyperic.hq.hqapi1.XmlUtil;
import org.hyperic.hq.hqapi1.types.Agent;
import org.hyperic.hq.hqapi1.types.AgentResponse;
import org.hyperic.hq.hqapi1.types.AgentsResponse;
import org.springframework.stereotype.Component;

import org.hyperic.hq.hqapi1.types.PingAgentResponse;
import org.hyperic.hq.hqapi1.types.ResourceResponse;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

@Component
public class AgentCommand extends AbstractCommand {

private static String CMD_LIST = "list";
private static final String CMD_LIST = "list";
private static final String CMD_PING = "ping";

private static final String OPT_ID = "id";
private static final String OPT_ADDRESS = "agentAddress";
private static final String OPT_PORT = "agentPort";
private static final String OPT_FQDN = "fqdn";

private static String[] COMMANDS = { CMD_LIST };
private static String[] COMMANDS = { CMD_LIST, CMD_PING };

private void printUsage() {
System.err.println("One of " + Arrays.toString(COMMANDS) + " required");
Expand All @@ -60,6 +73,8 @@ public int handleCommand(String[] args) throws Exception {

if (args[0].equals(CMD_LIST)) {
list(trim(args));
} else if (args[0].equals(CMD_PING)) {
ping(trim(args));
} else {
printUsage();
return 1;
Expand All @@ -82,4 +97,69 @@ private void list(String[] args) throws Exception {

XmlUtil.serialize(agents, System.out, Boolean.TRUE);
}

private void printPingResponse(Agent a, PingAgentResponse response) {
if (response.isUp()) {
System.out.println("Successfully pinged agent " + a.getAddress() + ":" + a.getPort());
} else {
System.out.println("Failure pinging agent at " + a.getAddress() + ":" + a.getPort());
}
}

private void ping(String[] args) throws Exception {

OptionParser p = getOptionParser();

p.accepts(OPT_ID, "The id of the agent to ping").
withRequiredArg().ofType(Integer.class);
p.accepts(OPT_ADDRESS, "The address of the agent to ping. Must be used with --" + OPT_PORT).
withRequiredArg().ofType(String.class);
p.accepts(OPT_PORT, "The port of the agent to ping. Must be used with --" + OPT_ADDRESS).
withRequiredArg().ofType(Integer.class);
p.accepts(OPT_FQDN, "The platform FQDN of the agent to ping").
withRequiredArg().ofType(String.class);

OptionSet options = getOptions(p, args);

HQApi api = getApi(options);

AgentApi agentApi = api.getAgentApi();

if (options.has(OPT_ID)) {
Integer id = (Integer)options.valueOf(OPT_ID);
AgentResponse response = agentApi.getAgent(id);
checkSuccess(response);
PingAgentResponse pingResponse = agentApi.pingAgent(response.getAgent());
checkSuccess(pingResponse);
printPingResponse(response.getAgent(), pingResponse);
} else if (options.has(OPT_ADDRESS) && options.has(OPT_PORT)) {
String address = (String)options.valueOf(OPT_ADDRESS);
Integer port = (Integer)options.valueOf(OPT_PORT);
AgentResponse response = agentApi.getAgent(address, port);
checkSuccess(response);
PingAgentResponse pingResponse = agentApi.pingAgent(response.getAgent());
checkSuccess(pingResponse);
printPingResponse(response.getAgent(), pingResponse);
} else if (options.has(OPT_FQDN)) {
ResourceApi rApi = api.getResourceApi();
String fqdn = (String)options.valueOf(OPT_FQDN);
ResourceResponse resourceResponse = rApi.getPlatformResource(fqdn, false, false);
checkSuccess(resourceResponse);
Agent a = resourceResponse.getResource().getAgent();
PingAgentResponse pingResponse = agentApi.pingAgent(a);
checkSuccess(pingResponse);
printPingResponse(a, pingResponse);
} else {
// Ping via XML
InputStream is = getInputStream(options);

AgentsResponse resp = XmlUtil.deserialize(AgentsResponse.class, is);
List<Agent> agents = resp.getAgent();
for (Agent a : agents) {
PingAgentResponse pingResponse = agentApi.pingAgent(a);
checkSuccess(pingResponse);
printPingResponse(a, pingResponse);
}
}
}
}

0 comments on commit 5da05cd

Please sign in to comment.