Search in sources :

Example 26 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method saveProgramRuntimeArgs.

/**
 * Save runtime args of program with app version.
 */
@PUT
@Path("/apps/{app-name}/versions/{app-version}/{program-type}/{program-name}/runtimeargs")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void saveProgramRuntimeArgs(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("app-version") String appVersion, @PathParam("program-type") String type, @PathParam("program-name") String programName) throws Exception {
    ProgramType programType = getProgramType(type);
    ProgramId programId = new ApplicationId(namespaceId, appName, appVersion).program(programType, programName);
    saveProgramIdRuntimeArgs(programId, request, responder);
}
Also used : ProgramType(co.cask.cdap.proto.ProgramType) ProgramId(co.cask.cdap.proto.id.ProgramId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 27 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method getProgramIdRuntimeArgs.

private void getProgramIdRuntimeArgs(ProgramId programId, HttpResponder responder) throws Exception {
    ProgramType programType = programId.getType();
    if (programType == null || programType == ProgramType.WEBAPP) {
        throw new NotFoundException(String.format("Getting program runtime arguments is not supported for program " + "type '%s'.", programType));
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(lifecycleService.getRuntimeArgs(programId)));
}
Also used : NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramType(co.cask.cdap.proto.ProgramType)

Example 28 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method doPerformAction.

private void doPerformAction(FullHttpRequest request, HttpResponder responder, String namespaceId, String appId, String appVersion, String type, String programId, String action) throws Exception {
    ApplicationId applicationId = new ApplicationId(namespaceId, appId, appVersion);
    if (SCHEDULES.equals(type)) {
        ScheduleId scheduleId = applicationId.schedule(programId);
        if (action.equals("disable") || action.equals("suspend")) {
            programScheduleService.suspend(scheduleId);
        } else if (action.equals("enable") || action.equals("resume")) {
            programScheduleService.resume(scheduleId);
        } else {
            throw new BadRequestException("Action for schedules may only be 'enable', 'disable', 'suspend', or 'resume' but is'" + action + "'");
        }
        responder.sendJson(HttpResponseStatus.OK, "OK");
        return;
    }
    ProgramType programType;
    try {
        programType = ProgramType.valueOfCategoryName(type);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(String.format("Unknown program type '%s'", type), e);
    }
    ProgramId program = applicationId.program(programType, programId);
    Map<String, String> args = decodeArguments(request);
    // we have already validated that the action is valid
    switch(action.toLowerCase()) {
        case "start":
            lifecycleService.start(program, args, false);
            break;
        case "debug":
            if (!isDebugAllowed(programType)) {
                throw new NotImplementedException(String.format("debug action is not implemented for program type %s", programType));
            }
            lifecycleService.start(program, args, true);
            break;
        case "stop":
            lifecycleService.stop(program);
            break;
        default:
            throw new NotFoundException(String.format("%s action was not found", action));
    }
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : NotImplementedException(co.cask.cdap.common.NotImplementedException) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramType(co.cask.cdap.proto.ProgramType) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ScheduleId(co.cask.cdap.proto.id.ScheduleId) ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 29 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method getStatus.

/**
 * Returns status of a type specified by the type{flows,workflows,mapreduce,spark,services,schedules}.
 */
@GET
@Path("/apps/{app-id}/versions/{version-id}/{program-type}/{program-id}/status")
public void getStatus(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("version-id") String versionId, @PathParam("program-type") String type, @PathParam("program-id") String programId) throws Exception {
    ApplicationId applicationId = new ApplicationId(namespaceId, appId, versionId);
    if (SCHEDULES.equals(type)) {
        JsonObject json = new JsonObject();
        ScheduleId scheduleId = applicationId.schedule(programId);
        ApplicationSpecification appSpec = store.getApplication(applicationId);
        if (appSpec == null) {
            throw new NotFoundException(applicationId);
        }
        json.addProperty("status", programScheduleService.getStatus(scheduleId).toString());
        responder.sendJson(HttpResponseStatus.OK, json.toString());
        return;
    }
    ProgramType programType;
    try {
        programType = ProgramType.valueOfCategoryName(type);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e);
    }
    ProgramId program = applicationId.program(programType, programId);
    ProgramStatus programStatus = lifecycleService.getProgramStatus(program);
    Map<String, String> status = ImmutableMap.of("status", programStatus.name());
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(status));
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) JsonObject(com.google.gson.JsonObject) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) ProgramType(co.cask.cdap.proto.ProgramType) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ScheduleId(co.cask.cdap.proto.id.ScheduleId) ProgramId(co.cask.cdap.proto.id.ProgramId) ProgramStatus(co.cask.cdap.proto.ProgramStatus) BatchProgramStatus(co.cask.cdap.proto.BatchProgramStatus) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 30 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method updateLogLevels.

private void updateLogLevels(FullHttpRequest request, HttpResponder responder, String namespace, String appName, String appVersion, String type, String programName, @Nullable String component, String runId) throws Exception {
    ProgramType programType = getProgramType(type);
    if (programType == null) {
        throw new BadRequestException("Invalid program type provided");
    }
    try {
        // we are decoding the body to Map<String, String> instead of Map<String, LogEntry.Level> here since Gson will
        // serialize invalid enum values to null, which is allowed for log level, instead of throw an Exception.
        lifecycleService.updateProgramLogLevels(new ApplicationId(namespace, appName, appVersion).program(programType, programName), transformLogLevelsMap(decodeArguments(request)), component, runId);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Invalid JSON in body");
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage());
    } catch (SecurityException e) {
        throw new UnauthorizedException("Unauthorized to update the log levels");
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) BadRequestException(co.cask.cdap.common.BadRequestException) ProgramType(co.cask.cdap.proto.ProgramType) ApplicationId(co.cask.cdap.proto.id.ApplicationId)

Aggregations

ProgramType (co.cask.cdap.proto.ProgramType)71 ProgramId (co.cask.cdap.proto.id.ProgramId)33 ApplicationId (co.cask.cdap.proto.id.ApplicationId)22 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)21 Path (javax.ws.rs.Path)16 BadRequestException (co.cask.cdap.common.BadRequestException)13 GET (javax.ws.rs.GET)13 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)12 NotFoundException (co.cask.cdap.common.NotFoundException)12 RunId (org.apache.twill.api.RunId)11 ProgramController (co.cask.cdap.app.runtime.ProgramController)8 ArrayList (java.util.ArrayList)8 ProgramContextAware (co.cask.cdap.data.ProgramContextAware)6 BasicProgramContext (co.cask.cdap.internal.app.runtime.BasicProgramContext)6 FlowSpecification (co.cask.cdap.api.flow.FlowSpecification)5 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)5 MetricsCollectionService (co.cask.cdap.api.metrics.MetricsCollectionService)4 WorkflowSpecification (co.cask.cdap.api.workflow.WorkflowSpecification)4 PluginInstantiator (co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator)4