Search in sources :

Example 11 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 12 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)

Example 13 with ApplicationNotFoundException

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

the class ApplicationClient method enableSchedule.

/**
 * Enable a schedule in an application.
 *
 * @param scheduleId the id of the schedule to be enabled
 */
public void enableSchedule(ScheduleId scheduleId) throws ApplicationNotFoundException, IOException, UnauthenticatedException, UnauthorizedException, BadRequestException {
    String path = String.format("apps/%s/versions/%s/program-type/schedules/program-id/%s/action/enable", scheduleId.getApplication(), scheduleId.getVersion(), scheduleId.getSchedule());
    HttpResponse response = restClient.execute(HttpMethod.PUT, config.resolveNamespacedURLV3(scheduleId.getParent().getParent(), path), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ApplicationNotFoundException(scheduleId.getParent());
    } 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 14 with ApplicationNotFoundException

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

the class ApplicationClient method upgradeApplication.

/**
 * Upgrades an application.
 *
 * @param app the application to delete
 * @param artifactScopes artifact scopes to consider while searching for latest artifacts.
 * @param allowSnapshot should consider snapshot artifacts or not.
 * @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 void upgradeApplication(ApplicationId app, Set<String> artifactScopes, boolean allowSnapshot) throws ApplicationNotFoundException, IOException, UnauthenticatedException, UnauthorizedException {
    String path = String.format("apps/%s/upgrade?allowSnapshot=%s", app.getApplication(), Boolean.toString(allowSnapshot));
    if (!artifactScopes.isEmpty()) {
        for (String scope : artifactScopes) {
            path = path + String.format("&artifactScope=%s", scope);
        }
    }
    // Required to add body even if runtimeArgs is null to avoid 411 error for Http POST
    HttpRequest.Builder request = HttpRequest.post(config.resolveNamespacedURLV3(app.getParent(), path)).withBody("");
    HttpResponse response = restClient.execute(request.build(), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ApplicationNotFoundException(app);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse)

Example 15 with ApplicationNotFoundException

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

the class ApplicationClient method delete.

/**
 * Deletes an application.
 *
 * @param app the application to delete
 * @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 void delete(ApplicationId app) throws ApplicationNotFoundException, IOException, AccessException {
    String path = String.format("apps/%s/versions/%s", app.getApplication(), app.getVersion());
    HttpResponse response = restClient.execute(HttpMethod.DELETE, config.resolveNamespacedURLV3(app.getParent(), path), config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ApplicationNotFoundException(app);
    }
}
Also used : ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) HttpResponse(io.cdap.common.http.HttpResponse)

Aggregations

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