Search in sources :

Example 1 with AlertType

use of io.hops.hopsworks.persistence.entity.alertmanager.AlertType in project hopsworks by logicalclocks.

the class AdminReceiverResource method create.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create a receiver.")
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN" })
public Response create(PostableReceiverDTO postableReceiverDTO, @QueryParam("defaultName") @DefaultValue("false") Boolean defaultName, @QueryParam("defaultTemplate") @DefaultValue("false") Boolean defaultTemplate, @Context UriInfo uriInfo, @Context SecurityContext sc) throws AlertException {
    if (postableReceiverDTO == null) {
        throw new AlertException(RESTCodes.AlertErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "No payload.");
    }
    Receiver receiver;
    try {
        AlertType alertType = null;
        if (defaultName || Strings.isNullOrEmpty(postableReceiverDTO.getName()) || AlertType.fromReceiverName(postableReceiverDTO.getName()) != null) {
            alertType = getDefaultName(postableReceiverDTO);
            postableReceiverDTO.setName(alertType.getReceiverName());
        }
        receiver = receiverBuilder.build(postableReceiverDTO, defaultTemplate, true);
        alertManagerConfiguration.addReceiver(receiver);
        if (alertType != null) {
            // to create a single route for global receivers
            alertController.createRoute(alertType);
        }
        ResourceRequest resourceRequest = new ResourceRequest(ResourceRequest.Name.RECEIVERS);
        ReceiverDTO dto = receiverBuilder.build(uriInfo, resourceRequest, receiver.getName(), null);
        dto.setHref(uriInfo.getAbsolutePathBuilder().path(receiver.getName()).build());
        return Response.created(dto.getHref()).entity(dto).build();
    } catch (AlertManagerConfigCtrlCreateException | AlertManagerUnreachableException | AlertManagerConfigReadException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.FAILED_TO_READ_CONFIGURATION, Level.FINE, e.getMessage());
    } catch (AlertManagerDuplicateEntryException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.RECEIVER_EXIST, Level.FINE, e.getMessage());
    } catch (AlertManagerConfigUpdateException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.FAILED_TO_UPDATE_AM_CONFIG, Level.FINE, e.getMessage());
    } catch (AlertManagerClientCreateException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.FAILED_TO_CONNECT, Level.FINE, e.getMessage());
    } catch (AlertManagerAccessControlException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.ACCESS_CONTROL_EXCEPTION, Level.FINE, e.getMessage());
    } catch (AlertManagerNoSuchElementException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.RECEIVER_NOT_FOUND, Level.FINE, e.getMessage());
    }
}
Also used : ReceiverDTO(io.hops.hopsworks.api.alert.receiver.ReceiverDTO) PostableReceiverDTO(io.hops.hopsworks.api.alert.receiver.PostableReceiverDTO) AlertManagerUnreachableException(io.hops.hopsworks.alert.exception.AlertManagerUnreachableException) AlertManagerAccessControlException(io.hops.hopsworks.alert.exception.AlertManagerAccessControlException) Receiver(io.hops.hopsworks.alerting.config.dto.Receiver) AlertManagerConfigUpdateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException) AlertManagerClientCreateException(io.hops.hopsworks.alerting.exceptions.AlertManagerClientCreateException) AlertType(io.hops.hopsworks.persistence.entity.alertmanager.AlertType) AlertManagerDuplicateEntryException(io.hops.hopsworks.alerting.exceptions.AlertManagerDuplicateEntryException) AlertManagerNoSuchElementException(io.hops.hopsworks.alerting.exceptions.AlertManagerNoSuchElementException) AlertManagerConfigCtrlCreateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigCtrlCreateException) ResourceRequest(io.hops.hopsworks.common.api.ResourceRequest) AlertManagerConfigReadException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigReadException) AlertException(io.hops.hopsworks.exceptions.AlertException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with AlertType

use of io.hops.hopsworks.persistence.entity.alertmanager.AlertType in project hopsworks by logicalclocks.

the class AdminReceiverResource method getDefaultName.

private AlertType getDefaultName(PostableReceiverDTO postableReceiverDTO) throws AlertException {
    AlertType alertType = null;
    int configs = 0;
    if (postableReceiverDTO.getEmailConfigs() != null && postableReceiverDTO.getEmailConfigs().size() > 0) {
        alertType = AlertType.GLOBAL_ALERT_EMAIL;
        configs++;
    }
    if (postableReceiverDTO.getSlackConfigs() != null && postableReceiverDTO.getSlackConfigs().size() > 0) {
        alertType = AlertType.GLOBAL_ALERT_SLACK;
        configs++;
    }
    if (postableReceiverDTO.getPagerdutyConfigs() != null && postableReceiverDTO.getPagerdutyConfigs().size() > 0) {
        alertType = AlertType.GLOBAL_ALERT_PAGERDUTY;
        configs++;
    }
    if (postableReceiverDTO.getPushoverConfigs() != null && postableReceiverDTO.getPushoverConfigs().size() > 0) {
        alertType = AlertType.GLOBAL_ALERT_PUSHOVER;
        configs++;
    }
    if (postableReceiverDTO.getOpsgenieConfigs() != null && postableReceiverDTO.getOpsgenieConfigs().size() > 0) {
        alertType = AlertType.GLOBAL_ALERT_OPSGENIE;
        configs++;
    }
    if (postableReceiverDTO.getWebhookConfigs() != null && postableReceiverDTO.getWebhookConfigs().size() > 0) {
        alertType = AlertType.GLOBAL_ALERT_WEBHOOK;
        configs++;
    }
    if (postableReceiverDTO.getVictoropsConfigs() != null && postableReceiverDTO.getVictoropsConfigs().size() > 0) {
        alertType = AlertType.GLOBAL_ALERT_VICTOROPS;
        configs++;
    }
    if (postableReceiverDTO.getWechatConfigs() != null && postableReceiverDTO.getWechatConfigs().size() > 0) {
        alertType = AlertType.GLOBAL_ALERT_WEBCHAT;
        configs++;
    }
    if (configs > 1 || alertType == null) {
        throw new AlertException(RESTCodes.AlertErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "Receiver name not set.");
    }
    return alertType;
}
Also used : AlertType(io.hops.hopsworks.persistence.entity.alertmanager.AlertType) AlertException(io.hops.hopsworks.exceptions.AlertException)

Aggregations

AlertException (io.hops.hopsworks.exceptions.AlertException)2 AlertType (io.hops.hopsworks.persistence.entity.alertmanager.AlertType)2 AlertManagerAccessControlException (io.hops.hopsworks.alert.exception.AlertManagerAccessControlException)1 AlertManagerUnreachableException (io.hops.hopsworks.alert.exception.AlertManagerUnreachableException)1 Receiver (io.hops.hopsworks.alerting.config.dto.Receiver)1 AlertManagerClientCreateException (io.hops.hopsworks.alerting.exceptions.AlertManagerClientCreateException)1 AlertManagerConfigCtrlCreateException (io.hops.hopsworks.alerting.exceptions.AlertManagerConfigCtrlCreateException)1 AlertManagerConfigReadException (io.hops.hopsworks.alerting.exceptions.AlertManagerConfigReadException)1 AlertManagerConfigUpdateException (io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException)1 AlertManagerDuplicateEntryException (io.hops.hopsworks.alerting.exceptions.AlertManagerDuplicateEntryException)1 AlertManagerNoSuchElementException (io.hops.hopsworks.alerting.exceptions.AlertManagerNoSuchElementException)1 PostableReceiverDTO (io.hops.hopsworks.api.alert.receiver.PostableReceiverDTO)1 ReceiverDTO (io.hops.hopsworks.api.alert.receiver.ReceiverDTO)1 ResourceRequest (io.hops.hopsworks.common.api.ResourceRequest)1 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)1 ApiOperation (io.swagger.annotations.ApiOperation)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1