Search in sources :

Example 16 with ApplicationNotFoundException

use of io.cdap.cdap.common.ApplicationNotFoundException in project cdap by cdapio.

the class ApplicationClient method addSchedule.

/**
 * Add a schedule to an application.
 *
 * @param app the application
 * @param scheduleDetail the schedule to be added
 */
public void addSchedule(ApplicationId app, ScheduleDetail scheduleDetail) throws ApplicationNotFoundException, IOException, UnauthenticatedException, UnauthorizedException, BadRequestException {
    String path = String.format("apps/%s/versions/%s/schedules/%s", app.getApplication(), app.getVersion(), scheduleDetail.getName());
    HttpResponse response = restClient.execute(HttpMethod.PUT, config.resolveNamespacedURLV3(app.getParent(), path), GSON.toJson(scheduleDetail), ImmutableMap.<String, String>of(), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ApplicationNotFoundException(app);
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(String.format("Bad Request. Reason: %s", response.getResponseBodyAsString()));
    }
}
Also used : ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException)

Example 17 with ApplicationNotFoundException

use of io.cdap.cdap.common.ApplicationNotFoundException in project cdap by cdapio.

the class ApplicationClient method listPrograms.

/**
 * Lists all programs belonging to an application.
 *
 * @param app the application
 * @return List of all {@link ProgramRecord}s
 * @throws ApplicationNotFoundException if the application with the given ID was not found
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public List<ProgramRecord> listPrograms(ApplicationId app) throws ApplicationNotFoundException, IOException, UnauthenticatedException, UnauthorizedException {
    String path = String.format("apps/%s/versions/%s", app.getApplication(), app.getVersion());
    URL url = config.resolveNamespacedURLV3(app.getParent(), path);
    HttpRequest request = HttpRequest.get(url).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ApplicationNotFoundException(app);
    }
    return ObjectResponse.fromJsonBody(response, ApplicationDetail.class).getResponseObject().getPrograms();
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 18 with ApplicationNotFoundException

use of io.cdap.cdap.common.ApplicationNotFoundException in project cdap by caskdata.

the class ApplicationLifecycleService method getPlugins.

/**
 * Get detail about the plugin in the specified application
 *
 * @param appId the id of the application
 * @return list of plugins in the application
 * @throws ApplicationNotFoundException if the specified application does not exist
 */
public List<PluginInstanceDetail> getPlugins(ApplicationId appId) throws ApplicationNotFoundException {
    ApplicationSpecification appSpec = store.getApplication(appId);
    if (appSpec == null) {
        throw new ApplicationNotFoundException(appId);
    }
    List<PluginInstanceDetail> pluginInstanceDetails = new ArrayList<>();
    for (Map.Entry<String, Plugin> entry : appSpec.getPlugins().entrySet()) {
        pluginInstanceDetails.add(new PluginInstanceDetail(entry.getKey(), entry.getValue()));
    }
    return pluginInstanceDetails;
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) ArrayList(java.util.ArrayList) PluginInstanceDetail(io.cdap.cdap.proto.PluginInstanceDetail) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Plugin(io.cdap.cdap.api.plugin.Plugin)

Example 19 with ApplicationNotFoundException

use of io.cdap.cdap.common.ApplicationNotFoundException in project cdap by caskdata.

the class ApplicationLifecycleService method updateApp.

/**
 * Update an existing application. An application's configuration and artifact version can be updated.
 *
 * @param appId the id of the application to update
 * @param appRequest the request to update the application, including new config and artifact
 * @param programTerminator a program terminator that will stop programs that are removed when updating an app.
 *                          For example, if an update removes a flow, the terminator defines how to stop that flow.
 * @return information about the deployed application
 * @throws ApplicationNotFoundException if the specified application does not exist
 * @throws ArtifactNotFoundException if the requested artifact does not exist
 * @throws InvalidArtifactException if the specified artifact is invalid. For example, if the artifact name changed,
 *                                  if the version is an invalid version, or the artifact contains no app classes
 * @throws Exception if there was an exception during the deployment pipeline. This exception will often wrap
 *                   the actual exception
 */
