Search in sources :

Example 11 with AlertManagerConfigUpdateException

use of io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException in project hopsworks by logicalclocks.

the class AlertManagerConfiguration method saveReceiverToDatabase.

private void saveReceiverToDatabase(String name, Receiver receiver) throws AlertManagerConfigUpdateException {
    try {
        ObjectMapper objectMapper = new ObjectMapper();
        JSONObject jsonObject = new JSONObject(objectMapper.writeValueAsString(receiver));
        AlertReceiver alertReceiver;
        Optional<AlertReceiver> optionalAlertReceiver = alertReceiverFacade.findByName(name);
        if (optionalAlertReceiver.isPresent()) {
            alertReceiver = optionalAlertReceiver.get();
            alertReceiver.setName(receiver.getName());
            alertReceiver.setConfig(jsonObject);
            alertReceiverFacade.update(alertReceiver);
        } else {
            alertReceiver = new AlertReceiver(receiver.getName(), jsonObject);
            alertReceiverFacade.save(alertReceiver);
        }
    } catch (JsonProcessingException e) {
        throw new AlertManagerConfigUpdateException("Failed to save receiver to database. Failed to parse receiver to json. " + e.getMessage(), e);
    }
}
Also used : JSONObject(org.json.JSONObject) AlertReceiver(io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver) AlertManagerConfigUpdateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 12 with AlertManagerConfigUpdateException

use of io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException in project hopsworks by logicalclocks.

the class ReceiverResource method update.

@PUT
@Path("{name}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update a receiver.", response = SilenceDTO.class)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER })
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
public Response update(@PathParam("name") String name, @QueryParam("defaultTemplate") @DefaultValue("false") Boolean defaultTemplate, PostableReceiverDTO postableReceiverDTO, @Context UriInfo uriInfo, @Context SecurityContext sc) throws AlertException, ProjectException {
    if (postableReceiverDTO == null) {
        throw new AlertException(RESTCodes.AlertErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "No payload.");
    }
    Receiver receiver = receiverBuilder.build(postableReceiverDTO, defaultTemplate, true);
    validateReceiverOneConfig(receiver);
    try {
        alertManagerConfiguration.updateReceiver(name, receiver, getProject());
    } 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 (AlertManagerNoSuchElementException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.RECEIVER_NOT_FOUND, Level.FINE, e.getMessage());
    } catch (AlertManagerAccessControlException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.ACCESS_CONTROL_EXCEPTION, Level.FINE, e.getMessage());
    } catch (IllegalArgumentException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.ILLEGAL_ARGUMENT, Level.FINE, e.getMessage());
    }
    ResourceRequest resourceRequest = new ResourceRequest(ResourceRequest.Name.RECEIVERS);
    ReceiverDTO dto = receiverBuilder.build(uriInfo, resourceRequest, receiver.getName(), getProject());
    return Response.ok().entity(dto).build();
}
Also used : 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) 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) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) ApiOperation(io.swagger.annotations.ApiOperation) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles) PUT(javax.ws.rs.PUT)

Example 13 with AlertManagerConfigUpdateException

use of io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException in project hopsworks by logicalclocks.

the class RouteResource method update.

@PUT
@Path("{receiver}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update a route.")
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER })
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
public Response update(@PathParam("receiver") String receiver, PostableRouteDTO route, @QueryParam("match") List<String> match, @QueryParam("matchRe") List<String> matchRe, @Context UriInfo uriInfo, @Context SecurityContext sc) throws AlertException, ProjectException {
    if (route == null) {
        throw new AlertException(RESTCodes.AlertErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "No payload.");
    }
    Route routeToUpdate = new Route(receiver).withMatch(routeBuilder.toMap(match)).withMatchRe(routeBuilder.toMap(matchRe));
    Route updatedRoute = routeBuilder.toRoute(route);
    try {
        alertManagerConfiguration.updateRoute(routeToUpdate, updatedRoute, getProject());
    } 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 (AlertManagerNoSuchElementException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.ROUTE_NOT_FOUND, Level.FINE, e.getMessage());
    } catch (AlertManagerAccessControlException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.ACCESS_CONTROL_EXCEPTION, Level.FINE, e.getMessage());
    } catch (IllegalArgumentException e) {
        throw new AlertException(RESTCodes.AlertErrorCode.ILLEGAL_ARGUMENT, Level.FINE, e.getMessage());
    }
    ResourceRequest resourceRequest = new ResourceRequest(ResourceRequest.Name.ROUTES);
    RouteDTO dto = routeBuilder.build(uriInfo, resourceRequest, updatedRoute, getProject());
    return Response.ok().entity(dto).build();
}
Also used : AlertManagerUnreachableException(io.hops.hopsworks.alert.exception.AlertManagerUnreachableException) AlertManagerAccessControlException(io.hops.hopsworks.alert.exception.AlertManagerAccessControlException) AlertManagerConfigUpdateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException) AlertManagerClientCreateException(io.hops.hopsworks.alerting.exceptions.AlertManagerClientCreateException) 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) Route(io.hops.hopsworks.alerting.config.dto.Route) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) ApiOperation(io.swagger.annotations.ApiOperation) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles) PUT(javax.ws.rs.PUT)

