Search in sources :

Example 1 with SystemServiceId

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

the class MonitorClient method getSystemServiceLiveInfo.

/**
 * Gets the live info of a system service.
 *
 * @param serviceName Name of the system service
 * @return live info of the system service
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public SystemServiceLiveInfo getSystemServiceLiveInfo(String serviceName) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    URL url = config.resolveURLV3(String.format("system/services/%s/live-info", serviceName));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    String responseBody = new String(response.getResponseBody());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(new SystemServiceId(serviceName));
    }
    return GSON.fromJson(responseBody, SystemServiceLiveInfo.class);
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) HttpResponse(io.cdap.common.http.HttpResponse) NotFoundException(io.cdap.cdap.common.NotFoundException) URL(java.net.URL)

Example 2 with SystemServiceId

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

the class MonitorClient method setSystemServiceInstances.

/**
 * Sets the number of instances the system service is running on.
 *
 * @param serviceName name of the system service
 * @param instances 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 UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public void setSystemServiceInstances(String serviceName, int instances) throws IOException, NotFoundException, BadRequestException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveURL(String.format("system/services/%s/instances", serviceName));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(new SystemServiceId(serviceName));
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(new String(response.getResponseBody()));
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) Instances(io.cdap.cdap.proto.Instances) SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) HttpResponse(io.cdap.common.http.HttpResponse) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) URL(java.net.URL)

Example 3 with SystemServiceId

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

the class MonitorHandlerAuthorizationTest method testGetSystemServiceLiveInfoAuthorization.

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

Example 4 with SystemServiceId

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

the class MonitorHandlerAuthorizationTest method testGetLatestRestartServiceInstanceStatusAuthorization.

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

Example 5 with SystemServiceId

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

the class MonitorHandlerAuthorizationTest method testSetServiceInstanceAuthorization.

@Test
public void testSetServiceInstanceAuthorization() throws Exception {
    SystemServiceId systemServiceId = new SystemServiceId(SERVICE_NAME);
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(systemServiceId), Arrays.asList(StandardPermission.UPDATE));
    Instances instances = new Instances(1);
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "system/services/service/instances", Unpooled.copiedBuffer(GSON.toJson(instances), StandardCharsets.UTF_8));
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.setServiceInstance(request, responder, SERVICE_NAME);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.setServiceInstance(request, responder, SERVICE_NAME);
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) Instances(io.cdap.cdap.proto.Instances) HttpResponder(io.cdap.http.HttpResponder) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Aggregations

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