public ApplicationWithPrograms updateApp(ApplicationId appId, AppRequest appRequest, ProgramTerminator programTerminator) throws Exception {
    // Check if the current user has admin privileges on it before updating.
    accessEnforcer.enforce(appId, authenticationContext.getPrincipal(), StandardPermission.UPDATE);
    // check that app exists
    ApplicationSpecification currentSpec = store.getApplication(appId);
    if (currentSpec == null) {
        throw new ApplicationNotFoundException(appId);
    }
    ArtifactId currentArtifact = currentSpec.getArtifactId();
    // if no artifact is given, use the current one.
    ArtifactId newArtifactId = currentArtifact;
    // otherwise, check requested artifact is valid and use it
    ArtifactSummary requestedArtifact = appRequest.getArtifact();
    if (requestedArtifact != null) {
        // cannot change artifact name, only artifact version.
        if (!currentArtifact.getName().equals(requestedArtifact.getName())) {
            throw new InvalidArtifactException(String.format(" Only artifact version updates are allowed. Cannot change from artifact '%s' to '%s'.", currentArtifact.getName(), requestedArtifact.getName()));
        }
        if (!currentArtifact.getScope().equals(requestedArtifact.getScope())) {
            throw new InvalidArtifactException("Only artifact version updates are allowed. " + "Cannot change from a non-system artifact to a system artifact or vice versa.");
        }
        // check requested artifact version is valid
        ArtifactVersion requestedVersion = new ArtifactVersion(requestedArtifact.getVersion());
        if (requestedVersion.getVersion() == null) {
            throw new InvalidArtifactException(String.format("Requested artifact version '%s' is invalid", requestedArtifact.getVersion()));
        }
        newArtifactId = new ArtifactId(currentArtifact.getName(), requestedVersion, currentArtifact.getScope());
    }
    // ownerAdmin.getImpersonationPrincipal will give the owner which will be impersonated for the application
    // irrespective of the version
    SecurityUtil.verifyOwnerPrincipal(appId, appRequest.getOwnerPrincipal(), ownerAdmin);
    Object requestedConfigObj = appRequest.getConfig();
    // if config is null, use the previous config. Shouldn't use a static GSON since the request Config object can
    // be a user class, otherwise there will be ClassLoader leakage.
    String requestedConfigStr = requestedConfigObj == null ? currentSpec.getConfiguration() : new Gson().toJson(requestedConfigObj);
    Id.Artifact artifactId = Id.Artifact.fromEntityId(Artifacts.toProtoArtifactId(appId.getParent(), newArtifactId));
    return deployApp(appId.getParent(), appId.getApplication(), null, artifactId, requestedConfigStr, programTerminator, ownerAdmin.getOwner(appId), appRequest.canUpdateSchedules());
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) Gson(com.google.gson.Gson) Id(io.cdap.cdap.common.id.Id) InstanceId(io.cdap.cdap.proto.id.InstanceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) EntityId(io.cdap.cdap.proto.id.EntityId) ProgramId(io.cdap.cdap.proto.id.ProgramId) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException)

Example 20 with ApplicationNotFoundException

use of io.cdap.cdap.common.ApplicationNotFoundException in project cdap by caskdata.

the class ApplicationClient method get.

/**
 * Get details about the specified application.
 *
 * @param appId the id of the application to get
 * @return details about the specified application
 * @throws ApplicationNotFoundException if the application with the given ID was not found
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public ApplicationDetail get(ApplicationId appId) throws ApplicationNotFoundException, IOException, UnauthenticatedException, UnauthorizedException {
    String path = String.format("apps/%s/versions/%s", appId.getApplication(), appId.getVersion());
    HttpResponse response = restClient.execute(HttpMethod.GET, config.resolveNamespacedURLV3(appId.getParent(), path), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ApplicationNotFoundException(appId);
    }
    return ObjectResponse.fromJsonBody(response, ApplicationDetail.class).getResponseObject();
}
Also used : ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse)

Aggregations

ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)34 HttpResponse (io.cdap.common.http.HttpResponse)16 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)12 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)10 ProgramNotFoundException (io.cdap.cdap.common.ProgramNotFoundException)8 NotFoundException (io.cdap.cdap.common.NotFoundException)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6 ProgramId (io.cdap.cdap.proto.id.ProgramId)6 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)6 HttpRequest (io.cdap.common.http.HttpRequest)6 URL (java.net.URL)6 TypeToken (com.google.common.reflect.TypeToken)4 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)4 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)4 InstanceNotFoundException (io.cdap.cdap.api.dataset.InstanceNotFoundException)4 WorkflowSpecification (io.cdap.cdap.api.workflow.WorkflowSpecification)4 BadRequestException (io.cdap.cdap.common.BadRequestException)4 ApplicationDetail (io.cdap.cdap.proto.ApplicationDetail)4 HashMap (java.util.HashMap)4 GET (javax.ws.rs.GET)4