Search in sources :

Example 1 with CreateConditionRequest

use of org.graylog2.rest.models.streams.alerts.requests.CreateConditionRequest in project graylog2-server by Graylog2.

the class StreamAlertConditionResource method update.

@PUT
@Timed
@Path("{conditionId}")
@ApiOperation(value = "Modify an alert condition")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
@AuditEvent(type = AuditEventTypes.ALERT_CONDITION_UPDATE)
public void update(@ApiParam(name = "streamId", value = "The stream id the alert condition belongs to.", required = true) @PathParam("streamId") String streamid, @ApiParam(name = "conditionId", value = "The alert condition id.", required = true) @PathParam("conditionId") String conditionid, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CreateConditionRequest ccr) throws NotFoundException, ValidationException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamid);
    final Stream stream = streamService.load(streamid);
    AlertCondition alertCondition = streamService.getAlertCondition(stream, conditionid);
    try {
        final AlertCondition updatedCondition = alertService.updateFromRequest(alertCondition, convertConfigurationInRequest(ccr));
        streamService.updateAlertCondition(stream, updatedCondition);
    } catch (ConfigurationException e) {
        throw new BadRequestException("Invalid alert condition parameters", e);
    }
}
Also used : ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) BadRequestException(javax.ws.rs.BadRequestException) Stream(org.graylog2.plugin.streams.Stream) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with CreateConditionRequest

use of org.graylog2.rest.models.streams.alerts.requests.CreateConditionRequest in project graylog2-server by Graylog2.

the class StreamAlertConditionResource method convertConfigurationInRequest.

private CreateConditionRequest convertConfigurationInRequest(final CreateConditionRequest request) {
    final AlertCondition.Factory factory = alertConditionMap.get(request.type());
    if (factory == null) {
        throw new BadRequestException("Unable to load alert condition of type " + request.type());
    }
    final ConfigurationRequest requestedConfiguration = factory.config().getRequestedConfiguration();
    // coerce the configuration to their correct types according to the condition's requested config
    final Map<String, Object> parameters;
    try {
        parameters = ConfigurationMapConverter.convertValues(request.parameters(), requestedConfiguration);
    } catch (ValidationException e) {
        throw new BadRequestException("Invalid alert condition parameters", e);
    }
    return request.toBuilder().setParameters(parameters).build();
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) BadRequestException(javax.ws.rs.BadRequestException)

Example 3 with CreateConditionRequest

use of org.graylog2.rest.models.streams.alerts.requests.CreateConditionRequest in project graylog2-server by Graylog2.

the class StreamAlertConditionResource method create.

@POST
@Timed
@ApiOperation(value = "Create an alert condition")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
@AuditEvent(type = AuditEventTypes.ALERT_CONDITION_CREATE)
public Response create(@ApiParam(name = "streamId", value = "The stream id this new alert condition belongs to.", required = true) @PathParam("streamId") String streamid, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CreateConditionRequest ccr) throws NotFoundException, ValidationException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamid);
    final Stream stream = streamService.load(streamid);
    try {
        final AlertCondition alertCondition = alertService.fromRequest(convertConfigurationInRequest(ccr), stream, getCurrentUser().getName());
        streamService.addAlertCondition(stream, alertCondition);
        final Map<String, String> result = ImmutableMap.of("alert_condition_id", alertCondition.getId());
        final URI alertConditionUri = getUriBuilderToSelf().path(StreamAlertConditionResource.class).path("{conditionId}").build(stream.getId(), alertCondition.getId());
        return Response.created(alertConditionUri).entity(result).build();
    } catch (ConfigurationException e) {
        throw new BadRequestException("Invalid alert condition parameters", e);
    }
}
Also used : ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) BadRequestException(javax.ws.rs.BadRequestException) Stream(org.graylog2.plugin.streams.Stream) URI(java.net.URI) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

BadRequestException (javax.ws.rs.BadRequestException)3 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)3 Timed (com.codahale.metrics.annotation.Timed)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 AuditEvent (org.graylog2.audit.jersey.AuditEvent)2 ConfigurationException (org.graylog2.plugin.configuration.ConfigurationException)2 Stream (org.graylog2.plugin.streams.Stream)2 URI (java.net.URI)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 ConfigurationRequest (org.graylog2.plugin.configuration.ConfigurationRequest)1 ValidationException (org.graylog2.plugin.database.ValidationException)1