Search in sources :

Example 1 with CannotBeDeletedException

use of io.cdap.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(NamespaceId namespaceId) throws Exception {
    Map<ProgramRunId, RunRecordDetail> 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());
        accessEnforcer.enforce(applicationId, authenticationContext.getPrincipal(), StandardPermission.DELETE);
        apps.put(applicationId, appSpec);
    }
    if (!runningPrograms.isEmpty()) {
        Set<String> activePrograms = new HashSet<>();
        for (Map.Entry<ProgramRunId, RunRecordDetail> 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(io.cdap.cdap.api.app.ApplicationSpecification) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RunRecordDetail(io.cdap.cdap.internal.app.store.RunRecordDetail) ArrayList(java.util.ArrayList) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) CannotBeDeletedException(io.cdap.cdap.common.CannotBeDeletedException) HashSet(java.util.HashSet)

Aggregations

ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)1 CannotBeDeletedException (io.cdap.cdap.common.CannotBeDeletedException)1 RunRecordDetail (io.cdap.cdap.internal.app.store.RunRecordDetail)1 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)1 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1