use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class EndpointRepository method getTargetEndpoints.
public List<Endpoint> getTargetEndpoints(String tenant, EventType eventType) {
String query = "SELECT DISTINCT e FROM Endpoint e JOIN e.behaviorGroupActions bga JOIN bga.behaviorGroup.behaviors b " + "WHERE e.enabled IS TRUE AND b.eventType = :eventType AND (bga.behaviorGroup.accountId = :accountId OR bga.behaviorGroup.accountId IS NULL)";
List<Endpoint> endpoints = statelessSessionFactory.getCurrentSession().createQuery(query, Endpoint.class).setParameter("eventType", eventType).setParameter("accountId", tenant).getResultList();
loadProperties(endpoints);
for (Endpoint endpoint : endpoints) {
if (endpoint.getAccountId() == null) {
if (endpoint.getType() == EMAIL_SUBSCRIPTION) {
endpoint.setAccountId(tenant);
} else {
LOGGER.warnf("Invalid endpoint configured in default behavior group: %s", endpoint.getId());
}
}
}
return endpoints;
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class EndpointRepository method loadTypedProperties.
private <T extends EndpointProperties> void loadTypedProperties(Class<T> typedEndpointClass, Set<Endpoint> endpoints, EndpointType type) {
Map<UUID, Endpoint> endpointsMap = endpoints.stream().filter(e -> e.getType().equals(type)).collect(Collectors.toMap(Endpoint::getId, Function.identity()));
if (endpointsMap.size() > 0) {
String hql = "FROM " + typedEndpointClass.getSimpleName() + " WHERE id IN (:endpointIds)";
List<T> propList = statelessSessionFactory.getCurrentSession().createQuery(hql, typedEndpointClass).setParameter("endpointIds", endpointsMap.keySet()).getResultList();
for (T props : propList) {
if (props != null) {
Endpoint endpoint = endpointsMap.get(props.getId());
endpoint.setProperties(props);
// Todo: NOTIF-429 backward compatibility change - Remove soon.
if (typedEndpointClass.equals(CamelProperties.class)) {
endpoint.getProperties(CamelProperties.class).setSubType(endpoint.getSubType());
}
}
}
}
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class EndpointRepository method getOrCreateDefaultEmailSubscription.
/**
* The purpose of this method is to find or create an EMAIL_SUBSCRIPTION endpoint with empty properties. This
* endpoint is used to aggregate and store in the DB the email actions outcome, which will be used later by the
* event log. The recipients of the current email action have already been resolved before this step, possibly from
* multiple endpoints and recipients settings. The properties created below have no impact on the resolution of the
* action recipients.
*/
public Endpoint getOrCreateDefaultEmailSubscription(String accountId) {
String query = "FROM Endpoint WHERE accountId = :accountId AND compositeType.type = :endpointType";
List<Endpoint> emailEndpoints = statelessSessionFactory.getCurrentSession().createQuery(query, Endpoint.class).setParameter("accountId", accountId).setParameter("endpointType", EMAIL_SUBSCRIPTION).getResultList();
loadProperties(emailEndpoints);
EmailSubscriptionProperties properties = new EmailSubscriptionProperties();
Optional<Endpoint> endpointOptional = emailEndpoints.stream().filter(endpoint -> properties.hasSameProperties(endpoint.getProperties(EmailSubscriptionProperties.class))).findFirst();
if (endpointOptional.isPresent()) {
return endpointOptional.get();
}
Endpoint endpoint = new Endpoint();
endpoint.setProperties(properties);
endpoint.setAccountId(accountId);
endpoint.setEnabled(true);
endpoint.setDescription("System email endpoint");
endpoint.setName("Email endpoint");
endpoint.setType(EMAIL_SUBSCRIPTION);
endpoint.prePersist();
properties.setEndpoint(endpoint);
statelessSessionFactory.getCurrentSession().insert(endpoint);
statelessSessionFactory.getCurrentSession().insert(endpoint.getProperties());
return endpoint;
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class WebhookTypeProcessor method process.
private NotificationHistory process(Notification item) {
processedCount.increment();
Endpoint endpoint = item.getEndpoint();
WebhookProperties properties = endpoint.getProperties(WebhookProperties.class);
final HttpRequest<Buffer> req = getWebClient(properties.getDisableSslVerification()).requestAbs(HttpMethod.valueOf(properties.getMethod().name()), properties.getUrl());
if (properties.getSecretToken() != null && !properties.getSecretToken().isBlank()) {
req.putHeader(TOKEN_HEADER, properties.getSecretToken());
}
if (properties.getBasicAuthentication() != null) {
req.basicAuthentication(properties.getBasicAuthentication().getUsername(), properties.getBasicAuthentication().getPassword());
}
JsonObject payload = transformer.transform(item.getEvent().getAction());
return doHttpRequest(item, req, payload);
}
use of com.redhat.cloud.notifications.models.Endpoint in project notifications-backend by RedHatInsights.
the class ResourceHelpers method createTestEndpoints.
public int[] createTestEndpoints(String tenant, int count) {
int[] statsValues = new int[3];
statsValues[0] = count;
for (int i = 0; i < count; i++) {
// Add new endpoints
WebhookProperties properties = new WebhookProperties();
properties.setMethod(HttpType.POST);
properties.setUrl("https://localhost");
Endpoint ep = new Endpoint();
ep.setType(WEBHOOK);
ep.setName(String.format("Endpoint %d", count - i));
ep.setDescription("Automatically generated");
boolean enabled = (i % (count / 5)) != 0;
if (!enabled) {
statsValues[1]++;
}
ep.setEnabled(enabled);
if (i > 0) {
statsValues[2]++;
ep.setProperties(properties);
}
ep.setAccountId(tenant);
endpointRepository.createEndpoint(ep);
}
return statsValues;
}
Aggregations