Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI commands for ServerConfigApi.
- Loading branch information
Ryan Morgan
committed
Jul 8, 2009
1 parent
1a3fb79
commit 0ce8e51
Showing
3 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/org/hyperic/hq/hqapi1/tools/ServerConfigCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters