Search in sources :

Example 6 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project helios by spotify.

the class HostsResource method list.

/**
   * Returns the list of hostnames of known hosts/agents.
   * @return The list of hostnames.
   */
@GET
@Produces(APPLICATION_JSON)
@Timed
@ExceptionMetered
public List<String> list(@QueryParam("namePattern") final String namePattern, @QueryParam("selector") final List<String> hostSelectors) {
    List<String> hosts = model.listHosts();
    if (namePattern != null) {
        final Predicate<String> matchesPattern = Pattern.compile(namePattern).asPredicate();
        hosts = hosts.stream().filter(matchesPattern).collect(Collectors.toList());
    }
    if (!hostSelectors.isEmpty()) {
        // check that all supplied selectors are parseable/valid
        final List<HostSelector> selectors = hostSelectors.stream().map(selectorStr -> {
            final HostSelector parsed = HostSelector.parse(selectorStr);
            if (parsed == null) {
                throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Invalid host selector: " + selectorStr).build());
            }
            return parsed;
        }).collect(Collectors.toList());
        final Map<String, Map<String, String>> hostsAndLabels = getLabels(hosts);
        final HostMatcher matcher = new HostMatcher(hostsAndLabels);
        hosts = matcher.getMatchingHosts(selectors);
    }
    return hosts;
}
Also used : Produces(javax.ws.rs.Produces) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) SetGoalResponse(com.spotify.helios.common.protocol.SetGoalResponse) HostRegisterResponse(com.spotify.helios.common.protocol.HostRegisterResponse) Valid(javax.validation.Valid) QueryParam(javax.ws.rs.QueryParam) Optional(com.google.common.base.Optional) JobUndeployResponse(com.spotify.helios.common.protocol.JobUndeployResponse) Map(java.util.Map) INVALID_ID(com.spotify.helios.common.protocol.JobUndeployResponse.Status.INVALID_ID) Deployment(com.spotify.helios.common.descriptors.Deployment) DefaultValue(javax.ws.rs.DefaultValue) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) HostStillInUseException(com.spotify.helios.master.HostStillInUseException) JobDeployResponse(com.spotify.helios.common.protocol.JobDeployResponse) DELETE(javax.ws.rs.DELETE) HostMatcher(com.spotify.helios.master.HostMatcher) Responses.notFound(com.spotify.helios.master.http.Responses.notFound) JobDoesNotExistException(com.spotify.helios.master.JobDoesNotExistException) FORBIDDEN(com.spotify.helios.common.protocol.JobUndeployResponse.Status.FORBIDDEN) Predicate(java.util.function.Predicate) JOB_NOT_FOUND(com.spotify.helios.common.protocol.JobUndeployResponse.Status.JOB_NOT_FOUND) Collectors(java.util.stream.Collectors) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Response(javax.ws.rs.core.Response) JobPortAllocationConflictException(com.spotify.helios.master.JobPortAllocationConflictException) WebApplicationException(javax.ws.rs.WebApplicationException) Responses.badRequest(com.spotify.helios.master.http.Responses.badRequest) Pattern(java.util.regex.Pattern) JobId(com.spotify.helios.common.descriptors.JobId) PathParam(javax.ws.rs.PathParam) HOST_NOT_FOUND(com.spotify.helios.common.protocol.JobUndeployResponse.Status.HOST_NOT_FOUND) GET(javax.ws.rs.GET) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) OK(com.spotify.helios.common.protocol.JobUndeployResponse.Status.OK) PATCH(com.spotify.helios.master.http.PATCH) Function(java.util.function.Function) Responses.forbidden(com.spotify.helios.master.http.Responses.forbidden) HostSelector(com.spotify.helios.common.descriptors.HostSelector) EMPTY_TOKEN(com.spotify.helios.common.descriptors.Job.EMPTY_TOKEN) HostStatus(com.spotify.helios.common.descriptors.HostStatus) HostNotFoundException(com.spotify.helios.master.HostNotFoundException) JobAlreadyDeployedException(com.spotify.helios.master.JobAlreadyDeployedException) MasterModel(com.spotify.helios.master.MasterModel) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) HostDeregisterResponse(com.spotify.helios.common.protocol.HostDeregisterResponse) Maps(com.google.common.collect.Maps) TokenVerificationException(com.spotify.helios.master.TokenVerificationException) PUT(javax.ws.rs.PUT) JobNotDeployedException(com.spotify.helios.master.JobNotDeployedException) WebApplicationException(javax.ws.rs.WebApplicationException) HostSelector(com.spotify.helios.common.descriptors.HostSelector) Map(java.util.Map) HostMatcher(com.spotify.helios.master.HostMatcher) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 7 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project helios by spotify.

