use of com.redhat.cloud.notifications.ingress.Action in project notifications-backend by RedHatInsights.
the class EmailSubscriptionTypeProcessor method sendEmail.
private List<NotificationHistory> sendEmail(Event event, Set<Endpoint> endpoints, EmailTemplate emailTemplate) {
EmailSubscriptionType emailSubscriptionType = EmailSubscriptionType.INSTANT;
processedEmailCount.increment();
Action action = event.getAction();
TemplateInstance subject;
TemplateInstance body;
if (useTemplatesFromDb) {
Optional<InstantEmailTemplate> instantEmailTemplate = templateRepository.findInstantEmailTemplate(event.getEventType().getId());
if (instantEmailTemplate.isEmpty()) {
return Collections.emptyList();
} else {
String subjectData = instantEmailTemplate.get().getSubjectTemplate().getData();
subject = templateService.compileTemplate(subjectData, "subject");
String bodyData = instantEmailTemplate.get().getBodyTemplate().getData();
body = templateService.compileTemplate(bodyData, "body");
}
} else {
if (!emailTemplate.isSupported(action.getEventType(), emailSubscriptionType)) {
return Collections.emptyList();
}
subject = emailTemplate.getTitle(action.getEventType(), emailSubscriptionType);
body = emailTemplate.getBody(action.getEventType(), emailSubscriptionType);
}
if (subject == null || body == null) {
return Collections.emptyList();
}
Set<RecipientSettings> requests = Stream.concat(endpoints.stream().map(EndpointRecipientSettings::new), ActionRecipientSettings.fromAction(action).stream()).collect(Collectors.toSet());
Set<String> subscribers = Set.copyOf(emailSubscriptionRepository.getEmailSubscribersUserId(action.getAccountId(), action.getBundle(), action.getApplication(), emailSubscriptionType));
LOG.info("sending email for event: " + event);
return recipientResolver.recipientUsers(action.getAccountId(), action.getOrgId(), requests, subscribers).stream().map(user -> emailSender.sendEmail(user, event, subject, body)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
}
use of com.redhat.cloud.notifications.ingress.Action in project notifications-backend by RedHatInsights.
the class FromCamelHistoryFiller method reinjectIfNeeded.
private void reinjectIfNeeded(Map<String, Object> payloadMap) {
if (!enableReInject || (payloadMap.containsKey("successful") && ((Boolean) payloadMap.get("successful")))) {
return;
}
String historyId = (String) payloadMap.get("historyId");
log.infof("Notification with id %s was not successful, resubmitting for further processing", historyId);
Endpoint ep = notificationHistoryRepository.getEndpointForHistoryId(historyId);
Event event = new Event();
Payload.PayloadBuilder payloadBuilder = new Payload.PayloadBuilder();
payloadMap.forEach(payloadBuilder::withAdditionalProperty);
// TODO augment with details from Endpoint and original event
event.setPayload(payloadBuilder.build());
// Save the original id, as we may need it in the future.
Context.ContextBuilder contextBuilder = new Context.ContextBuilder();
contextBuilder.withAdditionalProperty("original-id", historyId);
if (ep != null) {
// TODO For the current tests. EP should not be null in real life
contextBuilder.withAdditionalProperty("failed-integration", ep.getName());
}
Action action = new Action.ActionBuilder().withId(UUID.randomUUID()).withBundle("console").withApplication("notifications").withEventType("integration-failed").withAccountId(ep != null ? ep.getAccountId() : "").withContext(contextBuilder.build()).withTimestamp(LocalDateTime.now(ZoneOffset.UTC)).withEvents(Collections.singletonList(event)).withRecipients(Collections.singletonList(new Recipient.RecipientBuilder().withOnlyAdmins(true).withIgnoreUserPreferences(true).build())).build();
String ser = Parser.encode(action);
// Add the message id in Kafka header for the de-duplicator
Message<String> message = buildMessageWithId(ser);
emitter.send(message);
}
use of com.redhat.cloud.notifications.ingress.Action in project notifications-backend by RedHatInsights.
the class TestHelpers method createEmailAggregation.
static EmailAggregation createEmailAggregation(String tenant, String orgId, String bundle, String application, String policyId, String inventoryId) {
EmailAggregation aggregation = new EmailAggregation();
aggregation.setBundleName(bundle);
aggregation.setApplicationName(application);
aggregation.setAccountId(tenant);
aggregation.setOrgId(orgId);
aggregation.setCreated(LocalDateTime.now(UTC).minusHours(5L));
Action emailActionMessage = new Action();
emailActionMessage.setBundle(bundle);
emailActionMessage.setApplication(application);
emailActionMessage.setTimestamp(LocalDateTime.now());
emailActionMessage.setEventType("testEmailSubscriptionInstant");
emailActionMessage.setContext(new Context.ContextBuilder().withAdditionalProperty("inventory_id", inventoryId).withAdditionalProperty("system_check_in", "2020-08-03T15:22:42.199046").withAdditionalProperty("display_name", "My test machine").withAdditionalProperty("tags", List.of()).build());
emailActionMessage.setEvents(List.of(new Event.EventBuilder().withMetadata(new Metadata.MetadataBuilder().build()).withPayload(new Payload.PayloadBuilder().withAdditionalProperty("policy_id", policyId).withAdditionalProperty("policy_name", "not-tested-name").withAdditionalProperty("policy_description", "not-used-desc").withAdditionalProperty("policy_condition", "not-used-condition").build()).build()));
emailActionMessage.setAccountId(tenant);
JsonObject payload = baseTransformer.transform(emailActionMessage);
aggregation.setPayload(payload);
return aggregation;
}
use of com.redhat.cloud.notifications.ingress.Action in project notifications-backend by RedHatInsights.
the class EventConsumerTest method testInvalidMessageId.
@Test
void testInvalidMessageId() {
EventType eventType = mockGetEventTypeAndCreateEvent();
Action action = buildValidAction();
String payload = serializeAction(action);
Message<String> message = buildMessageWithId("I am not a valid UUID!".getBytes(UTF_8), payload);
inMemoryConnector.source(INGRESS_CHANNEL).send(message);
micrometerAssertionHelper.awaitAndAssertTimerIncrement(CONSUMED_TIMER_NAME, 1);
assertEquals(1L, registry.timer(CONSUMED_TIMER_NAME, "bundle", action.getBundle(), "application", action.getApplication()).count());
micrometerAssertionHelper.assertCounterIncrement(MESSAGE_ID_INVALID_COUNTER_NAME, 1);
assertNoCounterIncrement(REJECTED_COUNTER_NAME, PROCESSING_ERROR_COUNTER_NAME, PROCESSING_EXCEPTION_COUNTER_NAME, DUPLICATE_COUNTER_NAME, MESSAGE_ID_VALID_COUNTER_NAME, MESSAGE_ID_MISSING_COUNTER_NAME);
verifyExactlyOneProcessing(eventType, payload, action);
verify(kafkaMessageDeduplicator, times(1)).registerMessageId(null);
}
use of com.redhat.cloud.notifications.ingress.Action in project notifications-backend by RedHatInsights.
the class EventConsumerTest method testProcessingErrorWithoutMessageId.
@Test
void testProcessingErrorWithoutMessageId() {
EventType eventType = mockGetEventTypeAndCreateEvent();
mockProcessingFailure();
Action action = buildValidAction();
String payload = serializeAction(action);
inMemoryConnector.source(INGRESS_CHANNEL).send(payload);
micrometerAssertionHelper.awaitAndAssertTimerIncrement(CONSUMED_TIMER_NAME, 1);
assertEquals(1L, registry.timer(CONSUMED_TIMER_NAME, "bundle", action.getBundle(), "application", action.getApplication()).count());
micrometerAssertionHelper.assertCounterIncrement(MESSAGE_ID_MISSING_COUNTER_NAME, 1);
micrometerAssertionHelper.assertCounterIncrement(PROCESSING_ERROR_COUNTER_NAME, 1);
assertNoCounterIncrement(REJECTED_COUNTER_NAME, DUPLICATE_COUNTER_NAME, MESSAGE_ID_VALID_COUNTER_NAME, MESSAGE_ID_INVALID_COUNTER_NAME);
verifyExactlyOneProcessing(eventType, payload, action);
verify(kafkaMessageDeduplicator, times(1)).registerMessageId(null);
}
Aggregations