modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/server/gwt/PluginFileUploadServlet.java | 15 - modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/ResourceHandlerBean.java | 81 +++++-- modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/ResourcesTest.java | 110 +++++++++- modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Resource.java | 76 ++++++ 4 files changed, 248 insertions(+), 34 deletions(-)
New commits: commit 977ff337c997c2f5d63d19d46ada868eed4d9624 Author: Heiko W. Rupp hwr@redhat.com Date: Tue Jan 29 21:30:06 2013 +0100
Use a real UUID for resource creation. Deprecate the endpoints for platform and resource creation.
diff --git a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/ResourceHandlerBean.java b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/ResourceHandlerBean.java index be28d92..8142f17 100644 --- a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/ResourceHandlerBean.java +++ b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/ResourceHandlerBean.java @@ -27,6 +27,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Set; +import java.util.UUID;
import javax.ejb.EJB; import javax.ejb.Stateless; @@ -529,15 +530,39 @@ public class ResourceHandlerBean extends AbstractRestBean {
@ApiOperation(value = "Create a new platform in the Server. If the platform already exists, this is a no-op." + "The platform internally has a special name so that it will not clash with one that was generated" + - "via a normal RHQ agent") + "via a normal RHQ agent. DEPRECATED Use POST /platforms instead") @POST @Path("platform/{name}") - public Response createPlatform( + public Response createPlatformOLD( @ApiParam(value = "Name of the platform") @PathParam("name") String name, @ApiParam(value = "Type of the platform", allowableValues = "Linux,Windows,... TODO") StringValue typeValue, @Context UriInfo uriInfo) { + String typeName = typeValue.getValue();
+ return createPlatformInternal(name, typeName, uriInfo); + + } + + @POST + @Path("platforms") + @ApiOperation(value = "Create a new platform in the Server. If the platform already exists, this is a no-op." + + "The platform internally has a special name so that it will not clash with one that was generated" + + "via a normal RHQ agent. Only resourceName and typeName need to be supplied in the passed object") + public Response createPlatform( + @ApiParam("The info about the platform. Only type name and resource name need to be supplied") ResourceWithType resource, + @Context UriInfo uriInfo) + { + + String typeName = resource.getTypeName(); + String resourceName = resource.getResourceName(); + + return createPlatformInternal(resourceName,typeName,uriInfo); + } + + + private Response createPlatformInternal(String name, String typeName, UriInfo uriInfo) { + ResourceType type = resourceTypeManager.getResourceTypeByNameAndPlugin(typeName,"Platforms"); if (type==null) { throw new StuffNotFoundException("Platform with type [" + typeName + "]"); @@ -565,10 +590,8 @@ public class ResourceHandlerBean extends AbstractRestBean { agent = new Agent("dummy-agent:name"+name,"-dummy-p:"+name,12345,"http://foo.com/p:name/%22+name,%22abc-%22+name); agentMgr.createAgent(agent);
- - Resource platform = new Resource(resourceKey,name,type); - platform.setUuid(resourceKey); + platform.setUuid(UUID.randomUUID().toString()); platform.setAgent(agent); platform.setInventoryStatus(InventoryStatus.COMMITTED); platform.setModifiedBy(caller.getName()); @@ -595,40 +618,44 @@ public class ResourceHandlerBean extends AbstractRestBean { } catch (Exception e) { throw new RuntimeException(e); } - }
- private void createSchedules(Resource resource) { - ResourceType rt = resource.getResourceType(); - Set<MeasurementDefinition> definitions = rt.getMetricDefinitions (); - for (MeasurementDefinition definition : definitions) { - MeasurementSchedule schedule = new MeasurementSchedule(definition,resource); - schedule.setEnabled(definition.isDefaultOn()); - schedule.setInterval(definition.getDefaultInterval()); - entityManager.persist(schedule); - } - }
- @ApiOperation(value = "Create a resource with a given type below a certain parent") + @ApiOperation(value = "Create a resource with a given type below a certain parent. DEPRECATED Use POST / instead") @POST @Path("{name}") - public Response createResource( + public Response createResourceOLD( @ApiParam("Name of the new resource") @PathParam("name") String name, @ApiParam("Name of the Resource type") StringValue typeValue, @ApiParam("Name of the plugin providing the type") @QueryParam("plugin") String plugin, @ApiParam("Id of the future parent to attach this to") @QueryParam("parentId") int parentId, @Context UriInfo uriInfo) {
+ String typeName = typeValue.getValue(); + + return createResourceInternal(name, plugin, parentId, typeName, uriInfo); + } + + @POST + @Path("/") + @ApiOperation("Create a new resource as a child of an existing resource¡") + public Response createResource( + @ApiParam("THe info about the resource. You need to supply resource name, resource type name, plugin name, id of the parent") ResourceWithType resource, + @Context UriInfo uriInfo) + { + return createResourceInternal(resource.getResourceName(),resource.getPluginName(),resource.getParentId(),resource.getTypeName(),uriInfo); + } + + private Response createResourceInternal(String name, String plugin, int parentId, String typeName, + UriInfo uriInfo) { Resource parent = resMgr.getResourceById(caller,parentId);
- String typeName = typeValue.getValue(); ResourceType resType = resourceTypeManager.getResourceTypeByNameAndPlugin(typeName,plugin); if (resType==null) throw new StuffNotFoundException("ResourceType with name [" + typeName + "] and plugin [" + plugin + "]");
String resourceKey = "res:" + name + ":" + parentId;
- Resource r = resMgr.getResourceByParentAndKey(caller,parent,resourceKey,plugin,typeName); if (r!=null) { // resource exists - return it @@ -644,7 +671,7 @@ public class ResourceHandlerBean extends AbstractRestBean { }
Resource res = new Resource(resourceKey,name,resType); - res.setUuid(resourceKey); + res.setUuid(UUID.randomUUID().toString()); res.setAgent(parent.getAgent()); res.setParentResource(parent); res.setInventoryStatus(InventoryStatus.COMMITTED); @@ -683,4 +710,16 @@ public class ResourceHandlerBean extends AbstractRestBean { return Response.status(Response.Status.NO_CONTENT).build();
} + + private void createSchedules(Resource resource) { + ResourceType rt = resource.getResourceType(); + Set<MeasurementDefinition> definitions = rt.getMetricDefinitions (); + for (MeasurementDefinition definition : definitions) { + MeasurementSchedule schedule = new MeasurementSchedule(definition,resource); + schedule.setEnabled(definition.isDefaultOn()); + schedule.setInterval(definition.getDefaultInterval()); + entityManager.persist(schedule); + } + } + } diff --git a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/ResourcesTest.java b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/ResourcesTest.java index e8066bc..6899995 100644 --- a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/ResourcesTest.java +++ b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/ResourcesTest.java @@ -25,12 +25,16 @@ package org.rhq.modules.integrationTests.restApi;
import com.jayway.restassured.http.ContentType; import com.jayway.restassured.path.json.JsonPath; +import com.jayway.restassured.path.xml.XmlPath; +import com.jayway.restassured.path.xml.element.Node; import com.jayway.restassured.response.Response;
import org.apache.http.HttpStatus; import org.hamcrest.CoreMatchers; import org.junit.Test;
+import org.rhq.modules.integrationTests.restApi.d.Resource; + import static com.jayway.restassured.RestAssured.expect; import static com.jayway.restassured.RestAssured.get; import static com.jayway.restassured.RestAssured.given; @@ -129,7 +133,7 @@ public class ResourcesTest extends AbstractBase { @Test public void testGetPlatformXml() { given() - .header("Accept","application/xml") + .header("Accept", "application/xml") .expect() .statusCode(200) .contentType(ContentType.XML) @@ -140,7 +144,7 @@ public class ResourcesTest extends AbstractBase { @Test public void testGetPlatformSchedules() { given() - .header("Accept","application/json") + .header("Accept", "application/json") .expect() .statusCode(200) .when() @@ -158,18 +162,66 @@ public class ResourcesTest extends AbstractBase { }
@Test - public void testCreatePlatform() throws Exception { + public void testCreatePlatformOld() throws Exception {
given().body("{"value":"Linux"}") - .header("Content-Type","application/json") - .header("Accept","application/json") + .header("Content-Type", "application/json") + .header("Accept", "application/json") .expect().statusCode(201) .when().post("/resource/platform/api-test-dummy"); }
@Test + public void testCreatePlatform() throws Exception { + + Resource resource = new Resource(); + resource.setResourceName("dummy-test"); + resource.setTypeName("Linux"); + + given() + .header(acceptXml) + .contentType(ContentType.JSON) + .body(resource) + .expect() + .statusCode(201) + .log().ifError() + .when() + .post("/resource/platforms"); + + } + + @Test public void testCreatePlatformAndRemove() throws Exception {
+ Resource resource = new Resource(); + resource.setResourceName("dummy-test"); + resource.setTypeName("Linux"); + + Response response = + given() + .header(acceptXml) + .contentType(ContentType.JSON) + .body(resource) + .expect() + .statusCode(201) + .log().ifError() + .when() + .post("/resource/platforms"); + + XmlPath xmlPath = response.xmlPath(); + Node resource1 = xmlPath.get("resource"); + Node platformIdNode = resource1.get("resourceId"); + String platformId = platformIdNode.value(); + + given().pathParam("id", platformId) + .expect().statusCode(HttpStatus.SC_NO_CONTENT) + .when().delete("/resource/{id}"); + + } + + @Test + public void testCreatePlatformOLDAndRemove() throws Exception { + Response response = with().body("{"value":"Linux"}") .header("Content-Type","application/json") @@ -185,12 +237,12 @@ public class ResourcesTest extends AbstractBase { }
@Test - public void testCreatePlatformWithChildAndRemove() throws Exception { + public void testCreatePlatformOLDWithChildOLDAndRemove() throws Exception {
Response response = with().body("{"value":"Linux"}") .header("Content-Type","application/json") - .header("Accept","application/json") + .header("Accept", "application/json") .expect() .statusCode(201) .when() @@ -219,6 +271,50 @@ public class ResourcesTest extends AbstractBase { }
@Test + public void testCreatePlatformWithChildAndRemove() throws Exception { + + Resource platform = new Resource(); + platform.setResourceName("dummy-test"); + platform.setTypeName("Linux"); + + Response response = + with().body(platform) + .header("Content-Type","application/json") + .header("Accept","application/json") + .expect() + .statusCode(201) + .log().ifError() + .when() + .post("/resource/platforms"); + + String platformId = response.jsonPath().getString("resourceId"); + + Resource child = new Resource(); + child.setResourceName("test"); + child.setTypeName("CPU"); + child.setPluginName("Platforms"); + child.setParentId(Integer.valueOf(platformId)); + + try { + + with() + .body(child) + .header("Content-Type", "application/json") + .header("Accept", "application/json") + .expect() + .statusCode(201) + .log().ifError() + .when() + .post("/resource"); + } + finally { + given().pathParam("id",platformId) + .expect().statusCode(HttpStatus.SC_NO_CONTENT) + .when().delete("/resource/{id}"); + } + } + + @Test public void testDoubleChildCreate() throws Exception { // a resource can be created again and again
diff --git a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Resource.java b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Resource.java new file mode 100644 index 0000000..9c3c1a3 --- /dev/null +++ b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Resource.java @@ -0,0 +1,76 @@ +/* + * RHQ Management Platform + * Copyright (C) 2005-2013 Red Hat, Inc. + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +package org.rhq.modules.integrationTests.restApi.d; + +/** + * A resource for testing purposes + * @author Heiko W. Rupp + */ +public class Resource { + + String resourceName; + int resourceId; + String typeName; + String pluginName; + Integer parentId; + + public Resource() { + } + + public String getResourceName() { + return resourceName; + } + + public void setResourceName(String resourceName) { + this.resourceName = resourceName; + } + + public Integer getParentId() { + return parentId; + } + + public void setParentId(Integer parentId) { + this.parentId = parentId; + } + + public String getPluginName() { + return pluginName; + } + + public void setPluginName(String pluginName) { + this.pluginName = pluginName; + } + + public int getResourceId() { + return resourceId; + } + + public void setResourceId(int resourceId) { + this.resourceId = resourceId; + } + + public String getTypeName() { + return typeName; + } + + public void setTypeName(String typeName) { + this.typeName = typeName; + } +}
commit 75903fd6abdef24ac5bab17f35f3b83ba2b1033c Author: Heiko W. Rupp hwr@redhat.com Date: Tue Jan 29 20:06:17 2013 +0100
Do not assume everything is a jar - we also support jar-less plugins.
diff --git a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/server/gwt/PluginFileUploadServlet.java b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/server/gwt/PluginFileUploadServlet.java index adc870a..3263d19 100644 --- a/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/server/gwt/PluginFileUploadServlet.java +++ b/modules/enterprise/gui/coregui/src/main/java/org/rhq/enterprise/gui/coregui/server/gwt/PluginFileUploadServlet.java @@ -66,13 +66,16 @@ public class PluginFileUploadServlet extends FileUploadServlet { File file = files.values().iterator().next(); String newPluginFilename = fileNames.values().iterator().next();
- // make sure its a .jar file and strip off any temp file suffix (in case the browser set the name to something odd) - int jarExtension = newPluginFilename.lastIndexOf(".jar"); - if (jarExtension < 0) { - newPluginFilename = newPluginFilename + ".jar"; // make sure it ends with ".jar" in case it is some tmp filename - jarExtension = newPluginFilename.lastIndexOf(".jar"); + // see if it is a jar-less plugin descriptor and if not check for .jar + if (!newPluginFilename.endsWith("-rhq-plugin.xml")) { + // make sure its a .jar file and strip off any temp file suffix (in case the browser set the name to something odd) + int jarExtension = newPluginFilename.lastIndexOf(".jar"); + if (jarExtension < 0) { + newPluginFilename = newPluginFilename + ".jar"; // make sure it ends with ".jar" in case it is some tmp filename + jarExtension = newPluginFilename.lastIndexOf(".jar"); + } + newPluginFilename = newPluginFilename.substring(0, jarExtension + ".jar".length()); } - newPluginFilename = newPluginFilename.substring(0, jarExtension + ".jar".length()); log.info("A new plugin [" + newPluginFilename + "] has been uploaded to [" + file + "]");
if (file == null || !file.exists()) {