use of org.graylog2.alarmcallbacks.AlarmCallbackConfigurationImpl in project graylog2-server by Graylog2.
the class V20161125161400_AlertReceiversMigration method updateConfiguration.
private Optional<String> updateConfiguration(Stream stream, AlarmCallbackConfiguration callbackConfiguration) {
final Map<String, List<String>> alertReceivers = stream.getAlertReceivers();
final List<String> usernames = alertReceivers.get("users");
final List<String> emails = alertReceivers.get("emails");
final Map<String, Object> configuration = callbackConfiguration.getConfiguration();
if (usernames != null && !usernames.isEmpty()) {
LOG.debug("Moving users alert receivers from stream <" + stream.getId() + ">");
configuration.put(EmailAlarmCallback.CK_USER_RECEIVERS, usernames);
}
if (emails != null && !emails.isEmpty()) {
LOG.debug("Moving emails alert receivers from stream <" + stream.getId() + ">");
configuration.put(EmailAlarmCallback.CK_EMAIL_RECEIVERS, emails);
}
final AlarmCallbackConfigurationImpl updatedConfiguration = ((AlarmCallbackConfigurationImpl) callbackConfiguration).toBuilder().setConfiguration(configuration).build();
try {
final String callbackId = this.alarmCallbackService.save(updatedConfiguration);
LOG.debug("Successfully created email alarm callback <" + callbackId + "> for stream <" + stream.getId() + ">.");
return Optional.of(callbackId);
} catch (ValidationException e) {
LOG.error("Unable to create email alarm callback for stream <" + stream.getId() + ">: ", e);
}
return Optional.empty();
}
use of org.graylog2.alarmcallbacks.AlarmCallbackConfigurationImpl 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.AlarmCallbackConfigurationImpl in project graylog2-server by Graylog2.
the class StreamServiceImpl method updateCallbackConfiguration.
// I tried to be sorry, really. https://www.youtube.com/watch?v=3KVyRqloGmk
private void updateCallbackConfiguration(String action, String type, String entity, List<AlarmCallbackConfiguration> streamCallbacks) {
final AtomicBoolean ran = new AtomicBoolean(false);
streamCallbacks.stream().filter(callback -> callback.getType().equals(EmailAlarmCallback.class.getCanonicalName())).forEach(callback -> {
ran.set(true);
final Map<String, Object> configuration = callback.getConfiguration();
String key;
if ("users".equals(type)) {
key = EmailAlarmCallback.CK_USER_RECEIVERS;
} else {
key = EmailAlarmCallback.CK_EMAIL_RECEIVERS;
}
@SuppressWarnings("unchecked") final List<String> recipients = (List<String>) configuration.get(key);
if ("add".equals(action)) {
if (!recipients.contains(entity)) {
recipients.add(entity);
}
} else {
if (recipients.contains(entity)) {
recipients.remove(entity);
}
}
configuration.put(key, recipients);
final AlarmCallbackConfiguration updatedConfig = ((AlarmCallbackConfigurationImpl) callback).toBuilder().setConfiguration(configuration).build();
try {
alarmCallbackConfigurationService.save(updatedConfig);
} catch (ValidationException e) {
throw new BadRequestException("Unable to save alarm callback configuration", e);
}
});
if (!ran.get()) {
throw new BadRequestException("Unable to " + action + " receiver: Stream has no email alarm callback.");
}
}
Aggregations