Search in sources :

Example 1 with CannotBeDeletedException

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

the class ApplicationLifecycleService method removeAll.

/**
 * Remove all the applications inside the given {@link Id.Namespace}
 *
 * @param namespaceId the {@link NamespaceId} under which all application should be deleted
 * @throws Exception
 */
public void removeAll(final NamespaceId namespaceId) throws Exception {
    Map<ProgramRunId, RunRecordMeta> runningPrograms = store.getActiveRuns(namespaceId);
    List<ApplicationSpecification> allSpecs = new ArrayList<>(store.getAllApplications(namespaceId));
    Map<ApplicationId, ApplicationSpecification> apps = new HashMap<>();
    for (ApplicationSpecification appSpec : allSpecs) {
        ApplicationId applicationId = namespaceId.app(appSpec.getName(), appSpec.getAppVersion());
        authorizationEnforcer.enforce(applicationId, authenticationContext.getPrincipal(), Action.ADMIN);
        apps.put(applicationId, appSpec);
    }
    if (!runningPrograms.isEmpty()) {
        Set<String> activePrograms = new HashSet<>();
        for (Map.Entry<ProgramRunId, RunRecordMeta> runningProgram : runningPrograms.entrySet()) {
            activePrograms.add(runningProgram.getKey().getApplication() + ": " + runningProgram.getKey().getProgram());
        }
        String appAllRunningPrograms = Joiner.on(',').join(activePrograms);
        throw new CannotBeDeletedException(namespaceId, "The following programs are still running: " + appAllRunningPrograms);
    }
    // All Apps are STOPPED, delete them
    for (ApplicationId appId : apps.keySet()) {
        removeAppInternal(appId, apps.get(appId));
    }
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) HashMap(java.util.HashMap) RunRecordMeta(co.cask.cdap.internal.app.store.RunRecordMeta) ArrayList(java.util.ArrayList) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Map(java.util.Map) HashMap(java.util.HashMap) CannotBeDeletedException(co.cask.cdap.common.CannotBeDeletedException) HashSet(java.util.HashSet)

Aggregations

ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)1 CannotBeDeletedException (co.cask.cdap.common.CannotBeDeletedException)1 RunRecordMeta (co.cask.cdap.internal.app.store.RunRecordMeta)1 ApplicationId (co.cask.cdap.proto.id.ApplicationId)1 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1