Search in sources :

Example 1 with ServiceNotEnabledException

use of co.cask.cdap.common.ServiceNotEnabledException in project cdap by caskdata.

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(co.cask.cdap.proto.id.SystemServiceId) ServiceNotEnabledException(co.cask.cdap.common.ServiceNotEnabledException) HttpResponse(co.cask.common.http.HttpResponse) NotFoundException(co.cask.cdap.common.NotFoundException) URL(java.net.URL)

Aggregations

NotFoundException (co.cask.cdap.common.NotFoundException)1 ServiceNotEnabledException (co.cask.cdap.common.ServiceNotEnabledException)1 SystemServiceId (co.cask.cdap.proto.id.SystemServiceId)1 HttpResponse (co.cask.common.http.HttpResponse)1 URL (java.net.URL)1