the class HostsResource method jobPut.

/**
   * Sets the deployment of the job identified by its {@link JobId} on the host named by
   * {@code host} to {@code deployment}.
   * @param host The host to deploy to.
   * @param jobId The job to deploy.
   * @param deployment Deployment information.
   * @param username The user deploying.
   * @param token The authorization token for this deployment.
   * @return The response.
   */
@PUT
@Path("/{host}/jobs/{job}")
@Produces(APPLICATION_JSON)
@Timed
@ExceptionMetered
public JobDeployResponse jobPut(@PathParam("host") final String host, @PathParam("job") final JobId jobId, @Valid final Deployment deployment, @RequestUser final String username, @QueryParam("token") @DefaultValue(EMPTY_TOKEN) final String token) {
    if (!jobId.isFullyQualified()) {
        throw badRequest(new JobDeployResponse(JobDeployResponse.Status.INVALID_ID, host, jobId));
    }
    try {
        final Deployment actualDeployment = deployment.toBuilder().setDeployerUser(username).build();
        model.deployJob(host, actualDeployment, token);
        return new JobDeployResponse(JobDeployResponse.Status.OK, host, jobId);
    } catch (JobAlreadyDeployedException e) {
        throw badRequest(new JobDeployResponse(JobDeployResponse.Status.JOB_ALREADY_DEPLOYED, host, jobId));
    } catch (HostNotFoundException e) {
        throw badRequest(new JobDeployResponse(JobDeployResponse.Status.HOST_NOT_FOUND, host, jobId));
    } catch (JobDoesNotExistException e) {
        throw badRequest(new JobDeployResponse(JobDeployResponse.Status.JOB_NOT_FOUND, host, jobId));
    } catch (JobPortAllocationConflictException e) {
        throw badRequest(new JobDeployResponse(JobDeployResponse.Status.PORT_CONFLICT, host, jobId));
    } catch (TokenVerificationException e) {
        throw forbidden(new JobDeployResponse(JobDeployResponse.Status.FORBIDDEN, host, jobId));
    }
}
Also used : JobPortAllocationConflictException(com.spotify.helios.master.JobPortAllocationConflictException) JobDoesNotExistException(com.spotify.helios.master.JobDoesNotExistException) HostNotFoundException(com.spotify.helios.master.HostNotFoundException) TokenVerificationException(com.spotify.helios.master.TokenVerificationException) Deployment(com.spotify.helios.common.descriptors.Deployment) JobAlreadyDeployedException(com.spotify.helios.master.JobAlreadyDeployedException) JobDeployResponse(com.spotify.helios.common.protocol.JobDeployResponse) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) PUT(javax.ws.rs.PUT)

Example 8 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project helios by spotify.

the class HostsResource method put.

/**
   * Registers a host with the cluster.  The {@code host} is the name of the host.  It SHOULD be
   * the hostname of the machine.  The {@code id} should be a persistent value for the host, but
   * initially randomly generated.  This way we don't have two machines claiming to be the same
   * host: at least by accident.
   * @param host The host to register.
   * @param id The randomly generated ID for the host.
   * @return The response.
   */
