modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/AlertDefinitionHandlerBean.java
| 2
modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/MetricHandlerBean.java
| 68 +++++++
modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/MetricsTest.java
| 90 +++++++++-
modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Schedule.java
| 56 ++++++
4 files changed, 211 insertions(+), 5 deletions(-)
New commits:
commit aaf75a50cbeb19bf7ea6716427a6121c338f448f
Author: Heiko W. Rupp <hwr(a)redhat.com>
Date: Sun Dec 8 22:31:26 2013 +0100
BZ 961000 - Allow to get and update metric definitions.
diff --git
a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/MetricHandlerBean.java
b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/MetricHandlerBean.java
index b33db05..831c24e 100644
---
a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/MetricHandlerBean.java
+++
b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/MetricHandlerBean.java
@@ -31,6 +31,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
+import javax.ejb.Asynchronous;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
@@ -123,6 +124,8 @@ public class MetricHandlerBean extends AbstractRestBean {
ResourceManagerLocal resMgr;
@EJB
ResourceGroupManagerLocal groupMgr;
+ @EJB
+ MetricHandlerBean metricHandlerBean;
@EJB
private StorageClientManagerBean sessionManager;
@@ -608,7 +611,7 @@ public class MetricHandlerBean extends AbstractRestBean {
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@ApiOperation(value = "Update the schedule (enabled, interval) ",
responseClass = "MetricSchedule")
@ApiError(code = 404, reason = NO_SCHEDULE_FOR_ID)
- public Response updateSchedule(@ApiParam("Id of the schedule to query")
@PathParam("id") int scheduleId,
+ public Response updateSchedule(@ApiParam("Id of the schedule to update")
@PathParam("id") int scheduleId,
@ApiParam(value = "New schedule data", required
= true) MetricSchedule in,
@Context HttpHeaders headers) {
@@ -632,6 +635,69 @@ public class MetricHandlerBean extends AbstractRestBean {
return Response.ok(ret,headers.getAcceptableMediaTypes().get(0)).build();
}
+
+ @GET
+ @Path("/definition/{id}")
+ @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
+ @ApiOperation(value = "Get the definition ", responseClass =
"MetricSchedule")
+ @ApiError(code = 404, reason = "No definition exists for the given id.")
+ public Response getDefinition(@ApiParam("Id of the definition to obtain")
@PathParam("id") int definitionId,
+ @Context HttpHeaders headers) {
+
+ MeasurementDefinition measurementDefinition =
definitionManager.getMeasurementDefinition(caller, definitionId);
+ if (measurementDefinition==null)
+ throw new StuffNotFoundException("Definition with id " +
definitionId);
+
+
+ MetricSchedule schedule = new MetricSchedule(definitionId,
+ measurementDefinition.getName(),
+ measurementDefinition.getDisplayName(),
+ measurementDefinition.isDefaultOn(),
+ measurementDefinition.getDefaultInterval(),
+ measurementDefinition.getUnits().getName(),
+ measurementDefinition.getDataType().name());
+
+ return Response.ok(schedule,headers.getAcceptableMediaTypes().get(0)).build();
+ }
+
+ @PUT
+ @Path("/definition/{id}")
+ @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
+ @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
+ @ApiOperation(value = "Update the definition (default enabled, default
interval)",
+ notes = "This operation may internally take a long time to complete and is
thus only triggered by this call. "
+ + "A return code of 200 only indicates that the operation was
successfully submitted."
+ , responseClass = "MetricSchedule")
+ @ApiError(code = 404, reason = "No definition exists for the given id.")
+ public Response updateDefinition(@ApiParam("Id of the definition to
update") @PathParam("id") int definitionId,
+ @ApiParam(value = "New definition data",
required = true) MetricSchedule in,
+ @ApiParam(value = "Update existing schedules for
this definition as well?") @QueryParam("updateExisting")
@DefaultValue("false") boolean updateExisting,
+ @Context HttpHeaders headers) {
+
+ MeasurementDefinition measurementDefinition =
definitionManager.getMeasurementDefinition(caller, definitionId);
+ if (measurementDefinition==null)
+ throw new StuffNotFoundException("Definition with id " +
definitionId);
+
+ // Call an async method to do the work as this can take a looong time
+ metricHandlerBean.submitDefinitionChange(definitionId, in, updateExisting);
+
+ StringValue ret = new StringValue("Request submitted - this may take a while
to complete.");
+
+ return Response.ok(ret,headers.getAcceptableMediaTypes().get(0)).build();
+ }
+
+ /**
+ * This method does the real updating, it is not exposed to the REST-clients, but
must be public so that #updateDefinition can
+ * call it and the container does an asynchronous request.
+ * @param definitionId Id of the measeuremnt definition to update
+ * @param in The data to be put in
+ * @param updateExisting Should existing schedules of the metric als be updated?
+ */
+ @Asynchronous
+ public void submitDefinitionChange(int definitionId, MetricSchedule in, boolean
updateExisting) {
+
scheduleManager.updateDefaultCollectionIntervalAndEnablementForMeasurementDefinitions(caller,new
int[]{definitionId},in.getCollectionInterval(),in.getEnabled(),updateExisting);
+ }
+
@GZIP
@ApiOperation(value = "Expose the raw metrics of a single schedule. This can
only expose raw data, which means the start date may "
+ "not be older than 7 days.")
diff --git
a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/MetricsTest.java
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/MetricsTest.java
index 6e945a6..ab2b3a2 100644
---
a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/MetricsTest.java
+++
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/MetricsTest.java
@@ -1025,11 +1025,11 @@ JsonPath jp =
given()
.header(acceptJson)
.queryParam("sid", numericScheduleId)
- .queryParam("dataPoints",99)
+ .queryParam("dataPoints", 99)
.expect()
.statusCode(200)
.log().ifError()
- .body("[0].dataPoints",iterableWithSize(99))
+ .body("[0].dataPoints", iterableWithSize(99))
// .body("[0].min",closeTo(1.5,0.1))
// .body("[0].max",closeTo(46.0,0.1)) // We may have data already
// .body("[0].avg",notNullValue())
@@ -1068,7 +1068,7 @@ JsonPath jp =
.expect()
.statusCode(200)
.log().ifError()
- .body("",iterableWithSize(2))
+ .body("", iterableWithSize(2))
// .body("[0].min", closeTo(1.5,0.1))
// .body("[0].max", notNullValue()) // We may have data already
// .body("[0].avg", notNullValue())
@@ -1077,7 +1077,91 @@ JsonPath jp =
.get("/metric/data");
}
+ @Test
+ public void testGetUpdateDefinition() throws Exception {
+
+ Schedule schedule = new Schedule();
+ schedule.setCollectionInterval(1234567);
+ schedule.setEnabled(true);
+
+ // First read the current data
+
+ Schedule def =
+ given()
+ .header(acceptJson)
+ .pathParam("id", numericScheduleDefinitionId)
+ .expect()
+ .statusCode(200)
+ .when()
+ .get("/metric/definition/{id}")
+ .as(Schedule.class);
+
+ long oldInterval = def.getCollectionInterval();
+
+
+ given()
+ .header(acceptJson)
+ .contentType(ContentType.JSON)
+ .pathParam("id", numericScheduleDefinitionId)
+ .body(schedule)
+ .expect()
+ .statusCode(200)
+ .log().ifError()
+ .when()
+ .put("/metric/definition/{id}");
+
+ // reset to original interval
+
+ schedule.setCollectionInterval(oldInterval);
+ given()
+ .header(acceptJson)
+ .contentType(ContentType.JSON)
+ .pathParam("id", numericScheduleDefinitionId)
+ .body(schedule)
+ .expect()
+ .statusCode(200)
+ .log().ifError()
+ .when()
+ .put("/metric/definition/{id}");
+
+
+
+ }
+ @Test
+ public void testUpdateBadDefinition() throws Exception {
+
+ Schedule schedule = new Schedule();
+ schedule.setCollectionInterval(1234567);
+ schedule.setEnabled(true);
+
+
+ given()
+ .header(acceptJson)
+ .contentType(ContentType.JSON)
+ .pathParam("id", 42)
+ .body(schedule)
+ .expect()
+ .statusCode(404)
+ .log().ifError()
+ .when()
+ .put("/metric/definition/{id}");
+
+ }
+
+ @Test
+ public void testGetBadDefinition() throws Exception {
+
+ given()
+ .header(acceptJson)
+ .pathParam("id",42)
+ .expect()
+ .statusCode(404)
+ .log().ifError()
+ .when()
+ .get("/metric/definition/{id}");
+
+ }
private void addDataToSchedule(int howMany) {
long now = System.currentTimeMillis();
diff --git
a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Schedule.java
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Schedule.java
index c0deeef..31df671 100644
---
a/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Schedule.java
+++
b/modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/Schedule.java
@@ -22,6 +22,8 @@
*/
package org.rhq.modules.integrationTests.restApi.d;
+import java.util.List;
+
/**
* A metric schedule for testing
* @author Heiko W. Rupp
@@ -32,6 +34,12 @@ public class Schedule {
private boolean enabled;
private long collectionInterval;
private int definitionId;
+ private String scheduleName;
+ private String displayName;
+ private String unit;
+ private String type;
+ List<Link> links;
+ private long mtime;
public Schedule() {
}
@@ -67,4 +75,52 @@ public class Schedule {
public void setDefinitionId(int definitionId) {
this.definitionId = definitionId;
}
+
+ public String getScheduleName() {
+ return scheduleName;
+ }
+
+ public void setScheduleName(String scheduleName) {
+ this.scheduleName = scheduleName;
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
+
+ public void setDisplayName(String displayName) {
+ this.displayName = displayName;
+ }
+
+ public String getUnit() {
+ return unit;
+ }
+
+ public void setUnit(String unit) {
+ this.unit = unit;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ public List<Link> getLinks() {
+ return links;
+ }
+
+ public void setLinks(List<Link> links) {
+ this.links = links;
+ }
+
+ public long getMtime() {
+ return mtime;
+ }
+
+ public void setMtime(long mtime) {
+ this.mtime = mtime;
+ }
}
commit 2cd49aa472ae861cb855c4d2beec6c3648c71736
Author: Heiko W. Rupp <hwr(a)redhat.com>
Date: Sat Dec 7 23:37:49 2013 +0100
Improve description
diff --git
a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/AlertDefinitionHandlerBean.java
b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/AlertDefinitionHandlerBean.java
index d42dade..b6c0695 100644
---
a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/AlertDefinitionHandlerBean.java
+++
b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/AlertDefinitionHandlerBean.java
@@ -155,7 +155,7 @@ public class AlertDefinitionHandlerBean extends AbstractRestBean {
@ApiParam("Should conditions and notifications be returned too?")
@QueryParam("full") @DefaultValue("false") boolean full,
@ApiParam(value = "Page number") @QueryParam("page")
Integer page,
@ApiParam(value = "Page size") @DefaultValue("20")
@QueryParam("ps") int pageSize,
- @ApiParam(value = "Resource id")
@QueryParam("resourceId") Integer resourceId,
+ @ApiParam(value = "Resource id to filter by")
@QueryParam("resourceId") Integer resourceId,
@Context HttpHeaders headers,
@Context UriInfo uriInfo) {