use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method getRunCounts.
/**
* Returns the run counts for all program runnables that are passed into the data. The data is an array of
* Json objects where each object must contain the following three elements: appId, programType, and programId.
* The max number of programs in the request is 100.
* <p>
* Example input:
* <pre><code>
* [{"appId": "App1", "programType": "Service", "programId": "Service1"},
* {"appId": "App1", "programType": "Workflow", "programId": "testWorkflow"},
* {"appId": "App2", "programType": "Workflow", "programId": "DataPipelineWorkflow"}]
* </code></pre>
* </p><p>
* </p><p>
* The response will be an array of JsonObjects each of which will contain the three input parameters
* as well as 2 fields, "runCount" which maps to the count of the program and "statusCode" which maps to the
* status code for the data in that JsonObjects.
* </p><p>
* If an error occurs in the input (for the example above, workflow in app1 does not exist),
* then all JsonObjects for which the parameters have a valid status will have the count field but all JsonObjects
* for which the parameters do not have a valid status will have an error message and statusCode.
* </p><p>
* For example, if there is no workflow in App1 in the data above, then the response would be 200 OK with following
* possible data:
* </p>
* <pre><code>
* [{"appId": "App1", "programType": "Service", "programId": "Service1",
* "statusCode": 200, "runCount": 20},
* {"appId": "App1", "programType": "Workflow", "programId": "testWorkflow", "statusCode": 404,
* "error": "Program 'testWorkflow' is not found"},
* {"appId": "App2", "programType": "Workflow", "programId": "DataPipelineWorkflow",
* "statusCode": 200, "runCount": 300}]
* </code></pre>
*/
@POST
@Path("/runcount")
public void getRunCounts(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
List<BatchProgram> programs = validateAndGetBatchInput(request, BATCH_PROGRAMS_TYPE);
if (programs.size() > 100) {
throw new BadRequestException(String.format("%d programs found in the request, the maximum number " + "supported is 100", programs.size()));
}
List<ProgramId> programIds = programs.stream().map(batchProgram -> new ProgramId(namespaceId, batchProgram.getAppId(), batchProgram.getProgramType(), batchProgram.getProgramId())).collect(Collectors.toList());
List<BatchProgramCount> counts = new ArrayList<>(programs.size());
for (RunCountResult runCountResult : lifecycleService.getProgramRunCounts(programIds)) {
ProgramId programId = runCountResult.getProgramId();
Exception exception = runCountResult.getException();
if (exception == null) {
counts.add(new BatchProgramCount(programId, HttpResponseStatus.OK.code(), null, runCountResult.getCount()));
} else if (exception instanceof NotFoundException) {
counts.add(new BatchProgramCount(programId, HttpResponseStatus.NOT_FOUND.code(), exception.getMessage(), null));
} else if (exception instanceof UnauthorizedException) {
counts.add(new BatchProgramCount(programId, HttpResponseStatus.FORBIDDEN.code(), exception.getMessage(), null));
} else {
counts.add(new BatchProgramCount(programId, HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), exception.getMessage(), null));
}
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(counts));
}
use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method getNextScheduledRunTime.
/**
* Returns next scheduled runtime of a workflow.
*/
@GET
@Path("/apps/{app-name}/{program-type}/{program-name}/nextruntime")
public void getNextScheduledRunTime(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appId, @PathParam("program-type") String type, @PathParam("program-name") String program) throws Exception {
ProgramType programType = getProgramType(type);
handleScheduleRunTime(responder, new NamespaceId(namespaceId).app(appId).program(programType, program), false);
}
use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class PreviewHttpHandler method start.
@POST
@Path("/previews")
public void start(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
NamespaceId namespace = new NamespaceId(namespaceId);
try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
AppRequest<?> appRequest = GSON.fromJson(reader, AppRequest.class);
responder.sendString(HttpResponseStatus.OK, GSON.toJson(previewManager.start(namespace, appRequest)));
} catch (JsonSyntaxException e) {
throw new BadRequestException("Request body is invalid json: " + e.getMessage());
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage());
}
}
use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class PreviewHttpHandler method stop.
@POST
@Path("/previews/{preview-id}/stop")
public void stop(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("preview-id") String previewId) throws Exception {
NamespaceId namespace = new NamespaceId(namespaceId);
ApplicationId application = namespace.app(previewId);
previewManager.stopPreview(application);
responder.sendStatus(HttpResponseStatus.OK);
}
use of io.cdap.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class PreferencesHttpHandler method getNamespacePrefs.
// Namespace Level Properties
// Resolved field, if set to true, returns the collapsed property map (Instance < Namespace)
@Path("/namespaces/{namespace-id}/preferences")
@GET
public void getNamespacePrefs(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @QueryParam("resolved") boolean resolved) throws Exception {
NamespaceId namespaceId = new NamespaceId(namespace);
accessEnforcer.enforce(namespaceId, authenticationContext.getPrincipal(), StandardPermission.GET);
if (nsStore.get(namespaceId) == null) {
responder.sendString(HttpResponseStatus.NOT_FOUND, String.format("Namespace %s not present", namespace));
} else {
if (resolved) {
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(preferencesService.getResolvedProperties(namespaceId)));
} else {
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(preferencesService.getProperties(namespaceId)));
}
}
}
Aggregations