use of com.cloud.context.CallContext in project cosmic by MissionCriticalCloud.
the class DomainManagerImpl method cleanupDomain.
private boolean cleanupDomain(final Long domainId, final Long ownerId) throws ConcurrentOperationException, ResourceUnavailableException {
s_logger.debug("Cleaning up domain id=" + domainId);
boolean success = true;
{
final DomainVO domainHandle = _domainDao.findById(domainId);
domainHandle.setState(Domain.State.Inactive);
_domainDao.update(domainId, domainHandle);
final SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
sc.addAnd("parent", SearchCriteria.Op.EQ, domainId);
final List<DomainVO> domains = _domainDao.search(sc, null);
final SearchCriteria<DomainVO> sc1 = _domainDao.createSearchCriteria();
sc1.addAnd("path", SearchCriteria.Op.LIKE, "%" + domainHandle.getPath() + "%");
final List<DomainVO> domainsToBeInactivated = _domainDao.search(sc1, null);
// update all subdomains to inactive so no accounts/users can be created
for (final DomainVO domain : domainsToBeInactivated) {
domain.setState(Domain.State.Inactive);
_domainDao.update(domain.getId(), domain);
}
// cleanup sub-domains first
for (final DomainVO domain : domains) {
success = (success && cleanupDomain(domain.getId(), domain.getAccountId()));
if (!success) {
s_logger.warn("Failed to cleanup domain id=" + domain.getId());
}
}
}
// delete users which will also delete accounts and release resources for those accounts
final SearchCriteria<AccountVO> sc = _accountDao.createSearchCriteria();
sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
final List<AccountVO> accounts = _accountDao.search(sc, null);
for (final AccountVO account : accounts) {
if (account.getType() != Account.ACCOUNT_TYPE_PROJECT) {
s_logger.debug("Deleting account " + account + " as a part of domain id=" + domainId + " cleanup");
final boolean deleteAccount = _accountMgr.deleteAccount(account, CallContext.current().getCallingUserId(), CallContext.current().getCallingAccount());
if (!deleteAccount) {
s_logger.warn("Failed to cleanup account id=" + account.getId() + " as a part of domain cleanup");
}
success = (success && deleteAccount);
} else {
final ProjectVO project = _projectDao.findByProjectAccountId(account.getId());
s_logger.debug("Deleting project " + project + " as a part of domain id=" + domainId + " cleanup");
final boolean deleteProject = _projectMgr.deleteProject(CallContext.current().getCallingAccount(), CallContext.current().getCallingUserId(), project);
if (!deleteProject) {
s_logger.warn("Failed to cleanup project " + project + " as a part of domain cleanup");
}
success = (success && deleteProject);
}
}
// delete the domain shared networks
boolean networksDeleted = true;
s_logger.debug("Deleting networks for domain id=" + domainId);
final List<Long> networkIds = _networkDomainDao.listNetworkIdsByDomain(domainId);
final CallContext ctx = CallContext.current();
final ReservationContext context = new ReservationContextImpl(null, null, _accountMgr.getActiveUser(ctx.getCallingUserId()), ctx.getCallingAccount());
for (final Long networkId : networkIds) {
s_logger.debug("Deleting network id=" + networkId + " as a part of domain id=" + domainId + " cleanup");
if (!_networkMgr.destroyNetwork(networkId, context, false)) {
s_logger.warn("Unable to destroy network id=" + networkId + " as a part of domain id=" + domainId + " cleanup.");
networksDeleted = false;
} else {
s_logger.debug("Network " + networkId + " successfully deleted as a part of domain id=" + domainId + " cleanup.");
}
}
// don't proceed if networks failed to cleanup. The cleanup will be performed for inactive domain once again
if (!networksDeleted) {
s_logger.debug("Failed to delete the shared networks as a part of domain id=" + domainId + " clenaup");
return false;
}
// don't remove the domain if there are accounts required cleanup
final boolean deleteDomainSuccess;
final List<AccountVO> accountsForCleanup = _accountDao.findCleanupsForRemovedAccounts(domainId);
if (accountsForCleanup.isEmpty()) {
// release dedication if any, before deleting the domain
final List<DedicatedResourceVO> dedicatedResources = _dedicatedDao.listByDomainId(domainId);
if (dedicatedResources != null && !dedicatedResources.isEmpty()) {
s_logger.debug("Releasing dedicated resources for domain" + domainId);
for (final DedicatedResourceVO dr : dedicatedResources) {
if (!_dedicatedDao.remove(dr.getId())) {
s_logger.warn("Fail to release dedicated resources for domain " + domainId);
return false;
}
}
}
// delete domain
deleteDomainSuccess = _domainDao.remove(domainId);
// Delete resource count and resource limits entries set for this domain (if there are any).
_resourceCountDao.removeEntriesByOwner(domainId, ResourceOwnerType.Domain);
_resourceLimitDao.removeEntriesByOwner(domainId, ResourceOwnerType.Domain);
} else {
s_logger.debug("Can't delete the domain yet because it has " + accountsForCleanup.size() + "accounts that need a cleanup");
return false;
}
return success && deleteDomainSuccess;
}
use of com.cloud.context.CallContext in project cosmic by MissionCriticalCloud.
the class DestroyRouterCmd method execute.
@Override
public void execute() throws ConcurrentOperationException, ResourceUnavailableException {
final CallContext ctx = CallContext.current();
ctx.setEventDetails("Router Id: " + getId());
final VirtualRouter result = _routerService.destroyRouter(getId(), ctx.getCallingAccount(), ctx.getCallingUserId());
if (result != null) {
final DomainRouterResponse response = _responseGenerator.createDomainRouterResponse(result);
response.setResponseName(getCommandName());
setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to destroy router");
}
}
Aggregations