@PUT
@Path("{host}")
@Produces(APPLICATION_JSON)
@Timed
@ExceptionMetered
public Response.Status put(@PathParam("host") final String host, @QueryParam("id") @DefaultValue("") final String id) {
    if (isNullOrEmpty(id)) {
        throw badRequest(new HostRegisterResponse(HostRegisterResponse.Status.INVALID_ID, host));
    }
    model.registerHost(host, id);
    log.info("added host {}", host);
    return Response.Status.OK;
}
Also used : HostRegisterResponse(com.spotify.helios.common.protocol.HostRegisterResponse) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) PUT(javax.ws.rs.PUT)

Example 9 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project helios by spotify.

the class VersionResource method versionCheck.

/**
   * Given the client version, returns the version status, i.e. whether or not they should be
   * compatible or not.
   * @param client The client version.
   * @return The VersionCheckResponse object.
   */
@GET
@Path("/check")
@Produces(APPLICATION_JSON)
@Timed
@ExceptionMetered
public VersionCheckResponse versionCheck(@QueryParam("client") @DefaultValue("") final String client) {
    final PomVersion serverVersion = PomVersion.parse(Version.POM_VERSION);
    final VersionCompatibility.Status status;
    if (isNullOrEmpty(client)) {
        return new VersionCheckResponse(VersionCompatibility.Status.MISSING, serverVersion, Version.RECOMMENDED_VERSION);
    }
    final PomVersion clientVersion = PomVersion.parse(client);
    status = VersionCompatibility.getStatus(serverVersion, clientVersion);
    return new VersionCheckResponse(status, serverVersion, Version.RECOMMENDED_VERSION);
}
Also used : VersionCheckResponse(com.spotify.helios.common.VersionCheckResponse) VersionCompatibility(com.spotify.helios.common.VersionCompatibility) PomVersion(com.spotify.helios.common.PomVersion) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 10 with ExceptionMetered

use of com.codahale.metrics.annotation.ExceptionMetered in project keywhiz by square.

the class GroupResource method deleteGroup.

/**
   * Delete a group
   *
   * @excludeParams automationClient
   * @param name Group name to delete
   *
   * @responseMessage 204 Group deleted
   * @responseMessage 404 Group not found
   */
@Timed
@ExceptionMetered
@DELETE
@Path("{name}")
public Response deleteGroup(@Auth AutomationClient automationClient, @PathParam("name") String name) {
    Group group = groupDAOReadWrite.getGroup(name).orElseThrow(NotFoundException::new);
    // Group memberships are deleted automatically by DB cascading.
    groupDAOReadWrite.deleteGroup(group);
    auditLog.recordEvent(new Event(Instant.now(), EventTag.GROUP_DELETE, automationClient.getName(), group.getName()));
    return Response.noContent().build();
}
Also used : Group(keywhiz.api.model.Group) NotFoundException(javax.ws.rs.NotFoundException) Event(keywhiz.log.Event) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Aggregations

ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)54 Timed (com.codahale.metrics.annotation.Timed)53 Path (javax.ws.rs.Path)36 Event (keywhiz.log.Event)29 HashMap (java.util.HashMap)28 NotFoundException (javax.ws.rs.NotFoundException)27 POST (javax.ws.rs.POST)25 Consumes (javax.ws.rs.Consumes)20 Produces (javax.ws.rs.Produces)20 DELETE (javax.ws.rs.DELETE)18 GET (javax.ws.rs.GET)17 Group (keywhiz.api.model.Group)16 SanitizedSecret (keywhiz.api.model.SanitizedSecret)16 Response (javax.ws.rs.core.Response)12 AutomationClient (keywhiz.api.model.AutomationClient)12 ConflictException (keywhiz.service.exceptions.ConflictException)12 Client (keywhiz.api.model.Client)11 Secret (keywhiz.api.model.Secret)11 URI (java.net.URI)9 PUT (javax.ws.rs.PUT)9