Example 14 with AlertManagerConfigUpdateException

use of io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException in project hopsworks by logicalclocks.

the class AlertManagerConfigController method write.

/**
 * Writes alertManagerConfig to configFile in YAML format.
 * Do not use if you are not sure the yaml is well-formed.
 * User writeAndReload instead that will rolls back if it loading the yaml fails
 * @param alertManagerConfig
 * @throws IOException
 */
public void write(AlertManagerConfig alertManagerConfig) throws AlertManagerConfigUpdateException {
    YAMLFactory yamlFactory = new YAMLFactory().enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
    ObjectMapper objectMapper = new ObjectMapper(yamlFactory);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    try {
        objectMapper.writeValue(configFile, alertManagerConfig);
    } catch (IOException e) {
        throw new AlertManagerConfigUpdateException("Failed to update configuration file. Error " + e.getMessage());
    }
}
Also used : YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) AlertManagerConfigUpdateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 15 with AlertManagerConfigUpdateException

use of io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException in project hopsworks by logicalclocks.

the class AlertManagerConfigController method writeAndReload.

/**
 * Writes alertManagerConfig to configFile in YAML format.
 * Rolls back if it fails to reload the file to the alertmanager.
 * @param alertManagerConfig
 * @throws IOException
 * @throws AlertManagerConfigUpdateException
 */
public void writeAndReload(AlertManagerConfig alertManagerConfig) throws AlertManagerConfigUpdateException, AlertManagerServerException, AlertManagerConfigReadException {
    AlertManagerConfig alertManagerConfigTmp = read();
    write(alertManagerConfig);
    try {
        client.reload();
    } catch (AlertManagerResponseException e) {
        write(alertManagerConfigTmp);
        throw new AlertManagerConfigUpdateException("Failed to update AlertManagerConfig. " + e.getMessage(), e);
    } catch (AlertManagerServerException se) {
        write(alertManagerConfigTmp);
        throw se;
    }
}
Also used : AlertManagerConfig(io.hops.hopsworks.alerting.config.dto.AlertManagerConfig) AlertManagerConfigUpdateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException) AlertManagerServerException(io.hops.hopsworks.alerting.exceptions.AlertManagerServerException) AlertManagerResponseException(io.hops.hopsworks.alerting.exceptions.AlertManagerResponseException)

Aggregations

AlertManagerConfigUpdateException (io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException)20 AlertManagerUnreachableException (io.hops.hopsworks.alert.exception.AlertManagerUnreachableException)14 AlertManagerClientCreateException (io.hops.hopsworks.alerting.exceptions.AlertManagerClientCreateException)14 AlertManagerConfigCtrlCreateException (io.hops.hopsworks.alerting.exceptions.AlertManagerConfigCtrlCreateException)14 AlertManagerConfigReadException (io.hops.hopsworks.alerting.exceptions.AlertManagerConfigReadException)14 AlertException (io.hops.hopsworks.exceptions.AlertException)13 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)13 Consumes (javax.ws.rs.Consumes)13 Produces (javax.ws.rs.Produces)13 Path (javax.ws.rs.Path)9 AlertManagerDuplicateEntryException (io.hops.hopsworks.alerting.exceptions.AlertManagerDuplicateEntryException)8 ResourceRequest (io.hops.hopsworks.common.api.ResourceRequest)8 ApiOperation (io.swagger.annotations.ApiOperation)8 PUT (javax.ws.rs.PUT)8 AlertManagerNoSuchElementException (io.hops.hopsworks.alerting.exceptions.AlertManagerNoSuchElementException)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Receiver (io.hops.hopsworks.alerting.config.dto.Receiver)5 Route (io.hops.hopsworks.alerting.config.dto.Route)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 AlertManagerAccessControlException (io.hops.hopsworks.alert.exception.AlertManagerAccessControlException)4