Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
526 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import org.hyperic.hq.common.server.session.ServerConfigManagerEJBImpl as SMan | ||
import org.hyperic.hq.hqapi1.ErrorCode | ||
|
||
class ServerconfigController extends ApiController { | ||
|
||
private _serverMan = SMan.one | ||
|
||
def getConfig(params) { | ||
|
||
def props = _serverMan.config | ||
|
||
renderXml() { | ||
ServerConfigResponse() { | ||
if (!user.isSuperUser()) { | ||
out << getFailureXML(ErrorCode.PERMISSION_DENIED, | ||
"User " + user.name + " is not superuser") | ||
} else { | ||
out << getSuccessXML() | ||
for (key in props.keySet().sort {a, b -> a <=> b}) { | ||
ServerConfig(key: key, value: props.getProperty(key)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
def setConfig(params) { | ||
|
||
def failureXml = null | ||
|
||
if (!user.isSuperUser()) { | ||
failureXml = getFailureXML(ErrorCode.PERMISSION_DENIED, | ||
"User " + user.name + " is not superuser") | ||
} else { | ||
|
||
Properties props = new Properties() | ||
def postData = new XmlParser().parseText(getPostData()) | ||
for (xmlConfig in postData['ServerConfig']) { | ||
props.put(xmlConfig.'@key', xmlConfig.'@value') | ||
} | ||
|
||
try { | ||
_serverMan.setConfig(user, props) | ||
} catch (Exception e) { | ||
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS, | ||
e.getMessage()) | ||
} | ||
} | ||
|
||
renderXml() { | ||
StatusResponse() { | ||
if (failureXml) { | ||
out << failureXml | ||
} else { | ||
out << getSuccessXML() | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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,50 @@ | ||
package org.hyperic.hq.hqapi1; | ||
|
||
import org.hyperic.hq.hqapi1.types.ServerConfigResponse; | ||
import org.hyperic.hq.hqapi1.types.ServerConfig; | ||
import org.hyperic.hq.hqapi1.types.ServerConfigRequest; | ||
import org.hyperic.hq.hqapi1.types.StatusResponse; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
/** | ||
* The Hyperic HQ Server Configuration API. | ||
*/ | ||
public class ServerConfigApi extends BaseApi { | ||
|
||
ServerConfigApi(HQConnection conn) { | ||
super(conn); | ||
} | ||
|
||
/** | ||
* Get the HQ server configuration. | ||
* | ||
* @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}, | ||
* a List of {@link org.hyperic.hq.hqapi1.types.ServerConfig}s. | ||
* | ||
* @throws IOException If a network error occurs while making the request. | ||
*/ | ||
public ServerConfigResponse getConfig() throws IOException { | ||
return doGet("serverconfig/getConfig.hqu", new HashMap<String, String[]>(), | ||
ServerConfigResponse.class); | ||
} | ||
|
||
/** | ||
* Set the HQ server configuration. The List of ServerConfig's must include | ||
* all configurations returned from #getConfig. | ||
* | ||
* @param configs An array of ServerConfig objects. | ||
* | ||
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if | ||
* the server configuration was updated sucessfully. | ||
* | ||
* @throws IOException If a network error occurs while making the request. | ||
*/ | ||
public StatusResponse setConfig(List<ServerConfig> configs) throws IOException { | ||
ServerConfigRequest request = new ServerConfigRequest(); | ||
request.getServerConfig().addAll(configs); | ||
return doPost("serverConfig/setConfig.hqu", request, StatusResponse.class); | ||
} | ||
} |
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,57 @@ | ||
package org.hyperic.hq.hqapi1.test; | ||
|
||
import org.hyperic.hq.hqapi1.ServerConfigApi; | ||
import org.hyperic.hq.hqapi1.HQApi; | ||
import org.hyperic.hq.hqapi1.UserApi; | ||
import org.hyperic.hq.hqapi1.types.ServerConfigResponse; | ||
import org.hyperic.hq.hqapi1.types.User; | ||
import org.hyperic.hq.hqapi1.types.StatusResponse; | ||
import org.hyperic.hq.hqapi1.types.UserResponse; | ||
|
||
import java.util.Random; | ||
|
||
public class ServerConfigGet_test extends HQApiTestBase { | ||
|
||
public ServerConfigGet_test(String name) { | ||
super(name); | ||
} | ||
|
||
public void testGetConfig() throws Exception { | ||
|
||
ServerConfigApi sApi = getApi().getServerConfigApi(); | ||
|
||
ServerConfigResponse response = sApi.getConfig(); | ||
hqAssertSuccess(response); | ||
assertTrue("No server configuration settings found", | ||
response.getServerConfig().size() > 0); | ||
} | ||
|
||
public void testGetConfigInvalidUser() throws Exception { | ||
|
||
HQApi api = getApi(); | ||
UserApi uApi = api.getUserApi(); | ||
|
||
Random r = new Random(); | ||
|
||
User user = new User(); | ||
user.setName("test" + r.nextInt()); | ||
user.setFirstName("Get Server Config"); | ||
user.setLastName("Test User"); | ||
user.setEmailAddress("testuser@springsource.com"); | ||
user.setActive(true); | ||
|
||
UserResponse userCreateResponse = | ||
uApi.createUser(user, "test"); // Create test user w/o Admin | ||
hqAssertSuccess(userCreateResponse); | ||
|
||
ServerConfigApi sApi = getApi(user.getName(), "test").getServerConfigApi(); | ||
|
||
ServerConfigResponse response = sApi.getConfig(); | ||
hqAssertFailurePermissionDenied(response); | ||
|
||
// Cleanup | ||
StatusResponse deleteResponse = | ||
uApi.deleteUser(userCreateResponse.getUser().getId()); | ||
hqAssertSuccess(deleteResponse); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/org/hyperic/hq/hqapi1/test/ServerConfigSet_test.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,107 @@ | ||
package org.hyperic.hq.hqapi1.test; | ||
|
||
import org.hyperic.hq.hqapi1.ServerConfigApi; | ||
import org.hyperic.hq.hqapi1.HQApi; | ||
import org.hyperic.hq.hqapi1.types.ServerConfigResponse; | ||
import org.hyperic.hq.hqapi1.types.StatusResponse; | ||
import org.hyperic.hq.hqapi1.types.User; | ||
import org.hyperic.hq.hqapi1.types.UserResponse; | ||
import org.hyperic.hq.hqapi1.types.ServerConfig; | ||
|
||
import java.util.Random; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ServerConfigSet_test extends HQApiTestBase { | ||
|
||
public ServerConfigSet_test(String name) { | ||
super(name); | ||
} | ||
|
||
public void testSetAllConfig() throws Exception { | ||
|
||
ServerConfigApi sApi = getApi().getServerConfigApi(); | ||
|
||
ServerConfigResponse configResponse = sApi.getConfig(); | ||
hqAssertSuccess(configResponse); | ||
|
||
StatusResponse response = sApi.setConfig(configResponse.getServerConfig()); | ||
hqAssertSuccess(response); | ||
} | ||
|
||
public void testSetSingleConfig() throws Exception { | ||
|
||
ServerConfigApi sApi = getApi().getServerConfigApi(); | ||
|
||
ServerConfigResponse configResponse = sApi.getConfig(); | ||
hqAssertSuccess(configResponse); | ||
|
||
List<ServerConfig> configs = configResponse.getServerConfig(); | ||
for (ServerConfig config : configs) { | ||
if (config.getKey().equals("HQ_ALERTS_ENABLED")) { | ||
config.setValue("false"); | ||
} | ||
} | ||
|
||
StatusResponse response = sApi.setConfig(configs); | ||
hqAssertSuccess(response); | ||
|
||
// Validate update of HQ_ALERTS_ENABLED | ||
configResponse = sApi.getConfig(); | ||
hqAssertSuccess(configResponse); | ||
|
||
configs = configResponse.getServerConfig(); | ||
for (ServerConfig config : configs) { | ||
if (config.getKey().equals("HQ_ALERTS_ENABLED")) { | ||
assertTrue("HQ_ALERTS_ENABLED was not false", | ||
config.getValue().equals("false")); | ||
config.setValue("true"); // Re-enable | ||
} | ||
} | ||
|
||
response = sApi.setConfig(configs); | ||
hqAssertSuccess(response); | ||
} | ||
|
||
public void testSetConfigEmpty() throws Exception { | ||
|
||
ServerConfigApi sApi = getApi().getServerConfigApi(); | ||
|
||
List<ServerConfig> configs = new ArrayList<ServerConfig>(); | ||
|
||
StatusResponse response = sApi.setConfig(configs); | ||
// All configs required | ||
hqAssertFailureInvalidParameters(response); | ||
} | ||
|
||
public void testSetConfigInvalidUser() throws Exception { | ||
|
||
HQApi api = getApi(); | ||
|
||
Random r = new Random(); | ||
|
||
User user = new User(); | ||
user.setName("test" + r.nextInt()); | ||
user.setFirstName("Get Server Config"); | ||
user.setLastName("Test User"); | ||
user.setEmailAddress("testuser@springsource.com"); | ||
user.setActive(true); | ||
|
||
UserResponse userCreateResponse = | ||
api.getUserApi().createUser(user, "test"); // Create test user w/o Admin | ||
hqAssertSuccess(userCreateResponse); | ||
|
||
ServerConfigResponse response = api.getServerConfigApi().getConfig(); | ||
hqAssertSuccess(response); | ||
|
||
// Re-sync with invalid user | ||
ServerConfigApi sApi = getApi(user.getName(), "test").getServerConfigApi(); | ||
StatusResponse putResponse = sApi.setConfig(response.getServerConfig()); | ||
hqAssertFailurePermissionDenied(putResponse); | ||
|
||
// Cleanup | ||
StatusResponse deleteResponse = | ||
api.getUserApi().deleteUser(userCreateResponse.getUser().getId()); | ||
hqAssertSuccess(deleteResponse); | ||
} | ||
} |
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,30 @@ | ||
package org.hyperic.hq.hqapi1.test; | ||
|
||
import org.hyperic.hq.hqapi1.wadl.*; | ||
|
||
public class WADLServerConfig_test extends WADLTestBase { | ||
|
||
public void testGet() throws Exception { | ||
Endpoint.ServerConfigGetConfigHqu get = | ||
new Endpoint.ServerConfigGetConfigHqu(); | ||
|
||
ServerConfigResponse response = get.getAsServerConfigResponse(); | ||
hqAssertSuccess(response); | ||
} | ||
|
||
public void testSet() throws Exception { | ||
Endpoint.ServerConfigGetConfigHqu get = | ||
new Endpoint.ServerConfigGetConfigHqu(); | ||
Endpoint.ServerConfigSetConfigHqu set = | ||
new Endpoint.ServerConfigSetConfigHqu(); | ||
|
||
ServerConfigResponse response = get.getAsServerConfigResponse(); | ||
hqAssertSuccess(response); | ||
|
||
ServerConfigRequest request = new ServerConfigRequest(); | ||
request.getServerConfig().addAll(response.getServerConfig()); | ||
|
||
StatusResponse setResponse = set.postAsStatusResponse(request); | ||
hqAssertSuccess(setResponse); | ||
} | ||
} |
Oops, something went wrong.