package io.cattle.platform.process.account;

import io.cattle.platform.core.model.Account;
import io.cattle.platform.core.model.Credential;
import io.cattle.platform.core.model.Host;
import io.cattle.platform.engine.handler.HandlerResult;
import io.cattle.platform.engine.process.ProcessInstance;
import io.cattle.platform.engine.process.ProcessState;
import io.cattle.platform.engine.process.impl.ProcessCancelException;
import io.cattle.platform.process.base.AbstractDefaultProcessHandler;

import javax.inject.Named;

@Named
public class AccountPurge extends AbstractDefaultProcessHandler {

    @Override
    public HandlerResult handle(ProcessState state, ProcessInstance process) {
        Account account = (Account) state.getResource();

        for (Credential cred : getObjectManager().children(account, Credential.class)) {
            try {
                deactivateThenRemove(cred, state.getData());
            } catch (ProcessCancelException e) {
                // ignore
            }
        }

        for (Host host : getObjectManager().children(account, Host.class)) {
            try {
                deactivateThenRemove(host, state.getData());
            } catch (ProcessCancelException e) {
                // ignore
            }
        }

        return null;
    }

}

@@ -73,19 +73,123 @@
 ore.
-model.Accou
+constants.InstanceConstants;%0Aimport io.cattle.platform.core.model.Account;%0Aimport io.cattle.platform.core.model.Age
 nt;%0A
@@ -280,16 +280,63 @@
 l.Host;%0A
+import io.cattle.platform.core.model.Instance;%0A
 import i
@@ -566,16 +566,67 @@
 eption;%0A
+import io.cattle.platform.object.util.ObjectUtils;%0A
 import i
@@ -688,16 +688,38 @@
 ndler;%0A%0A
+import java.util.Map;%0A
 import j
@@ -1068,163 +1068,494 @@
     
-try %7B%0A                deactivateThenRemove(cred, state.getData());%0A            %7D catch (ProcessCancelException e) %7B%0A                // ignore%0A            %7D
+if (cred.getRemoved() != null) %7B%0A                continue;%0A            %7D%0A%0A            deactivateThenRemove(cred, state.getData());%0A        %7D%0A%0A        for (Host host : getObjectManager().children(account, Host.class)) %7B%0A            try %7B%0A                deactivateThenRemove(host, state.getData());%0A            %7D catch (ProcessCancelException e) %7B%0A                // ignore%0A            %7D%0A            purge(host, null);%0A%0A            deleteAgentAccount(host.getAgentId(), state.getData());
 %0A   
@@ -1571,32 +1571,34 @@
        for (
-Host hos
+Agent agen
 t : getObjec
@@ -1618,35 +1618,36 @@
 ildren(account, 
-Hos
+Agen
 t.class)) %7B%0A    
@@ -1658,194 +1658,1234 @@
     
-try %7B%0A                deactivateThenRemove(host, state.getData());%0A            %7D catch (ProcessCancelException e) %7B%0A                // ignore%0A            %7D%0A        %7D%0A%0A        return null
+if (agent.getRemoved() != null) %7B%0A                continue;%0A            %7D%0A            deactivateThenRemove(agent, state.getData());%0A        %7D%0A%0A        for (Instance instance : getObjectManager().children(account, Instance.class)) %7B%0A            deleteAgentAccount(instance.getAgentId(), state.getData());%0A%0A            if (instance.getRemoved() != null) %7B%0A                continue;%0A            %7D%0A%0A            try %7B%0A                objectProcessManager.executeProcess(InstanceConstants.PROCESS_STOP, instance, null);%0A            %7D catch (ProcessCancelException e) %7B%0A                // ignore%0A            %7D%0A%0A            remove(instance, null);%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A    protected void deleteAgentAccount(Long agentId, Map%3CString, Object%3E data) %7B%0A        if (agentId == null) %7B%0A            return;%0A        %7D%0A%0A        Agent agent = getObjectManager().loadResource(Agent.class, agentId);%0A        Account account  = getObjectManager().loadResource(Account.class, agent.getAccountId());%0A        if (account == null) %7B%0A            return;%0A        %7D%0A%0A        try %7B%0A            deactivateThenRemove(account, data);%0A        %7D catch (ProcessCancelException e) %7B%0A            // ignore%0A        %7D%0A%0A        purge(account, data)
 ;%0A  

Change account purge logic