From: <rm...@hy...> - 2008-11-15 19:57:35
|
Author: rmorgan Date: 2008-11-15 11:57:27 -0800 (Sat, 15 Nov 2008) New Revision: 280 URL: http://svn.hyperic.org/?view=rev&root=HQApi&revision=280 Modified: trunk/hqu/hqapi1/app/ResourceController.groovy trunk/src/org/hyperic/hq/hqapi1/ResourceApi.java trunk/src/org/hyperic/hq/hqapi1/test/ResourceCreateService_test.java Log: Complete service creation. Modified: trunk/hqu/hqapi1/app/ResourceController.groovy =================================================================== --- trunk/hqu/hqapi1/app/ResourceController.groovy 2008-11-15 02:46:54 UTC (rev 279) +++ trunk/hqu/hqapi1/app/ResourceController.groovy 2008-11-15 19:57:27 UTC (rev 280) @@ -25,6 +25,27 @@ } } + /** + * Get the resource based on the given id. If the resource is not found, + * null is returned. + */ + private getResource(id) { + def resource = resourceHelper.findById(id) + + if (!resource) { + return null + } else { + //XXX: ResourceHelper needs some work here.. + try { + resource.name // Check the object really exists + resource.entityId // Check the object is an appdef object + return resource + } catch (Throwable t) { + return null + } + } + } + def listResourcePrototypes(params) { def prototypes = resourceHelper.findAllAppdefPrototypes() @@ -83,23 +104,51 @@ !xmlService || xmlService.size() != 1 || !xmlServicePrototype || xmlServicePrototype.size() != 1) { renderXml() { - CreateRoleResponse() { + CreateResourceResponse() { out << getFailureXML(ErrorCode.INVALID_PARAMETERS) } } return } - def parent = xmlParent[0] - def service = xmlService[0] - def servicePrototype = xmlServicePrototype[0] + def parent = getResource(xmlParent[0].'@id'.toInteger()) + def prototype = resourceHelper.find(prototype: xmlServicePrototype[0].'@name') - log.info("Found parent=" + parent.'@name' + " service=" + - service.'@name' + " type=" + servicePrototype.'@name') + if (!parent || !prototype) { + renderXml() { + CreateResourceResponse() { + out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND) + } + } + } + def serviceXml = xmlService[0] + def cfgXml = serviceXml['ResourceConfig'] + def cfg = [:] + cfgXml.each { c -> + cfg.put(c.'@key', c.'@value') + } + + def service + try { + service = prototype.createInstance(parent, serviceXml.'@name', + user, cfg) + } catch (Exception e) { + // XXX: duplicate service not handled properly, but assume that's + // the case. + renderXml() { + CreateResourceResponse() { + out << getFailureXML(ErrorCode.OBJECT_EXISTS); + } + } + log.debug("Error creating service", e) + return + } + renderXml() { - out << CreateResourceResponse() { - out << getFailureXML(ErrorCode.NOT_IMPLEMENTED) + CreateResourceResponse() { + out << getSuccessXML() + out << getResourceXML(service) } } } @@ -175,19 +224,11 @@ } else if (prototype) { resources = resourceHelper.find('byPrototype': prototype) } else if (childrenOfId) { - def resource = resourceHelper.findById(childrenOfId) - + def resource = getResource(childrenOfId) if (!resource) { failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND) } else { - //XXX: ResourceHelper needs some work here.. - try { - resource.name // Check the object really exists - resource.entityId // Check the object is an appdef object - resources = resource.getViewableChildren(user) - } catch (Throwable t) { - failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND) - } + resources = resource.getViewableChildren(user) } } else { // Shouldn't happen Modified: trunk/src/org/hyperic/hq/hqapi1/ResourceApi.java =================================================================== --- trunk/src/org/hyperic/hq/hqapi1/ResourceApi.java 2008-11-15 02:46:54 UTC (rev 279) +++ trunk/src/org/hyperic/hq/hqapi1/ResourceApi.java 2008-11-15 19:57:27 UTC (rev 280) @@ -154,7 +154,6 @@ * @param type The {@link ResourcePrototype} for the resource to be created. * @param parent The parent {@link Resource} for the created resource. * @param name The name of the resource to create. - * @param description The description of the resource to create. * @param config The configuration for the service. * * @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}, @@ -166,13 +165,11 @@ public CreateResourceResponse createService(ResourcePrototype type, Resource parent, String name, - String description, Map<String,String> config) throws IOException { Resource service = new Resource(); service.setName(name); - service.setDescription(description); for (String k : config.keySet()) { ResourceConfig c = new ResourceConfig(); c.setKey(k); Modified: trunk/src/org/hyperic/hq/hqapi1/test/ResourceCreateService_test.java =================================================================== --- trunk/src/org/hyperic/hq/hqapi1/test/ResourceCreateService_test.java 2008-11-15 02:46:54 UTC (rev 279) +++ trunk/src/org/hyperic/hq/hqapi1/test/ResourceCreateService_test.java 2008-11-15 19:57:27 UTC (rev 280) @@ -10,6 +10,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Random; public class ResourceCreateService_test extends ResourceTestBase { @@ -27,34 +28,19 @@ ResourceApi api = getApi().getResourceApi(); + // Find HTTP resource type GetResourcePrototypeResponse protoResponse = api.getResourcePrototype("HTTP"); hqAssertSuccess(protoResponse); ResourcePrototype pt = protoResponse.getResourcePrototype(); + // Find local platform FindResourcesResponse resourcesResponse = api.findResources(a); hqAssertSuccess(resourcesResponse); assertTrue("Did not find a single platform for " + a.getAddress() + ":" + a.getPort(), resourcesResponse.getResource().size() == 1); Resource platform = resourcesResponse.getResource().get(0); - FindResourcesResponse childResourcesResponse = api.findResourceChildren(platform); - hqAssertSuccess(childResourcesResponse); - - String PARENT_TYPE = "Net Services"; - Resource parent = null; - for (Resource r : childResourcesResponse.getResource()) { - if (r.getResourcePrototype().getName().equals("Net Services")) { - parent = r; - break; - } - } - - if (parent == null) { - getLog().error("Unable to find server of type " + PARENT_TYPE + - " skipping test."); - return; - } - + // Configure service Map<String,String> params = new HashMap<String,String>(); params.put("hostname", "www.hyperic.com"); params.put("port", "80"); @@ -62,10 +48,13 @@ params.put("path", "/"); params.put("method", "GET"); - CreateResourceResponse resp = - api.createService(pt, parent, "My HTTP Check", - "A test service created by the test suite", - params); - hqAssertFailureNotImplemented(resp); + Random r = new Random(); + String name = "My HTTP Check " + r.nextInt(); + + CreateResourceResponse resp = api.createService(pt, platform, name, + params); + hqAssertSuccess(resp); + Resource createdResource = resp.getResource(); + assertEquals(createdResource.getName(), name); } } |