Search in sources :

Example 11 with Endpoint

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;
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint)

Example 12 with Endpoint

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());
                }
            }
        }
    }
}
Also used : CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) EndpointProperties(com.redhat.cloud.notifications.models.EndpointProperties) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) Endpoint(com.redhat.cloud.notifications.models.Endpoint) Logger(org.jboss.logging.Logger) Set(java.util.Set) EMAIL_SUBSCRIPTION(com.redhat.cloud.notifications.models.EndpointType.EMAIL_SUBSCRIPTION) UUID(java.util.UUID) CAMEL(com.redhat.cloud.notifications.models.EndpointType.CAMEL) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) WEBHOOK(com.redhat.cloud.notifications.models.EndpointType.WEBHOOK) Inject(javax.inject.Inject) HashSet(java.util.HashSet) EventType(com.redhat.cloud.notifications.models.EventType) List(java.util.List) EndpointType(com.redhat.cloud.notifications.models.EndpointType) StatelessSessionFactory(com.redhat.cloud.notifications.db.StatelessSessionFactory) Map(java.util.Map) Optional(java.util.Optional) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Endpoint(com.redhat.cloud.notifications.models.Endpoint) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) UUID(java.util.UUID)

Example 13 with Endpoint

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;
}
Also used : CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) EndpointProperties(com.redhat.cloud.notifications.models.EndpointProperties) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) Endpoint(com.redhat.cloud.notifications.models.Endpoint) Logger(org.jboss.logging.Logger) Set(java.util.Set) EMAIL_SUBSCRIPTION(com.redhat.cloud.notifications.models.EndpointType.EMAIL_SUBSCRIPTION) UUID(java.util.UUID) CAMEL(com.redhat.cloud.notifications.models.EndpointType.CAMEL) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) WEBHOOK(com.redhat.cloud.notifications.models.EndpointType.WEBHOOK) Inject(javax.inject.Inject) HashSet(java.util.HashSet) EventType(com.redhat.cloud.notifications.models.EventType) List(java.util.List) EndpointType(com.redhat.cloud.notifications.models.EndpointType) StatelessSessionFactory(com.redhat.cloud.notifications.db.StatelessSessionFactory) Map(java.util.Map) Optional(java.util.Optional) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Endpoint(com.redhat.cloud.notifications.models.Endpoint) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties)

Example 14 with 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);
}
Also used : Buffer(io.vertx.mutiny.core.buffer.Buffer) Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) JsonObject(io.vertx.core.json.JsonObject)

Example 15 with Endpoint

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;
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) Endpoint(com.redhat.cloud.notifications.models.Endpoint)

Aggregations

Endpoint (com.redhat.cloud.notifications.models.Endpoint)57 Test (org.junit.jupiter.api.Test)23 JsonObject (io.vertx.core.json.JsonObject)22 QuarkusTest (io.quarkus.test.junit.QuarkusTest)20 WebhookProperties (com.redhat.cloud.notifications.models.WebhookProperties)18 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)15 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)14 NotificationHistory (com.redhat.cloud.notifications.models.NotificationHistory)14 Header (io.restassured.http.Header)13 List (java.util.List)13 Transactional (javax.transaction.Transactional)13 Inject (javax.inject.Inject)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 EndpointType (com.redhat.cloud.notifications.models.EndpointType)10 Collectors (java.util.stream.Collectors)10 EmailSubscriptionProperties (com.redhat.cloud.notifications.models.EmailSubscriptionProperties)9 Event (com.redhat.cloud.notifications.models.Event)9 Response (io.restassured.response.Response)9 Map (java.util.Map)9 UUID (java.util.UUID)9