Search in sources :

Example 1 with EndpointType

use of com.redhat.cloud.notifications.models.EndpointType in project notifications-backend by RedHatInsights.

the class EndpointResource method disableEndpoint.

@DELETE
@Path("/{id}/enable")
@RolesAllowed(ConsoleIdentityProvider.RBAC_WRITE_INTEGRATIONS_ENDPOINTS)
@APIResponse(responseCode = "204", description = "The integration has been disabled", content = @Content(schema = @Schema(type = SchemaType.STRING)))
@Transactional
public Response disableEndpoint(@Context SecurityContext sec, @PathParam("id") UUID id) {
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    EndpointType endpointType = endpointRepository.getEndpointTypeById(principal.getAccount(), id);
    checkSystemEndpoint(endpointType);
    endpointRepository.disableEndpoint(principal.getAccount(), id);
    return Response.noContent().build();
}
Also used : RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) EndpointType(com.redhat.cloud.notifications.models.EndpointType) CompositeEndpointType(com.redhat.cloud.notifications.models.CompositeEndpointType) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) RolesAllowed(javax.annotation.security.RolesAllowed) APIResponse(org.eclipse.microprofile.openapi.annotations.responses.APIResponse) Transactional(javax.transaction.Transactional)

Example 2 with EndpointType

use of com.redhat.cloud.notifications.models.EndpointType in project notifications-backend by RedHatInsights.

the class EndpointResource method enableEndpoint.

@PUT
@Path("/{id}/enable")
@Produces(TEXT_PLAIN)
@RolesAllowed(ConsoleIdentityProvider.RBAC_WRITE_INTEGRATIONS_ENDPOINTS)
@APIResponse(responseCode = "200", content = @Content(schema = @Schema(type = SchemaType.STRING)))
@Transactional
public Response enableEndpoint(@Context SecurityContext sec, @PathParam("id") UUID id) {
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    EndpointType endpointType = endpointRepository.getEndpointTypeById(principal.getAccount(), id);
    checkSystemEndpoint(endpointType);
    endpointRepository.enableEndpoint(principal.getAccount(), id);
    return Response.ok().build();
}
Also used : RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) EndpointType(com.redhat.cloud.notifications.models.EndpointType) CompositeEndpointType(com.redhat.cloud.notifications.models.CompositeEndpointType) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) APIResponse(org.eclipse.microprofile.openapi.annotations.responses.APIResponse) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT) Transactional(javax.transaction.Transactional)

Example 3 with EndpointType

use of com.redhat.cloud.notifications.models.EndpointType in project notifications-backend by RedHatInsights.

the class EndpointProcessor method process.

public void process(Event event) {
    processedItems.increment();
    List<Endpoint> endpoints = endpointRepository.getTargetEndpoints(event.getAccountId(), event.getEventType());
    // Target endpoints are grouped by endpoint type.
    endpointTargeted.increment(endpoints.size());
    Map<EndpointType, List<Endpoint>> endpointsByType = endpoints.stream().collect(Collectors.groupingBy(Endpoint::getType));
    for (Map.Entry<EndpointType, List<Endpoint>> entry : endpointsByType.entrySet()) {
        /*
             * For each endpoint type, the list of target endpoints is sent alongside with the action to the relevant processor.
             * Each processor returns a list of history entries. All of the returned lists are flattened into a single list.
             */
        EndpointTypeProcessor processor = endpointTypeToProcessor(entry.getKey());
        List<NotificationHistory> historyEntries = processor.process(event, entry.getValue());
        // Now each history entry is persisted.
        for (NotificationHistory history : historyEntries) {
            try {
                notificationHistoryRepository.createNotificationHistory(history);
            } catch (Exception e) {
                LOGGER.errorf("Notification history creation failed for %s", history.getEndpoint());
            }
        }
    }
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) EndpointTypeProcessor(com.redhat.cloud.notifications.processors.EndpointTypeProcessor) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) EndpointType(com.redhat.cloud.notifications.models.EndpointType) List(java.util.List) Map(java.util.Map)

Example 4 with EndpointType

use of com.redhat.cloud.notifications.models.EndpointType 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 5 with EndpointType

use of com.redhat.cloud.notifications.models.EndpointType 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)

Aggregations

EndpointType (com.redhat.cloud.notifications.models.EndpointType)8 CompositeEndpointType (com.redhat.cloud.notifications.models.CompositeEndpointType)5 RolesAllowed (javax.annotation.security.RolesAllowed)5 Path (javax.ws.rs.Path)5 RhIdPrincipal (com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal)4 Endpoint (com.redhat.cloud.notifications.models.Endpoint)4 List (java.util.List)4 Map (java.util.Map)4 Transactional (javax.transaction.Transactional)4 APIResponse (org.eclipse.microprofile.openapi.annotations.responses.APIResponse)4 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)3 EndpointProperties (com.redhat.cloud.notifications.models.EndpointProperties)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 UUID (java.util.UUID)3 Collectors (java.util.stream.Collectors)3 Inject (javax.inject.Inject)3 Produces (javax.ws.rs.Produces)3 StatelessSessionFactory (com.redhat.cloud.notifications.db.StatelessSessionFactory)2 EmailSubscriptionProperties (com.redhat.cloud.notifications.models.EmailSubscriptionProperties)2