Skip to content

Commit

Permalink
Add CLI commands for ServerConfigApi.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Jul 8, 2009
1 parent 1a3fb79 commit 0ce8e51
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/org/hyperic/hq/hqapi1/ServerConfigApi.java
Expand Up @@ -4,12 +4,10 @@
import org.hyperic.hq.hqapi1.types.ServerConfig;
import org.hyperic.hq.hqapi1.types.ServerConfigRequest;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.ServerConfigResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* The Hyperic HQ Server Configuration API.
Expand Down
150 changes: 150 additions & 0 deletions src/org/hyperic/hq/hqapi1/tools/ServerConfigCommand.java
@@ -0,0 +1,150 @@
package org.hyperic.hq.hqapi1.tools;

import joptsimple.OptionParser;
import joptsimple.OptionSet;

import java.util.Arrays;
import java.util.List;
import java.io.InputStream;

import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.XmlUtil;
import org.hyperic.hq.hqapi1.ServerConfigApi;
import org.hyperic.hq.hqapi1.types.ServerConfigsResponse;
import org.hyperic.hq.hqapi1.types.ServerConfig;
import org.hyperic.hq.hqapi1.types.StatusResponse;

public class ServerConfigCommand extends Command {

private static String CMD_GET = "getConfig";
private static String CMD_GET_PARAMETER = "getConfigParameter";
private static String CMD_SET = "setConfig";
private static String CMD_SET_PARAMETER = "setConfigParameter";

private static String[] COMMANDS = { CMD_GET, CMD_GET_PARAMETER,
CMD_SET, CMD_SET_PARAMETER};

private static String OPT_KEY = "key";
private static String OPT_VALUE = "value";

private void printUsage() {
System.err.println("One of " + Arrays.toString(COMMANDS) + " required");
}

protected void handleCommand(String[] args) throws Exception {
if (args.length == 0) {
printUsage();
System.exit(-1);
}

if (args[0].equals(CMD_GET)) {
get(trim(args));
} else if (args[0].equals(CMD_GET_PARAMETER)) {
getParameter(trim(args));
} else if (args[0].equals(CMD_SET)) {
set(trim(args));
} else if (args[0].equals(CMD_SET_PARAMETER)) {
setParameter(trim(args));
} else {
printUsage();
System.exit(-1);
}
}

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

OptionParser p = getOptionParser();

OptionSet options = getOptions(p, args);

HQApi api = getApi(options);
ServerConfigApi configApi = api.getServerConfigApi();

ServerConfigsResponse response = configApi.getConfig();
checkSuccess(response);

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

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

OptionParser p = getOptionParser();

p.accepts(OPT_KEY, "The configuration parameter to retreive").
withRequiredArg().ofType(String.class);

OptionSet options = getOptions(p, args);

HQApi api = getApi(options);
ServerConfigApi configApi = api.getServerConfigApi();

String key = (String)getRequired(options, OPT_KEY);

ServerConfigsResponse response = configApi.getConfig();
checkSuccess(response);

for (ServerConfig c : response.getServerConfig()) {
if (c.getKey().equals(key)) {
System.out.println("Current value for " + key + " = " + c.getValue());
return;
}
}
System.err.print("Unknown configuration parameter " + key);
}

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

OptionParser p = getOptionParser();
OptionSet options = getOptions(p, args);

HQApi api = getApi(options);

ServerConfigApi serverConfigApi = api.getServerConfigApi();

InputStream is = getInputStream(options);
ServerConfigsResponse resp = XmlUtil.deserialize(ServerConfigsResponse.class, is);
List<ServerConfig> config = resp.getServerConfig();

StatusResponse response = serverConfigApi.setConfig(config);
checkSuccess(response);

System.out.println("Successfully updated HQ configuration.");
}

private void setParameter(String[] args) throws Exception {
OptionParser p = getOptionParser();

p.accepts(OPT_KEY, "The configuration parameter to set").
withRequiredArg().ofType(String.class);
p.accepts(OPT_VALUE, "The new value for the specified key").
withRequiredArg().ofType(String.class);

OptionSet options = getOptions(p, args);

HQApi api = getApi(options);
ServerConfigApi configApi = api.getServerConfigApi();

String key = (String)getRequired(options, OPT_KEY);
String value = (String)getRequired(options, OPT_VALUE);

ServerConfigsResponse response = configApi.getConfig();
checkSuccess(response);

List<ServerConfig> config = response.getServerConfig();
boolean found = false;
for (ServerConfig c : config) {
if (c.getKey().equals(key)) {
c.setValue(value);
found = true;
}
}

if (!found) {
System.err.print("Unknown configuration parameter " + key);
} else {
StatusResponse setResponse = configApi.setConfig(config);
checkSuccess(setResponse);
System.out.println("Successfully updated HQ configuration.");
}
}
}
1 change: 1 addition & 0 deletions src/org/hyperic/hq/hqapi1/tools/Shell.java
Expand Up @@ -48,6 +48,7 @@ public class Shell {
_commands.put("dependency", new ResourceEdgeCommand());
_commands.put("role", new RoleCommand());
_commands.put("user", new UserCommand());
_commands.put("serverConfig", new ServerConfigCommand());
}

private static void printHelp() {
Expand Down

0 comments on commit 0ce8e51

Please sign in to comment.