use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.
the class StreamAlarmCallbackResource method update.
@PUT
@Path("/{alarmCallbackId}")
@Timed
@ApiOperation(value = "Update an alarm callback")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.ALARM_CALLBACK_UPDATE)
public void update(@ApiParam(name = "streamid", value = "The stream id this alarm callback belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(name = "alarmCallbackId", required = true) @PathParam("alarmCallbackId") String alarmCallbackId, @ApiParam(name = "JSON body", required = true) CreateAlarmCallbackRequest alarmCallbackRequest) throws NotFoundException {
checkPermission(RestPermissions.STREAMS_EDIT, streamid);
final AlarmCallbackConfiguration callbackConfiguration = alarmCallbackConfigurationService.load(alarmCallbackId);
if (callbackConfiguration == null) {
throw new NotFoundException("Unable to find alarm callback configuration " + alarmCallbackId);
}
final Map<String, Object> configuration = convertConfigurationValues(alarmCallbackRequest);
final AlarmCallbackConfiguration updatedConfig = ((AlarmCallbackConfigurationImpl) callbackConfiguration).toBuilder().setTitle(alarmCallbackRequest.title()).setConfiguration(configuration).build();
try {
alarmCallbackFactory.create(updatedConfig).checkConfiguration();
alarmCallbackConfigurationService.save(updatedConfig);
} catch (ValidationException | AlarmCallbackConfigurationException | ConfigurationException e) {
LOG.error("Invalid alarm callback configuration.", e);
throw new BadRequestException(e.getMessage(), e);
} catch (ClassNotFoundException e) {
LOG.error("Invalid alarm callback type.", e);
throw new BadRequestException("Invalid alarm callback type.", e);
}
}
use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.
the class StreamAlarmCallbackResource method delete.
@DELETE
@Path("/{alarmCallbackId}")
@Timed
@ApiOperation(value = "Delete an alarm callback")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Alarm callback not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
@AuditEvent(type = AuditEventTypes.ALARM_CALLBACK_DELETE)
public void delete(@ApiParam(name = "streamid", value = "The stream id this alarm callback belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(name = "alarmCallbackId", required = true) @PathParam("alarmCallbackId") String alarmCallbackId) throws NotFoundException {
checkPermission(RestPermissions.STREAMS_EDIT, streamid);
final Stream stream = streamService.load(streamid);
final AlarmCallbackConfiguration result = alarmCallbackConfigurationService.load(alarmCallbackId);
if (result == null || !result.getStreamId().equals(stream.getId())) {
throw new javax.ws.rs.NotFoundException("Couldn't find alarm callback " + alarmCallbackId + " in for steam " + streamid);
}
if (alarmCallbackConfigurationService.destroy(result) == 0) {
final String msg = "Couldn't remove alarm callback with ID " + result.getId();
LOG.error(msg);
throw new InternalServerErrorException(msg);
}
}
use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.
the class AlarmCallbackConfigurationServiceImplTest method testGetForStreamSingleDocument.
@Test
@MongoDBFixtures("alarmCallbackConfigurationsSingleDocument.json")
public void testGetForStreamSingleDocument() throws Exception {
final Stream stream = mock(StreamImpl.class);
final String streamId = "5400deadbeefdeadbeefaffe";
when(stream.getId()).thenReturn(streamId);
final List<AlarmCallbackConfiguration> configs = alarmCallbackConfigurationService.getForStream(stream);
final AlarmCallbackConfiguration alarmCallback = configs.get(0);
assertNotNull("Returned list should not be null", configs);
assertEquals("Returned list should contain a single document", 1, configs.size());
assertNotNull("Returned Alarm Callback should not be null", alarmCallback);
}
use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.
the class AlarmCallbackHistoryServiceImplTest method testSuccess.
@Test
public void testSuccess() throws Exception {
final AlarmCallbackConfiguration alarmCallbackConfiguration = mockAlarmCallbackConfiguration(new Date());
final Alert alert = mockAlert();
final AlertCondition alertCondition = mockAlertCondition();
final AlarmCallbackHistory alarmCallbackHistory = this.alarmCallbackHistoryService.success(alarmCallbackConfiguration, alert, alertCondition);
verifyAlarmCallbackHistory(alarmCallbackHistory, alert, alertCondition);
assertThat(alarmCallbackHistory.result()).isNotNull().isInstanceOf(AlarmCallbackSuccess.class);
assertThat(alarmCallbackHistory.result().type()).isEqualTo("success");
}
use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.
the class V20161125161400_AlertReceiversMigrationTest method doMigrateMultipleQualifyingStreams.
@Test
public void doMigrateMultipleQualifyingStreams() throws Exception {
final String matchingStreamId1 = new ObjectId().toHexString();
final String matchingStreamId2 = new ObjectId().toHexString();
final Stream stream1 = mock(Stream.class);
when(stream1.getAlertReceivers()).thenReturn(Collections.emptyMap());
final Stream stream2 = mock(Stream.class);
when(stream2.getAlertReceivers()).thenReturn(ImmutableMap.of("users", ImmutableList.of("foouser"), "emails", ImmutableList.of("foo@bar.com")));
when(stream2.getId()).thenReturn(matchingStreamId1);
final Stream stream3 = mock(Stream.class);
when(stream3.getAlertReceivers()).thenReturn(ImmutableMap.of("users", ImmutableList.of("foouser2")));
when(stream3.getId()).thenReturn(matchingStreamId2);
when(this.streamService.loadAll()).thenReturn(ImmutableList.of(stream1, stream2, stream3));
final AlertCondition alertCondition1 = mock(AlertCondition.class);
final AlertCondition alertCondition2 = mock(AlertCondition.class);
when(this.streamService.getAlertConditions(eq(stream2))).thenReturn(ImmutableList.of(alertCondition1));
when(this.streamService.getAlertConditions(eq(stream3))).thenReturn(ImmutableList.of(alertCondition2));
final String alarmCallbackId1 = new ObjectId().toHexString();
final AlarmCallbackConfiguration alarmCallback1 = AlarmCallbackConfigurationImpl.create(alarmCallbackId1, matchingStreamId1, EmailAlarmCallback.class.getCanonicalName(), "Email Alert Notification", new HashMap<>(), new Date(), "admin");
final String alarmCallbackId2 = new ObjectId().toHexString();
final AlarmCallbackConfiguration alarmCallback2 = AlarmCallbackConfigurationImpl.create(alarmCallbackId2, matchingStreamId2, EmailAlarmCallback.class.getCanonicalName(), "Email Alert Notification", new HashMap<>(), new Date(), "admin");
final String alarmCallbackId3 = new ObjectId().toHexString();
final AlarmCallbackConfiguration alarmCallback3 = AlarmCallbackConfigurationImpl.create(alarmCallbackId3, matchingStreamId2, EmailAlarmCallback.class.getCanonicalName(), "Email Alert Notification", new HashMap<>(), new Date(), "admin");
final String alarmCallbackId4 = new ObjectId().toHexString();
final AlarmCallbackConfiguration alarmCallback4 = AlarmCallbackConfigurationImpl.create(alarmCallbackId4, matchingStreamId2, HTTPAlarmCallback.class.getCanonicalName(), "Email Alert Notification", new HashMap<>(), new Date(), "admin");
when(alarmCallbackConfigurationService.getForStream(eq(stream2))).thenReturn(ImmutableList.of(alarmCallback1));
when(alarmCallbackConfigurationService.getForStream(eq(stream3))).thenReturn(ImmutableList.of(alarmCallback2, alarmCallback3, alarmCallback4));
when(alarmCallbackConfigurationService.save(eq(alarmCallback1))).thenReturn(alarmCallbackId1);
when(alarmCallbackConfigurationService.save(eq(alarmCallback2))).thenReturn(alarmCallbackId2);
when(alarmCallbackConfigurationService.save(eq(alarmCallback3))).thenReturn(alarmCallbackId3);
when(this.dbCollection.update(any(BasicDBObject.class), any(BasicDBObject.class))).thenReturn(new WriteResult(1, true, matchingStreamId1));
when(this.dbCollection.update(any(BasicDBObject.class), any(BasicDBObject.class))).thenReturn(new WriteResult(1, true, matchingStreamId2));
this.alertReceiversMigration.upgrade();
final ArgumentCaptor<AlarmCallbackConfiguration> configurationArgumentCaptor = ArgumentCaptor.forClass(AlarmCallbackConfiguration.class);
verify(this.alarmCallbackConfigurationService, times(3)).save(configurationArgumentCaptor.capture());
final List<AlarmCallbackConfiguration> configurationValues = configurationArgumentCaptor.getAllValues();
assertThat(configurationValues).isNotNull().isNotEmpty().hasSize(3).contains(alarmCallback1).contains(alarmCallback2).contains(alarmCallback3);
for (AlarmCallbackConfiguration configurationValue : configurationValues) {
if (configurationValue.getStreamId().equals(matchingStreamId1)) {
assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_EMAIL_RECEIVERS)).size()).isEqualTo(1);
assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_EMAIL_RECEIVERS)).get(0)).isEqualTo("foo@bar.com");
assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).size()).isEqualTo(1);
assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).get(0)).isEqualTo("foouser");
}
if (configurationValue.getStreamId().equals(matchingStreamId2)) {
assertThat(configurationValue.getConfiguration().get(EmailAlarmCallback.CK_EMAIL_RECEIVERS)).isNull();
assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).size()).isEqualTo(1);
assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).get(0)).isEqualTo("foouser2");
}
}
final ArgumentCaptor<BasicDBObject> queryCaptor = ArgumentCaptor.forClass(BasicDBObject.class);
final ArgumentCaptor<BasicDBObject> updateCaptor = ArgumentCaptor.forClass(BasicDBObject.class);
verify(this.dbCollection, times(2)).update(queryCaptor.capture(), updateCaptor.capture());
final List<BasicDBObject> queries = queryCaptor.getAllValues();
for (BasicDBObject query : queries) {
final String streamId = (queries.indexOf(query) == 0 ? matchingStreamId1 : matchingStreamId2);
assertThat(query.toJson()).isEqualTo("{\"_id\": {\"$oid\": \"" + streamId + "\"}}");
}
updateCaptor.getAllValues().forEach(update -> assertThat(update.toJson()).isEqualTo("{\"$unset\": {\"" + StreamImpl.FIELD_ALERT_RECEIVERS + "\": \"\"}}"));
verifyMigrationCompletedWasPosted(ImmutableMap.of(matchingStreamId1, Optional.of(alarmCallbackId1), matchingStreamId2, Optional.of(alarmCallbackId2 + ", " + alarmCallbackId3)));
}
Aggregations