Search in sources :

Example 11 with SystemServiceId

use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by cdapio.

the class MonitorClient method getSystemServiceStatus.

/**
 * Gets the status of a system service.
 *
 * @param serviceName Name of the system service
 * @return status of the system service
 * @throws IOException if a network error occurred
 * @throws NotFoundException if the system service with the specified name could not be found
 * @throws BadRequestException if the operation was not valid for the system service
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public String getSystemServiceStatus(String serviceName) throws IOException, NotFoundException, BadRequestException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveURL(String.format("system/services/%s/status", serviceName));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    String responseBody = new String(response.getResponseBody());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(new SystemServiceId(serviceName));
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(responseBody);
    }
    Map<String, String> status = GSON.fromJson(responseBody, new TypeToken<Map<String, String>>() {
    }.getType());
    return status.get("status");
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) TypeToken(com.google.common.reflect.TypeToken) HttpResponse(io.cdap.common.http.HttpResponse) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) URL(java.net.URL)

Example 12 with SystemServiceId

use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by cdapio.

the class MonitorClient method getSystemServiceInstances.

/**
 * Gets the number of instances the system service is running on.
 *
 * @param serviceName name of the system service
 * @return number of instances the system service is running on
 * @throws IOException if a network error occurred
 * @throws NotFoundException if the system service with the specified name was not found
 * @throws ServiceNotEnabledException if the system service is not currently enabled
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public int getSystemServiceInstances(String serviceName) throws IOException, NotFoundException, ServiceNotEnabledException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveURL(String.format("system/services/%s/instances", serviceName));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_FORBIDDEN);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(new SystemServiceId(serviceName));
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN) {
        throw new ServiceNotEnabledException(serviceName);
    }
    return ObjectResponse.fromJsonBody(response, Instances.class).getResponseObject().getInstances();
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) ServiceNotEnabledException(io.cdap.cdap.common.ServiceNotEnabledException) HttpResponse(io.cdap.common.http.HttpResponse) NotFoundException(io.cdap.cdap.common.NotFoundException) URL(java.net.URL)

Example 13 with SystemServiceId

use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by cdapio.

the class MonitorHandlerAuthorizationTest method testRestartServiceInstanceAuthorization.

@Test
public void testRestartServiceInstanceAuthorization() throws Exception {
    SystemServiceId systemServiceId = new SystemServiceId(SERVICE_NAME);
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(systemServiceId), Arrays.asList(ApplicationPermission.EXECUTE));
    FullHttpRequest request = mock(FullHttpRequest.class);
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.restartServiceInstance(request, responder, SERVICE_NAME, 0);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.restartServiceInstance(request, responder, SERVICE_NAME, 0);
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) HttpResponder(io.cdap.http.HttpResponder) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 14 with SystemServiceId

use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by cdapio.

the class MonitorHandler method getLatestRestartServiceInstanceStatus.

/**
 * Send request to get the status of latest restart instances request for a CDAP system service.
 */
@Path("/system/services/{service-name}/latest-restart")
@GET
public void getLatestRestartServiceInstanceStatus(HttpRequest request, HttpResponder responder, @PathParam("service-name") String serviceName) throws Exception {
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    SystemServiceId systemServiceId = new SystemServiceId(serviceName);
    contextAccessEnforcer.enforce(systemServiceId, StandardPermission.GET);
    try {
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(serviceStore.getLatestRestartInstancesRequest(serviceName)));
    } catch (IllegalStateException ex) {
        throw new NotFoundException(String.format("No restart instances request found or %s", serviceName));
    }
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) NotFoundException(io.cdap.cdap.common.NotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 15 with SystemServiceId

use of io.cdap.cdap.proto.id.SystemServiceId in project cdap by cdapio.

the class MonitorHandler method setServiceInstance.

/**
 * Sets the number of instances of CDAP Services
 */
@Path("/system/services/{service-name}/instances")
@PUT
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void setServiceInstance(FullHttpRequest request, HttpResponder responder, @PathParam("service-name") final String serviceName) throws Exception {
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    SystemServiceId systemServiceId = new SystemServiceId(serviceName);
    contextAccessEnforcer.enforce(systemServiceId, StandardPermission.UPDATE);
    MasterServiceManager serviceManager = serviceManagementMap.get(serviceName);
    int instances = getInstances(request);
    if (!serviceManager.isServiceEnabled()) {
        throw new ForbiddenException(String.format("Service %s is not enabled", serviceName));
    }
    int currentInstances = getSystemServiceInstanceCount(serviceName);
    if (instances < serviceManager.getMinInstances() || instances > serviceManager.getMaxInstances()) {
        String response = String.format("Instance count should be between [%s,%s]", serviceManager.getMinInstances(), serviceManager.getMaxInstances());
        throw new BadRequestException(response);
    } else if (instances == currentInstances) {
        responder.sendStatus(HttpResponseStatus.OK);
        return;
    }
    serviceStore.setServiceInstance(serviceName, instances);
    if (serviceManager.setInstances(instances)) {
        responder.sendStatus(HttpResponseStatus.OK);
    } else {
        throw new BadRequestException("Operation did not succeed");
    }
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) ForbiddenException(io.cdap.cdap.common.ForbiddenException) MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Aggregations

SystemServiceId (io.cdap.cdap.proto.id.SystemServiceId)38 NotFoundException (io.cdap.cdap.common.NotFoundException)22 Test (org.junit.Test)16 MonitorHandler (io.cdap.cdap.gateway.handlers.MonitorHandler)14 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)14 HttpResponder (io.cdap.http.HttpResponder)14 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)14 BadRequestException (io.cdap.cdap.common.BadRequestException)12 ForbiddenException (io.cdap.cdap.common.ForbiddenException)12 MasterServiceManager (io.cdap.cdap.common.twill.MasterServiceManager)12 Path (javax.ws.rs.Path)12 HttpResponse (io.cdap.common.http.HttpResponse)8 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)8 URL (java.net.URL)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)6 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)6 GET (javax.ws.rs.GET)6 Instances (io.cdap.cdap.proto.Instances)4 HttpRequest (io.netty.handler.codec.http.HttpRequest)4 PUT (javax.ws.rs.PUT)4