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");
}
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();
}
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);
}
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));
}
}
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");
}
}
Aggregations