diff options
Diffstat (limited to 'orchestrator/src/main')
-rw-r--r-- | orchestrator/src/main/java/com/yahoo/vespa/orchestrator/resources/HostSuspensionHandler.java (renamed from orchestrator/src/main/java/com/yahoo/vespa/orchestrator/resources/hostsuspension/HostSuspensionResource.java) | 61 |
1 files changed, 35 insertions, 26 deletions
diff --git a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/resources/hostsuspension/HostSuspensionResource.java b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/resources/HostSuspensionHandler.java index 4cb22792237..ca720ec8b68 100644 --- a/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/resources/hostsuspension/HostSuspensionResource.java +++ b/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/resources/HostSuspensionHandler.java @@ -1,21 +1,21 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.orchestrator.resources.hostsuspension; +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.orchestrator.resources; import com.google.common.util.concurrent.UncheckedTimeoutException; -import com.yahoo.container.jaxrs.annotation.Component; +import com.google.inject.Inject; +import com.yahoo.container.jdisc.LoggingRequestHandler; +import com.yahoo.jdisc.Response; +import com.yahoo.restapi.JacksonJsonResponse; +import com.yahoo.restapi.RestApi; +import com.yahoo.restapi.RestApiException; +import com.yahoo.restapi.RestApiRequestHandler; import com.yahoo.vespa.applicationmodel.HostName; import com.yahoo.vespa.orchestrator.BatchHostNameNotFoundException; import com.yahoo.vespa.orchestrator.BatchInternalErrorException; import com.yahoo.vespa.orchestrator.Orchestrator; import com.yahoo.vespa.orchestrator.policy.BatchHostStateChangeDeniedException; -import com.yahoo.vespa.orchestrator.restapi.HostSuspensionApi; import com.yahoo.vespa.orchestrator.restapi.wire.BatchOperationResult; -import javax.inject.Inject; -import javax.ws.rs.Path; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -23,50 +23,59 @@ import java.util.stream.Collectors; /** * @author hakonhall + * @author bjorncs */ -@Path("") -public class HostSuspensionResource implements HostSuspensionApi { +public class HostSuspensionHandler extends RestApiRequestHandler<HostSuspensionHandler> { - private static final Logger log = Logger.getLogger(HostSuspensionResource.class.getName()); + private static final Logger log = Logger.getLogger(HostSuspensionHandler.class.getName()); private final Orchestrator orchestrator; @Inject - public HostSuspensionResource(@Component Orchestrator orchestrator) { + public HostSuspensionHandler(LoggingRequestHandler.Context context, Orchestrator orchestrator) { + super(context, HostSuspensionHandler::createRestApiDefinition); this.orchestrator = orchestrator; } - @Override - public BatchOperationResult suspendAll(String parentHostnameString, List<String> hostnamesAsStrings) { + private static RestApi createRestApiDefinition(HostSuspensionHandler self) { + return RestApi.builder() + .addRoute(RestApi.route("/orchestrator/v1/suspensions/hosts/{hostname}") + .put(self::suspendAll)) + .registerJacksonResponseEntity(BatchOperationResult.class) + .build(); + } + + private BatchOperationResult suspendAll(RestApi.RequestContext context) { + String parentHostnameString = context.pathParameters().getStringOrThrow("hostname"); + List<String> hostnamesAsStrings = context.queryParameters().getStringList("hostname"); + HostName parentHostname = new HostName(parentHostnameString); List<HostName> hostnames = hostnamesAsStrings.stream().map(HostName::new).collect(Collectors.toList()); try { orchestrator.suspendAll(parentHostname, hostnames); } catch (BatchHostStateChangeDeniedException e) { log.log(Level.FINE, "Failed to suspend nodes " + hostnames + " with parent host " + parentHostname, e); - throw createWebApplicationException(e.getMessage(), Response.Status.CONFLICT); + throw createRestApiException(e.getMessage(), Response.Status.CONFLICT, e); } catch (UncheckedTimeoutException e) { log.log(Level.FINE, "Failed to suspend nodes " + hostnames + " with parent host " + parentHostname, e); - throw createWebApplicationException(e.getMessage(), Response.Status.CONFLICT); + throw createRestApiException(e.getMessage(), Response.Status.CONFLICT, e); } catch (BatchHostNameNotFoundException e) { log.log(Level.FINE, "Failed to suspend nodes " + hostnames + " with parent host " + parentHostname, e); // Note that we're returning BAD_REQUEST instead of NOT_FOUND because the resource identified // by the URL path was found. It's one of the hostnames in the request it failed to find. - throw createWebApplicationException(e.getMessage(), Response.Status.BAD_REQUEST); + throw createRestApiException(e.getMessage(), Response.Status.BAD_REQUEST, e); } catch (BatchInternalErrorException e) { log.log(Level.FINE, "Failed to suspend nodes " + hostnames + " with parent host " + parentHostname, e); - throw createWebApplicationException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR); + throw createRestApiException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR, e); } log.log(Level.FINE, "Suspended " + hostnames + " with parent " + parentHostname); return BatchOperationResult.successResult(); } - private WebApplicationException createWebApplicationException(String errorMessage, Response.Status status) { - return new WebApplicationException( - Response.status(status) - .entity(new BatchOperationResult(errorMessage)) - .type(MediaType.APPLICATION_JSON_TYPE) - .build()); + private RestApiException createRestApiException(String errorMessage, int statusCode, Throwable cause) { + return new RestApiException( + new JacksonJsonResponse<>(statusCode, new BatchOperationResult(errorMessage), true), + errorMessage, + cause); } - } |