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