Skip to content

Commit

Permalink
Merge branch 'HHQ-3144'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Jul 8, 2009
2 parents 4269814 + 75ed80c commit 32b676a
Show file tree
Hide file tree
Showing 10 changed files with 526 additions and 0 deletions.
60 changes: 60 additions & 0 deletions hqu/hqapi1/app/ServerconfigController.groovy
@@ -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()
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/org/hyperic/hq/hqapi1/HQApi.java
Expand Up @@ -45,6 +45,7 @@ public class HQApi {
private final AlertDefinitionApi _alertDefinitionApi;
private final MaintenanceApi _maintenanceApi;
private final ResourceEdgeApi _resourceEdgeApi;
private final ServerConfigApi _serverConfigApi;

/**
* @param host The hostname of the HQ Server to connect to.
Expand All @@ -68,6 +69,7 @@ public HQApi(String host, int port, boolean isSecure, String user,
_alertDefinitionApi = new AlertDefinitionApi(connection);
_maintenanceApi = new MaintenanceApi(connection);
_resourceEdgeApi = new ResourceEdgeApi(connection);
_serverConfigApi = new ServerConfigApi(connection);
}

/**
Expand Down Expand Up @@ -169,4 +171,13 @@ public MaintenanceApi getMaintenanceApi() {
public ResourceEdgeApi getResourceEdgeApi() {
return _resourceEdgeApi;
}

/**
* Manipulate HQ server configuration settings
*
* @return The API for modifying HQ server settings
*/
public ServerConfigApi getServerConfigApi() {
return _serverConfigApi;
}
}
50 changes: 50 additions & 0 deletions src/org/hyperic/hq/hqapi1/ServerConfigApi.java
@@ -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);
}
}
57 changes: 57 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/ServerConfigGet_test.java
@@ -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 src/org/hyperic/hq/hqapi1/test/ServerConfigSet_test.java
@@ -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);
}
}
30 changes: 30 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/WADLServerConfig_test.java
@@ -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);
}
}

0 comments on commit 32b676a

Please sign in to comment.