modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/GroupHandlerBean.java | 200 ++++++++++ modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/GroupHandlerLocal.java | 38 + modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/domain/GroupDefinitionRest.java | 107 +++++ modules/enterprise/server/jar/src/main/resources/rest_templates/groupDefinition.ftl | 62 +++ modules/enterprise/server/jar/src/main/resources/rest_templates/listGroupDefinition.ftl | 37 + modules/enterprise/server/jar/src/main/resources/rest_templates/listMetricSchedule.ftl | 2 modules/enterprise/server/jar/src/main/resources/rest_templates/listResourceWithType.ftl | 2 modules/enterprise/server/jar/src/main/resources/rest_templates/metricSchedule.ftl | 2 8 files changed, 447 insertions(+), 3 deletions(-)
New commits: commit 46f0fdb4bf6ef4bd40b3a9f24ce93a4074dc0dcb Author: Heiko W. Rupp hwr@redhat.com Date: Mon May 28 18:58:24 2012 +0200
Add support for GroupDefinitions to the REST api
diff --git a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/GroupHandlerBean.java b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/GroupHandlerBean.java index 76180d9..405a439 100644 --- a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/GroupHandlerBean.java +++ b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/GroupHandlerBean.java @@ -9,11 +9,13 @@ import javax.ejb.EJB; import javax.ejb.Stateless; import javax.interceptor.Interceptors; import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.GenericEntity; import javax.ws.rs.core.HttpHeaders; @@ -29,12 +31,21 @@ import org.apache.commons.logging.LogFactory; import org.rhq.core.domain.criteria.ResourceGroupCriteria; import org.rhq.core.domain.resource.Resource; import org.rhq.core.domain.resource.ResourceType; +import org.rhq.core.domain.resource.group.GroupDefinition; import org.rhq.core.domain.resource.group.ResourceGroup; +import org.rhq.core.domain.util.PageControl; +import org.rhq.core.domain.util.PageList; import org.rhq.enterprise.server.resource.ResourceManagerLocal; import org.rhq.enterprise.server.resource.ResourceTypeManagerLocal; import org.rhq.enterprise.server.resource.ResourceTypeNotFoundException; import org.rhq.enterprise.server.resource.group.ResourceGroupDeleteException; import org.rhq.enterprise.server.resource.group.ResourceGroupManagerLocal; +import org.rhq.enterprise.server.resource.group.definition.GroupDefinitionManagerLocal; +import org.rhq.enterprise.server.resource.group.definition.exception.GroupDefinitionAlreadyExistsException; +import org.rhq.enterprise.server.resource.group.definition.exception.GroupDefinitionCreateException; +import org.rhq.enterprise.server.resource.group.definition.exception.GroupDefinitionDeleteException; +import org.rhq.enterprise.server.resource.group.definition.exception.GroupDefinitionNotFoundException; +import org.rhq.enterprise.server.rest.domain.GroupDefinitionRest; import org.rhq.enterprise.server.rest.domain.GroupRest; import org.rhq.enterprise.server.rest.domain.Link; import org.rhq.enterprise.server.rest.domain.ResourceWithType; @@ -58,6 +69,9 @@ public class GroupHandlerBean extends AbstractRestBean implements GroupHandlerLo @EJB ResourceTypeManagerLocal resourceTypeManager;
+ @EJB + GroupDefinitionManagerLocal definitionManager; + public Response getGroups(@Context Request request, @Context HttpHeaders headers, @Context UriInfo uriInfo) {
ResourceGroupCriteria criteria = new ResourceGroupCriteria(); @@ -283,4 +297,190 @@ public class GroupHandlerBean extends AbstractRestBean implements GroupHandlerLo
return gr; } + + @Override + @GET + @Path("/definitions") + public Response getDefinitions(@Context Request request, @Context HttpHeaders headers, @Context UriInfo uriInfo) { + + PageList<GroupDefinition> gdlist = definitionManager.getGroupDefinitions(caller,new PageControl()); + List<GroupDefinitionRest> list = new ArrayList<GroupDefinitionRest>(gdlist.getTotalSize()); + for (GroupDefinition def: gdlist) { + GroupDefinitionRest definitionRest = new GroupDefinitionRest(def.getId(),def.getName(),def.getDescription(), + def.getRecalculationInterval()); + definitionRest.setExpression(def.getExpressionAsList()); + + List<Integer> generatedGroups = new ArrayList<Integer>(def.getManagedResourceGroups().size()); + for (ResourceGroup group : def.getManagedResourceGroups() ) { + generatedGroups.add(group.getId()); + } + definitionRest.setGeneratedGroupIds(generatedGroups); + list.add(definitionRest); + } + + MediaType mediaType = headers.getAcceptableMediaTypes().get(0); + Response.ResponseBuilder builder; + + if (mediaType.equals(MediaType.TEXT_HTML_TYPE)) { + builder = Response.ok(renderTemplate("listGroupDefinition", list), mediaType); + } + else { + GenericEntity<List<GroupDefinitionRest>> ret = new GenericEntity<List<GroupDefinitionRest>>(list) { + }; + builder = Response.ok(ret); + } + + return builder.build(); + } + + @Override + @GET + @Path("/definition/{id}") + public Response getDefinition(@PathParam("id") int definitionId, @Context Request request, + @Context HttpHeaders headers, @Context UriInfo uriInfo) { + + try { + GroupDefinition def = definitionManager.getById(definitionId); + GroupDefinitionRest gdr = new GroupDefinitionRest(def.getId(),def.getName(),def.getDescription(), def.getRecalculationInterval()); + gdr.setRecursive(def.isRecursive()); + List<Integer> generatedGroups = new ArrayList<Integer>(def.getManagedResourceGroups().size()); + for (ResourceGroup group : def.getManagedResourceGroups() ) { + generatedGroups.add(group.getId()); + } + gdr.setGeneratedGroupIds(generatedGroups); + gdr.setExpression(def.getExpressionAsList()); + + MediaType mediaType = headers.getAcceptableMediaTypes().get(0); + Response.ResponseBuilder builder; + if (mediaType.equals(MediaType.TEXT_HTML_TYPE)) { + builder = Response.ok(renderTemplate("groupDefinition", gdr), mediaType); + } + else { + builder= Response.ok(gdr); + } + return builder.build(); + } catch (GroupDefinitionNotFoundException e) { + throw new StuffNotFoundException("Group definition with id " + definitionId); + } + } + + @Override + @DELETE + @Path("/definition/{id}") + public Response deleteDefinition(@PathParam("id") int definitionId, @Context Request request, + @Context HttpHeaders headers, @Context UriInfo uriInfo) { + + try { + GroupDefinition def = definitionManager.getById(definitionId); + definitionManager.removeGroupDefinition(caller,definitionId); + return Response.ok().build(); + } catch (GroupDefinitionNotFoundException e) { + // Idem potent + return Response.ok().build(); + } catch (GroupDefinitionDeleteException e) { + throw new StuffNotFoundException("Group definition with id " + definitionId); + } + } + + @Override + @POST + @Path("/definitions") + public Response createDefinition( + GroupDefinitionRest definition, + @Context Request request, @Context HttpHeaders headers, + @Context UriInfo uriInfo) { + + Response.ResponseBuilder builder = null; + + if (definition.getName()==null||definition.getName().isEmpty()) { + builder = Response.status(Response.Status.NOT_ACCEPTABLE); + builder.entity("No name for the definition given"); + } + if (builder!=null) + return builder.build(); + + + GroupDefinition gd = new GroupDefinition(definition.getName()); + gd.setDescription(definition.getDescription()); + List<String> expressionList = definition.getExpression(); + StringBuilder sb = new StringBuilder(); + for(String e : expressionList ) { + sb.append(e); + sb.append("\n"); + } + gd.setExpression(sb.toString()); + gd.setRecalculationInterval(definition.getRecalcInterval()); + gd.setRecursive(definition.isRecursive()); + + try { + GroupDefinition res = definitionManager.createGroupDefinition(caller,gd); + UriBuilder uriBuilder = uriInfo.getBaseUriBuilder(); + uriBuilder.path("/group/definition/{id}"); + URI location = uriBuilder.build(res.getId()); + builder= Response.created(location); + + } catch (GroupDefinitionAlreadyExistsException e) { + builder =Response.status(Response.Status.CONFLICT); + } catch (GroupDefinitionCreateException e) { + e.printStackTrace(); // TODO: Customise this generated block + builder = Response.status(Response.Status.INTERNAL_SERVER_ERROR); + } + return builder.build(); + } + + @Override + @PUT + @Path("/definition/{id}") + public Response updateDefinition(@PathParam("id") int definitionId, + boolean recalculate, GroupDefinitionRest definition, + @Context Request request, @Context HttpHeaders headers, @Context UriInfo uriInfo) { + + GroupDefinition gd; + try { + gd = definitionManager.getById(definitionId); + } catch (GroupDefinitionNotFoundException e) { + throw new StuffNotFoundException("Group Definition with id " + definitionId); + } + + Response.ResponseBuilder builder = null; + + if (!definition.getName().isEmpty()) + gd.setName(definition.getName()); + gd.setDescription(definition.getDescription()); + List<String> expressionList = definition.getExpression(); + StringBuilder sb = new StringBuilder(); + for(String e : expressionList ) { + sb.append(e); + sb.append("\n"); + } + gd.setExpression(sb.toString()); + + gd.setRecalculationInterval(definition.getRecalcInterval()); + gd.setRecursive(definition.isRecursive()); + + try { + definitionManager.updateGroupDefinition(caller,gd); + } catch (Exception e) { + e.printStackTrace(); // TODO: Customise this generated block + builder = Response.status(Response.Status.NOT_ACCEPTABLE); + builder.entity(e.getLocalizedMessage()); + return builder.build(); + } + + String msg=null; + if (recalculate) { + try { + definitionManager.calculateGroupMembership(caller,gd.getId()); + } catch (Exception e) { + msg = e.getLocalizedMessage(); +// e.printStackTrace(); // TODO: Customise this generated block + } + } + builder = Response.ok(gd); + if (msg!=null) { + builder.entity(msg); + } + + return builder.build(); + } } diff --git a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/GroupHandlerLocal.java b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/GroupHandlerLocal.java index d742139..3d3a197 100644 --- a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/GroupHandlerLocal.java +++ b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/GroupHandlerLocal.java @@ -1,13 +1,16 @@ package org.rhq.enterprise.server.rest;
import javax.ejb.Local; +import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; @@ -17,6 +20,7 @@ import javax.ws.rs.core.UriInfo;
import org.jboss.resteasy.annotations.cache.Cache;
+import org.rhq.enterprise.server.rest.domain.GroupDefinitionRest; import org.rhq.enterprise.server.rest.domain.GroupRest;
/** @@ -75,4 +79,38 @@ public interface GroupHandlerLocal { public Response removeResource(@PathParam("id") int id, @PathParam("resourceId") int resourceId, @Context Request request, @Context HttpHeaders headers, @Context UriInfo uriInfo); + + @GET + @Path("/definitions") + public Response getDefinitions(@Context Request request,@Context HttpHeaders headers, + @Context UriInfo uriInfo); + + @GET + @Path("/definition/{id}") + public Response getDefinition(@PathParam("id") int definitionId, + @Context Request request, @Context HttpHeaders headers, + @Context UriInfo uriInfo); + @DELETE + @Path("/definition/{id}") + public Response deleteDefinition(@PathParam("id") int definitionId, + @Context Request request, @Context HttpHeaders headers, + @Context UriInfo uriInfo); + + @POST + @Path("/definitions") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Response createDefinition( + GroupDefinitionRest definition, + @Context Request request, @Context HttpHeaders headers, + @Context UriInfo uriInfo); + + @PUT + @Path("/definition/{id}") + @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}) + public Response updateDefinition(@PathParam("id") int definitionId, + @QueryParam("recalculate") @DefaultValue("false") boolean recalculate, + GroupDefinitionRest definition, + @Context Request request, @Context HttpHeaders headers, + @Context UriInfo uriInfo); + } diff --git a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/domain/GroupDefinitionRest.java b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/domain/GroupDefinitionRest.java new file mode 100644 index 0000000..1fc7b25 --- /dev/null +++ b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/domain/GroupDefinitionRest.java @@ -0,0 +1,107 @@ +/* + * RHQ Management Platform + * Copyright (C) 2005-2012 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +package org.rhq.enterprise.server.rest.domain; + +import java.util.List; + +import javax.xml.bind.annotation.XmlID; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * A group definition ("Dyna Group definition") + * @author Heiko W. Rupp + */ +@XmlRootElement(name="groupDefinition") +public class GroupDefinitionRest { + + private int id; + private String name; + private String description; + private List<String> expression; + private long recalcInterval; + private boolean recursive=false; + List<Integer> generatedGroupIds; + + public GroupDefinitionRest() { + } + + public GroupDefinitionRest(int id, String name, String description, long recalcInterval) { + this.id = id; + this.name = name; + this.description = description; + this.recalcInterval = recalcInterval; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public List<String> getExpression() { + return expression; + } + + public void setExpression(List<String> expression) { + this.expression = expression; + } + + public long getRecalcInterval() { + return recalcInterval; + } + + public void setRecalcInterval(long recalcInterval) { + this.recalcInterval = recalcInterval; + } + + public List<Integer> getGeneratedGroupIds() { + return generatedGroupIds; + } + + public void setGeneratedGroupIds(List<Integer> generatedGroupIds) { + this.generatedGroupIds = generatedGroupIds; + } + + public boolean isRecursive() { + return recursive; + } + + public void setRecursive(boolean recursive) { + this.recursive = recursive; + } +} diff --git a/modules/enterprise/server/jar/src/main/resources/rest_templates/groupDefinition.ftl b/modules/enterprise/server/jar/src/main/resources/rest_templates/groupDefinition.ftl new file mode 100644 index 0000000..59aa4da --- /dev/null +++ b/modules/enterprise/server/jar/src/main/resources/rest_templates/groupDefinition.ftl @@ -0,0 +1,62 @@ +<#ftl > +<#-- +/* + * RHQ Management Platform + * Copyright (C) 2005-2011 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +--> +<#-- @ftlvariable name="var" type="org.rhq.enterprise.server.rest.domain.GroupDefinitionRest" --> +<html> + <table> + <thead> + <tr> + <td>Name</td><td>Value</td> + </tr> + </thead> + <tr> + <td>Name</td><td>${var.name}</td> + </tr> + <tr> + <td>Id</td><td>${var.id}</td> + </tr> + <tr> + <td>Description</td><td>${var.description}</td> + </tr> + <tr> + <td>Recalculation interval (ms)</td><td>${var.recalcInterval}</td> + </tr> + <tr> + <td>Recursive</td><td>${var.recursive?string("Yes","No")}</td> + </tr> + <tr> + <td>Expression</td> + <td> + <#list var.expression as line> + ${line}<br/> + </#list> + </td> + </tr> + <tr> + <td>Groups</td> + <td> + <#list var.generatedGroupIds as groupId> + <a href="/rest/1/group/${groupId?c}.html">${groupId?c}</a> + </#list> + </td> + </tr> + </table> +</html> diff --git a/modules/enterprise/server/jar/src/main/resources/rest_templates/listGroupDefinition.ftl b/modules/enterprise/server/jar/src/main/resources/rest_templates/listGroupDefinition.ftl new file mode 100644 index 0000000..15e8272 --- /dev/null +++ b/modules/enterprise/server/jar/src/main/resources/rest_templates/listGroupDefinition.ftl @@ -0,0 +1,37 @@ +<#ftl > +<#-- +/* + * RHQ Management Platform + * Copyright (C) 2005-2012 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +--> +<#-- @ftlvariable name="var" type="java.util.List<org.rhq.enterprise.server.rest.domain.GroupDefinitionRest>" --> +<html> +<ul> + <#-- the next looks odd, but the incoming var is a list --> + <#if (var?size>0) > + <#list var as var> + <li> + <#include "groupDefinition.ftl"/> + </li> + </#list> + <#else> + <strong>No Definitions have been set up</strong> + </#if> + +</ul> +<html> \ No newline at end of file diff --git a/modules/enterprise/server/jar/src/main/resources/rest_templates/listMetricSchedule.ftl b/modules/enterprise/server/jar/src/main/resources/rest_templates/listMetricSchedule.ftl index 7ef3fe9..b351644 100644 --- a/modules/enterprise/server/jar/src/main/resources/rest_templates/listMetricSchedule.ftl +++ b/modules/enterprise/server/jar/src/main/resources/rest_templates/listMetricSchedule.ftl @@ -2,7 +2,7 @@ <#-- /* * RHQ Management Platform - * Copyright (C) 2005-2008 Red Hat, Inc. + * Copyright (C) 2005-2012 Red Hat, Inc. * All rights reserved. * * This program is free software; you can redistribute it and/or modify diff --git a/modules/enterprise/server/jar/src/main/resources/rest_templates/listResourceWithType.ftl b/modules/enterprise/server/jar/src/main/resources/rest_templates/listResourceWithType.ftl index 132d7cb..820a722 100644 --- a/modules/enterprise/server/jar/src/main/resources/rest_templates/listResourceWithType.ftl +++ b/modules/enterprise/server/jar/src/main/resources/rest_templates/listResourceWithType.ftl @@ -2,7 +2,7 @@ <#-- /* * RHQ Management Platform - * Copyright (C) 2005-2008 Red Hat, Inc. + * Copyright (C) 2005-2012 Red Hat, Inc. * All rights reserved. * * This program is free software; you can redistribute it and/or modify diff --git a/modules/enterprise/server/jar/src/main/resources/rest_templates/metricSchedule.ftl b/modules/enterprise/server/jar/src/main/resources/rest_templates/metricSchedule.ftl index 0a0f187..e05012e 100644 --- a/modules/enterprise/server/jar/src/main/resources/rest_templates/metricSchedule.ftl +++ b/modules/enterprise/server/jar/src/main/resources/rest_templates/metricSchedule.ftl @@ -2,7 +2,7 @@ <#-- /* * RHQ Management Platform - * Copyright (C) 2005-2008 Red Hat, Inc. + * Copyright (C) 2005-2012 Red Hat, Inc. * All rights reserved. * * This program is free software; you can redistribute it and/or modify