Skip to content

Commit

Permalink
[HHQ-3244] Add better error handling to Resource create and updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Jul 16, 2009
1 parent bfb0991 commit 4db7cf0
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 9 deletions.
36 changes: 27 additions & 9 deletions hqu/hqapi1/app/ResourceController.groovy
Expand Up @@ -38,6 +38,13 @@ class ResourceController extends ApiController {
}
}

private String getCause(Throwable t) {
while (t.getCause()) {
t = t.getCause()
}
return t.getMessage()
}

def getResourcePrototypes(params) {
def existing = params.getOne('existing')?.toBoolean()

Expand Down Expand Up @@ -132,14 +139,16 @@ class ResourceController extends ApiController {
try {
resource = prototype.createInstance(parent, resourceXml.'@name',
user, cfg, agent, ips)
} catch (Exception e) {
// TODO: Fix this
} catch (Throwable t) {
String cause = getCause(t)
renderXml() {
ResourceResponse() {
out << getFailureXML(ErrorCode.OBJECT_EXISTS);
out << getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Error creating '" +
resourceXml.'@name' + "': " + cause)
}
}
log.warn("Error creating resource", e)
log.warn("Error creating resource", t)
return
}

Expand Down Expand Up @@ -191,14 +200,16 @@ class ResourceController extends ApiController {
try {
resource = prototype.createInstance(parent, resourceXml.'@name',
user, cfg)
} catch (Exception e) {
// TODO: Fix this
} catch (Throwable t) {
String cause = getCause(t)
renderXml() {
ResourceResponse() {
out << getFailureXML(ErrorCode.OBJECT_EXISTS);
out << getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Error creating " +
resourceXml.'@name' + "': " + cause)
}
}
log.warn("Error creating resource", e)
log.warn("Error creating resource", t)
return
}

Expand Down Expand Up @@ -325,7 +336,14 @@ class ResourceController extends ApiController {
}

if (!configsEqual(resource.getConfig(), config)) {
resource.setConfig(config, user)
try {
resource.setConfig(config, user)
} catch (Throwable t) {
String cause = getCause(t)
return getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Error updating '" + name + "': " +
cause)
}
}

def xmlChildren = xmlResource['Resource']
Expand Down
45 changes: 45 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/ResourceCreatePlatform_test.java
Expand Up @@ -58,4 +58,49 @@ public void testCreatePlatform() throws Exception {
StatusResponse deleteResponse = api.deleteResource(resp.getResource().getId());
hqAssertSuccess(deleteResponse);
}

public void testCreatePlatformLongDescription() throws Exception {

Agent a = getRunningAgent();
ResourceApi api = getApi().getResourceApi();

ResourcePrototypeResponse protoResponse =
api.getResourcePrototype("Network Device");

Random r = new Random();
final String name = "Test Network Device" + r.nextInt();
final String fqdn = "apitest.hyperic.com";

Ip ip = new Ip();
ip.setAddress("10.0.0.1");

List<Ip> ips = new ArrayList<Ip>();
ips.add(ip);

String longDescription = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

Map<String,String> config = new HashMap<String,String>();
config.put("interface.index", "ifDescr");
config.put("snmpIp", "10.0.0.1");
config.put("snmpPort", "161");
config.put("snmpCommunity", "public");
config.put("snmpVersion", "v2c");
config.put("description", longDescription);

ResourceResponse resp =
api.createPlatform(a, protoResponse.getResourcePrototype(),
name, fqdn, ips, config);
hqAssertFailureInvalidParameters(resp);
}
}
39 changes: 39 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/ResourceCreateServer_test.java
Expand Up @@ -50,4 +50,43 @@ public void testCreateServer() throws Exception {
StatusResponse deleteResponse = api.deleteResource(resp.getResource().getId());
hqAssertSuccess(deleteResponse);
}

public void testCreateServerLongDescription() throws Exception {

ResourceApi api = getApi().getResourceApi();

ResourcePrototypeResponse protoResponse =
api.getResourcePrototype("Apache httpd");
hqAssertSuccess(protoResponse);

Resource parent = getLocalPlatformResource(false, false);

String longDescription = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

Random r = new Random();
final String name = "Test Apache Server" + r.nextInt();

Map<String,String> config = new HashMap<String,String>();
config.put("hostname", "localhost");
config.put("port", "80");
config.put("path", "/server-status");
config.put("sotimeout", "10");
config.put("description", longDescription);

ResourceResponse resp =
api.createServer(protoResponse.getResourcePrototype(),
parent, name, config);
hqAssertFailureInvalidParameters(resp);
}
}
51 changes: 51 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/ResourceCreateService_test.java
Expand Up @@ -3,6 +3,15 @@
import org.hyperic.hq.hqapi1.ResourceApi;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.Agent;
import org.hyperic.hq.hqapi1.types.ResourcePrototypeResponse;
import org.hyperic.hq.hqapi1.types.ResourcePrototype;
import org.hyperic.hq.hqapi1.types.ResourcesResponse;
import org.hyperic.hq.hqapi1.types.ResourceResponse;

import java.util.Map;
import java.util.HashMap;
import java.util.Random;

public class ResourceCreateService_test extends ResourceTestBase {

Expand All @@ -19,4 +28,46 @@ public void testServiceCreate() throws Exception {
StatusResponse deleteResponse = api.deleteResource(createdResource.getId());
hqAssertSuccess(deleteResponse);
}

public void testServiceCreateInvalidDescription() throws Exception {

Agent a = getRunningAgent();

ResourceApi api = getApi().getResourceApi();

// Find HTTP resource type
ResourcePrototypeResponse protoResponse = api.getResourcePrototype("HTTP");
hqAssertSuccess(protoResponse);
ResourcePrototype pt = protoResponse.getResourcePrototype();

// Find local platform
ResourcesResponse resourcesResponse = api.getResources(a, false, false);
hqAssertSuccess(resourcesResponse);
assertTrue("Did not find a single platform for " + a.getAddress() + ":" +
a.getPort(), resourcesResponse.getResource().size() == 1);
Resource platform = resourcesResponse.getResource().get(0);

String longDescription = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

// Configure service
Map<String,String> params = new HashMap<String,String>();
params.put("hostname", "www.hyperic.com");
params.put("port", "80");
params.put("sotimeout", "10");
params.put("path", "/");
params.put("method", "GET");
params.put("description", longDescription);

Random r = new Random();
String name = "My HTTP Check " + r.nextInt();

ResourceResponse resp = api.createService(pt, platform, name, params);
hqAssertFailureInvalidParameters(resp);
}
}
30 changes: 30 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/ResourceUpdate_test.java
Expand Up @@ -157,5 +157,35 @@ public void testUpdateInvalidChildResource() throws Exception {
StatusResponse updateResponse = api.updateResource(platform);
hqAssertFailureObjectNotFound(updateResponse);
}

public void testUpdateInvalidDescription() throws Exception {

ResourceApi api = getApi().getResourceApi();
Resource createdResource = createTestHTTPService();

String longDescription = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

createdResource.setDescription(longDescription);

StatusResponse updateResponse = api.updateResource(createdResource);
hqAssertFailureInvalidParameters(updateResponse);

// Cannot delete resources soon after modifying them..
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// Ignore
}

// Cleanup
StatusResponse deleteResponse = api.deleteResource(createdResource.getId());
hqAssertSuccess(deleteResponse);
}
}

0 comments on commit 4db7cf0

Please sign in to comment.