Search in sources :

Example 6 with SystemServiceId

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

the class MonitorHandler method getServiceInstance.

/**
 * Returns the number of instances of CDAP Services
 */
@Path("/system/services/{service-name}/instances")
@GET
public void getServiceInstance(HttpRequest request, HttpResponder responder, @PathParam("service-name") String serviceName) throws Exception {
    JsonObject reply = new JsonObject();
    if (!serviceManagementMap.containsKey(serviceName)) {
        throw new NotFoundException(String.format("Invalid service name %s", serviceName));
    }
    SystemServiceId systemServiceId = new SystemServiceId(serviceName);
    contextAccessEnforcer.enforce(systemServiceId, StandardPermission.GET);
    MasterServiceManager serviceManager = serviceManagementMap.get(serviceName);
    if (serviceManager.isServiceEnabled()) {
        int actualInstance = serviceManagementMap.get(serviceName).getInstances();
        reply.addProperty("provisioned", actualInstance);
        reply.addProperty("requested", getSystemServiceInstanceCount(serviceName));
        responder.sendJson(HttpResponseStatus.OK, reply.toString());
    } else {
        throw new ForbiddenException(String.format("Service %s is not enabled", serviceName));
    }
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) ForbiddenException(io.cdap.cdap.common.ForbiddenException) MasterServiceManager(io.cdap.cdap.common.twill.MasterServiceManager) JsonObject(com.google.gson.JsonObject) NotFoundException(io.cdap.cdap.common.NotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 7 with SystemServiceId

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

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)

Example 8 with SystemServiceId

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

the class AuditMessageTest method testAccessMessage.

@Test
public void testAccessMessage() throws Exception {
    String workerAccessJson = "{\"version\":2,\"time\":2000,\"metadataEntity\":{\"details\":{\"namespace\":\"ns1\",\"dataset\":\"ds1\"}," + "\"type\":\"dataset\"},\"user\":\"user1\",\"type\":\"ACCESS\",\"payload\":{\"accessType\":\"WRITE\"," + "\"accessor\":{\"namespace\":\"ns1\",\"application\":\"app1\",\"version\":\"v1\",\"type\":\"Worker\"," + "\"program\":\"worker1\",\"run\":\"run1\",\"entity\":\"PROGRAM_RUN\"}}}";
    AuditMessage workerAccess = new AuditMessage(2000L, new NamespaceId("ns1").dataset("ds1"), "user1", AuditType.ACCESS, new AccessPayload(AccessType.WRITE, new NamespaceId("ns1").app("app1", "v1").worker("worker1").run("run1")));
    Assert.assertEquals(jsonToMap(workerAccessJson), jsonToMap(GSON.toJson(workerAccess)));
    Assert.assertEquals(workerAccess, GSON.fromJson(workerAccessJson, AuditMessage.class));
    String exploreAccessJson = "{\"version\":2,\"time\":2500,\"metadataEntity\":{\"details\":{\"namespace\":\"ns1\",\"dataset\":\"ds1\"}," + "\"type\":\"dataset\"},\"user\":\"user1\",\"type\":\"ACCESS\",\"payload\":{\"accessType\":\"UNKNOWN\"," + "\"accessor\":{\"service\":\"explore\",\"entity\":\"SYSTEM_SERVICE\"}}}";
    AuditMessage exploreAccess = new AuditMessage(2500L, new NamespaceId("ns1").dataset("ds1"), "user1", AuditType.ACCESS, new AccessPayload(AccessType.UNKNOWN, new SystemServiceId("explore")));
    Assert.assertEquals(jsonToMap(exploreAccessJson), jsonToMap(GSON.toJson(exploreAccess)));
    Assert.assertEquals(exploreAccess, GSON.fromJson(exploreAccessJson, AuditMessage.class));
}
Also used : SystemServiceId(io.cdap.cdap.proto.id.SystemServiceId) AccessPayload(io.cdap.cdap.proto.audit.payload.access.AccessPayload) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Test(org.junit.Test)

Example 9 with SystemServiceId

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

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 10 with SystemServiceId

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

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)

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