diff --git a/src/org/hyperic/hq/hqapi1/ServerConfigApi.java b/src/org/hyperic/hq/hqapi1/ServerConfigApi.java index da6fe6cc..d535c653 100644 --- a/src/org/hyperic/hq/hqapi1/ServerConfigApi.java +++ b/src/org/hyperic/hq/hqapi1/ServerConfigApi.java @@ -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. diff --git a/src/org/hyperic/hq/hqapi1/tools/ServerConfigCommand.java b/src/org/hyperic/hq/hqapi1/tools/ServerConfigCommand.java new file mode 100644 index 00000000..c5c0fef3 --- /dev/null +++ b/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 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 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."); + } + } +} diff --git a/src/org/hyperic/hq/hqapi1/tools/Shell.java b/src/org/hyperic/hq/hqapi1/tools/Shell.java index c7b65ce8..d5dffbfb 100644 --- a/src/org/hyperic/hq/hqapi1/tools/Shell.java +++ b/src/org/hyperic/hq/hqapi1/tools/Shell.java @@ -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() {