Search in sources :

Example 21 with DedicatedResourceVO

use of com.cloud.dc.DedicatedResourceVO in project cloudstack by apache.

the class DomainManagerImpl method cleanupDomain.

protected boolean cleanupDomain(Long domainId, Long ownerId) throws ConcurrentOperationException, ResourceUnavailableException {
    s_logger.debug("Cleaning up domain id=" + domainId);
    boolean success = true;
    DomainVO domainHandle = _domainDao.findById(domainId);
    {
        domainHandle.setState(Domain.State.Inactive);
        _domainDao.update(domainId, domainHandle);
        SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
        sc.addAnd("parent", SearchCriteria.Op.EQ, domainId);
        List<DomainVO> domains = _domainDao.search(sc, null);
        SearchCriteria<DomainVO> sc1 = _domainDao.createSearchCriteria();
        sc1.addAnd("path", SearchCriteria.Op.LIKE, "%" + "replace(" + domainHandle.getPath() + ", '%', '[%]')" + "%");
        List<DomainVO> domainsToBeInactivated = _domainDao.search(sc1, null);
        // update all subdomains to inactive so no accounts/users can be created
        for (DomainVO domain : domainsToBeInactivated) {
            domain.setState(Domain.State.Inactive);
            _domainDao.update(domain.getId(), domain);
        }
        // cleanup sub-domains first
        for (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
    SearchCriteria<AccountVO> sc = _accountDao.createSearchCriteria();
    sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
    List<AccountVO> accounts = _accountDao.search(sc, null);
    for (AccountVO account : accounts) {
        if (account.getType() != Account.ACCOUNT_TYPE_PROJECT) {
            s_logger.debug("Deleting account " + account + " as a part of domain id=" + domainId + " cleanup");
            boolean deleteAccount = _accountMgr.deleteAccount(account, CallContext.current().getCallingUserId(), getCaller());
            if (!deleteAccount) {
                s_logger.warn("Failed to cleanup account id=" + account.getId() + " as a part of domain cleanup");
            }
            success = (success && deleteAccount);
        } else {
            ProjectVO project = _projectDao.findByProjectAccountId(account.getId());
            s_logger.debug("Deleting project " + project + " as a part of domain id=" + domainId + " cleanup");
            boolean deleteProject = _projectMgr.deleteProject(getCaller(), 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);
    List<Long> networkIds = _networkDomainDao.listNetworkIdsByDomain(domainId);
    CallContext ctx = CallContext.current();
    ReservationContext context = new ReservationContextImpl(null, null, _accountMgr.getActiveUser(ctx.getCallingUserId()), ctx.getCallingAccount());
    for (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
    boolean deleteDomainSuccess = true;
    List<AccountVO> accountsForCleanup = _accountDao.findCleanupsForRemovedAccounts(domainId);
    if (accountsForCleanup.isEmpty()) {
        //release dedication if any, before deleting the domain
        List<DedicatedResourceVO> dedicatedResources = _dedicatedDao.listByDomainId(domainId);
        if (dedicatedResources != null && !dedicatedResources.isEmpty()) {
            s_logger.debug("Releasing dedicated resources for domain" + domainId);
            for (DedicatedResourceVO dr : dedicatedResources) {
                if (!_dedicatedDao.remove(dr.getId())) {
                    s_logger.warn("Fail to release dedicated resources for domain " + domainId);
                    return false;
                }
            }
        }
        //delete domain
        _messageBus.publish(_name, MESSAGE_PRE_REMOVE_DOMAIN_EVENT, PublishScope.LOCAL, domainHandle);
        deleteDomainSuccess = _domainDao.remove(domainId);
        _messageBus.publish(_name, MESSAGE_REMOVE_DOMAIN_EVENT, PublishScope.LOCAL, domainHandle);
        // 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;
}
Also used : CallContext(org.apache.cloudstack.context.CallContext) ReservationContextImpl(com.cloud.vm.ReservationContextImpl) SearchCriteria(com.cloud.utils.db.SearchCriteria) ProjectVO(com.cloud.projects.ProjectVO) ReservationContext(com.cloud.vm.ReservationContext) DomainVO(com.cloud.domain.DomainVO) List(java.util.List) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO)

Example 22 with DedicatedResourceVO

use of com.cloud.dc.DedicatedResourceVO in project cloudstack by apache.

the class UserVmManagerImpl method domainOfDedicatedHost.

private Long domainOfDedicatedHost(HostVO host) {
    long hostId = host.getId();
    DedicatedResourceVO dedicatedHost = _dedicatedDao.findByHostId(hostId);
    DedicatedResourceVO dedicatedClusterOfHost = _dedicatedDao.findByClusterId(host.getClusterId());
    DedicatedResourceVO dedicatedPodOfHost = _dedicatedDao.findByPodId(host.getPodId());
    if (dedicatedHost != null) {
        return dedicatedHost.getDomainId();
    }
    if (dedicatedClusterOfHost != null) {
        return dedicatedClusterOfHost.getDomainId();
    }
    if (dedicatedPodOfHost != null) {
        return dedicatedPodOfHost.getDomainId();
    }
    return null;
}
Also used : DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO)

Example 23 with DedicatedResourceVO

use of com.cloud.dc.DedicatedResourceVO in project cloudstack by apache.

the class DedicatedResourceManagerImpl method dedicatePod.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Pod")
public List<DedicatedResourceVO> dedicatePod(final Long podId, final Long domainId, final String accountName) {
    Long accountId = null;
    if (accountName != null) {
        Account caller = CallContext.current().getCallingAccount();
        Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, null);
        accountId = owner.getId();
    }
    List<Long> childDomainIds = getDomainChildIds(domainId);
    childDomainIds.add(domainId);
    checkAccountAndDomain(accountId, domainId);
    HostPodVO pod = _podDao.findById(podId);
    List<HostVO> hosts = null;
    if (pod == null) {
        throw new InvalidParameterValueException("Unable to find pod by id " + podId);
    } else {
        DedicatedResourceVO dedicatedPod = _dedicatedDao.findByPodId(podId);
        DedicatedResourceVO dedicatedZoneOfPod = _dedicatedDao.findByZoneId(pod.getDataCenterId());
        //check if pod is dedicated
        if (dedicatedPod != null) {
            s_logger.error("Pod " + pod.getName() + " is already dedicated");
            throw new CloudRuntimeException("Pod " + pod.getName() + " is already dedicated");
        }
        if (dedicatedZoneOfPod != null) {
            boolean domainIdInChildreanList = getDomainChildIds(dedicatedZoneOfPod.getDomainId()).contains(domainId);
            //can dedicate a pod to an account/domain if zone is dedicated to parent-domain
            if (dedicatedZoneOfPod.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedZoneOfPod.getDomainId().equals(domainId) || domainIdInChildreanList))) {
                DataCenterVO zone = _zoneDao.findById(pod.getDataCenterId());
                s_logger.error("Cannot dedicate Pod. Its zone is already dedicated");
                throw new CloudRuntimeException("Pod's Zone " + zone.getName() + " is already dedicated");
            }
        }
        //check if any resource under this pod is dedicated to different account or sub-domain
        List<ClusterVO> clusters = _clusterDao.listByPodId(pod.getId());
        List<DedicatedResourceVO> clustersToRelease = new ArrayList<DedicatedResourceVO>();
        List<DedicatedResourceVO> hostsToRelease = new ArrayList<DedicatedResourceVO>();
        for (ClusterVO cluster : clusters) {
            DedicatedResourceVO dCluster = _dedicatedDao.findByClusterId(cluster.getId());
            if (dCluster != null) {
                if (!(childDomainIds.contains(dCluster.getDomainId()))) {
                    throw new CloudRuntimeException("Cluster " + cluster.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
                }
                /*if all dedicated resources belongs to same account and domain then we should release dedication
                    and make new entry for this Pod*/
                if (accountId != null) {
                    if (dCluster.getAccountId().equals(accountId)) {
                        clustersToRelease.add(dCluster);
                    } else {
                        s_logger.error("Cluster " + cluster.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
                        throw new CloudRuntimeException("Cluster " + cluster.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
                    }
                } else {
                    if (dCluster.getAccountId() == null && dCluster.getDomainId().equals(domainId)) {
                        clustersToRelease.add(dCluster);
                    }
                }
            }
        }
        for (DedicatedResourceVO dr : clustersToRelease) {
            releaseDedicatedResource(null, null, dr.getClusterId(), null);
        }
        hosts = _hostDao.findByPodId(pod.getId());
        for (HostVO host : hosts) {
            DedicatedResourceVO dHost = _dedicatedDao.findByHostId(host.getId());
            if (dHost != null) {
                if (!(getDomainChildIds(domainId).contains(dHost.getDomainId()))) {
                    throw new CloudRuntimeException("Host " + host.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
                }
                if (accountId != null) {
                    if (dHost.getAccountId().equals(accountId)) {
                        hostsToRelease.add(dHost);
                    } else {
                        s_logger.error("Host " + host.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
                        throw new CloudRuntimeException("Host " + host.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
                    }
                } else {
                    if (dHost.getAccountId() == null && dHost.getDomainId().equals(domainId)) {
                        hostsToRelease.add(dHost);
                    }
                }
            }
        }
        for (DedicatedResourceVO dr : hostsToRelease) {
            releaseDedicatedResource(null, null, null, dr.getHostId());
        }
    }
    checkHostsSuitabilityForExplicitDedication(accountId, childDomainIds, hosts);
    final Long accountIdFinal = accountId;
    return Transaction.execute(new TransactionCallback<List<DedicatedResourceVO>>() {

        @Override
        public List<DedicatedResourceVO> doInTransaction(TransactionStatus status) {
            // find or create the affinity group by name under this account/domain
            AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountIdFinal);
            if (group == null) {
                s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group");
                throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support.");
            }
            DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, podId, null, null, null, null, group.getId());
            try {
                dedicatedResource.setDomainId(domainId);
                if (accountIdFinal != null) {
                    dedicatedResource.setAccountId(accountIdFinal);
                }
                dedicatedResource = _dedicatedDao.persist(dedicatedResource);
            } catch (Exception e) {
                s_logger.error("Unable to dedicate pod due to " + e.getMessage(), e);
                throw new CloudRuntimeException("Failed to dedicate pod. Please contact Cloud Support.");
            }
            List<DedicatedResourceVO> result = new ArrayList<DedicatedResourceVO>();
            result.add(dedicatedResource);
            return result;
        }
    });
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) ClusterVO(com.cloud.dc.ClusterVO) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) AffinityGroup(org.apache.cloudstack.affinity.AffinityGroup) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) List(java.util.List) ArrayList(java.util.ArrayList) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 24 with DedicatedResourceVO

use of com.cloud.dc.DedicatedResourceVO in project cloudstack by apache.

the class DedicatedResourceManagerImpl method dedicateCluster.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Cluster")
public List<DedicatedResourceVO> dedicateCluster(final Long clusterId, final Long domainId, final String accountName) {
    Long accountId = null;
    List<HostVO> hosts = null;
    if (accountName != null) {
        Account caller = CallContext.current().getCallingAccount();
        Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, null);
        accountId = owner.getId();
    }
    List<Long> childDomainIds = getDomainChildIds(domainId);
    childDomainIds.add(domainId);
    checkAccountAndDomain(accountId, domainId);
    ClusterVO cluster = _clusterDao.findById(clusterId);
    if (cluster == null) {
        throw new InvalidParameterValueException("Unable to find cluster by id " + clusterId);
    } else {
        DedicatedResourceVO dedicatedCluster = _dedicatedDao.findByClusterId(clusterId);
        DedicatedResourceVO dedicatedPodOfCluster = _dedicatedDao.findByPodId(cluster.getPodId());
        DedicatedResourceVO dedicatedZoneOfCluster = _dedicatedDao.findByZoneId(cluster.getDataCenterId());
        //check if cluster is dedicated
        if (dedicatedCluster != null) {
            s_logger.error("Cluster " + cluster.getName() + " is already dedicated");
            throw new CloudRuntimeException("Cluster " + cluster.getName() + " is already dedicated");
        }
        if (dedicatedPodOfCluster != null) {
            boolean domainIdInChildreanList = getDomainChildIds(dedicatedPodOfCluster.getDomainId()).contains(domainId);
            //can dedicate a cluster to an account/domain if pod is dedicated to parent-domain
            if (dedicatedPodOfCluster.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedPodOfCluster.getDomainId().equals(domainId) || domainIdInChildreanList))) {
                s_logger.error("Cannot dedicate Cluster. Its Pod is already dedicated");
                HostPodVO pod = _podDao.findById(cluster.getPodId());
                throw new CloudRuntimeException("Cluster's Pod " + pod.getName() + " is already dedicated");
            }
        }
        if (dedicatedZoneOfCluster != null) {
            boolean domainIdInChildreanList = getDomainChildIds(dedicatedZoneOfCluster.getDomainId()).contains(domainId);
            //can dedicate a cluster to an account/domain if zone is dedicated to parent-domain
            if (dedicatedZoneOfCluster.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedZoneOfCluster.getDomainId().equals(domainId) || domainIdInChildreanList))) {
                s_logger.error("Cannot dedicate Cluster. Its zone is already dedicated");
                DataCenterVO zone = _zoneDao.findById(cluster.getDataCenterId());
                throw new CloudRuntimeException("Cluster's Zone " + zone.getName() + " is already dedicated");
            }
        }
        //check if any resource under this cluster is dedicated to different account or sub-domain
        hosts = _hostDao.findByClusterId(cluster.getId());
        List<DedicatedResourceVO> hostsToRelease = new ArrayList<DedicatedResourceVO>();
        for (HostVO host : hosts) {
            DedicatedResourceVO dHost = _dedicatedDao.findByHostId(host.getId());
            if (dHost != null) {
                if (!(childDomainIds.contains(dHost.getDomainId()))) {
                    throw new CloudRuntimeException("Host " + host.getName() + " under this Cluster " + cluster.getName() + " is dedicated to different account/domain");
                }
                /*if all dedicated resources belongs to same account and domain then we should release dedication
                    and make new entry for this cluster */
                if (accountId != null) {
                    if (dHost.getAccountId().equals(accountId)) {
                        hostsToRelease.add(dHost);
                    } else {
                        s_logger.error("Cannot dedicate Cluster " + cluster.getName() + " to account" + accountName);
                        throw new CloudRuntimeException("Cannot dedicate Cluster " + cluster.getName() + " to account" + accountName);
                    }
                } else {
                    if (dHost.getAccountId() == null && dHost.getDomainId().equals(domainId)) {
                        hostsToRelease.add(dHost);
                    }
                }
            }
        }
        for (DedicatedResourceVO dr : hostsToRelease) {
            releaseDedicatedResource(null, null, null, dr.getHostId());
        }
    }
    checkHostsSuitabilityForExplicitDedication(accountId, childDomainIds, hosts);
    final Long accountIdFinal = accountId;
    return Transaction.execute(new TransactionCallback<List<DedicatedResourceVO>>() {

        @Override
        public List<DedicatedResourceVO> doInTransaction(TransactionStatus status) {
            // find or create the affinity group by name under this account/domain
            AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountIdFinal);
            if (group == null) {
                s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group");
                throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support.");
            }
            DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, null, clusterId, null, null, null, group.getId());
            try {
                dedicatedResource.setDomainId(domainId);
                if (accountIdFinal != null) {
                    dedicatedResource.setAccountId(accountIdFinal);
                }
                dedicatedResource = _dedicatedDao.persist(dedicatedResource);
            } catch (Exception e) {
                s_logger.error("Unable to dedicate host due to " + e.getMessage(), e);
                throw new CloudRuntimeException("Failed to dedicate cluster. Please contact Cloud Support.");
            }
            List<DedicatedResourceVO> result = new ArrayList<DedicatedResourceVO>();
            result.add(dedicatedResource);
            return result;
        }
    });
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) ClusterVO(com.cloud.dc.ClusterVO) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) AffinityGroup(org.apache.cloudstack.affinity.AffinityGroup) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) List(java.util.List) ArrayList(java.util.ArrayList) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 25 with DedicatedResourceVO

use of com.cloud.dc.DedicatedResourceVO in project cloudstack by apache.

the class DedicatedResourceManagerImpl method dedicateHost.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Host")
public List<DedicatedResourceVO> dedicateHost(final Long hostId, final Long domainId, final String accountName) {
    Long accountId = null;
    if (accountName != null) {
        Account caller = CallContext.current().getCallingAccount();
        Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, null);
        accountId = owner.getId();
    }
    checkAccountAndDomain(accountId, domainId);
    HostVO host = _hostDao.findById(hostId);
    if (host == null) {
        throw new InvalidParameterValueException("Unable to find host by id " + hostId);
    } else {
        //check if host is of routing type
        if (host.getType() != Host.Type.Routing) {
            throw new CloudRuntimeException("Invalid host type for host " + host.getName());
        }
        DedicatedResourceVO dedicatedHost = _dedicatedDao.findByHostId(hostId);
        DedicatedResourceVO dedicatedClusterOfHost = _dedicatedDao.findByClusterId(host.getClusterId());
        DedicatedResourceVO dedicatedPodOfHost = _dedicatedDao.findByPodId(host.getPodId());
        DedicatedResourceVO dedicatedZoneOfHost = _dedicatedDao.findByZoneId(host.getDataCenterId());
        if (dedicatedHost != null) {
            s_logger.error("Host " + host.getName() + " is already dedicated");
            throw new CloudRuntimeException("Host " + host.getName() + " is already dedicated");
        }
        if (dedicatedClusterOfHost != null) {
            boolean domainIdInChildreanList = getDomainChildIds(dedicatedClusterOfHost.getDomainId()).contains(domainId);
            //can dedicate a host to an account/domain if cluster is dedicated to parent-domain
            if (dedicatedClusterOfHost.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedClusterOfHost.getDomainId().equals(domainId) || domainIdInChildreanList))) {
                ClusterVO cluster = _clusterDao.findById(host.getClusterId());
                s_logger.error("Host's Cluster " + cluster.getName() + " is already dedicated");
                throw new CloudRuntimeException("Host's Cluster " + cluster.getName() + " is already dedicated");
            }
        }
        if (dedicatedPodOfHost != null) {
            boolean domainIdInChildreanList = getDomainChildIds(dedicatedPodOfHost.getDomainId()).contains(domainId);
            //can dedicate a host to an account/domain if pod is dedicated to parent-domain
            if (dedicatedPodOfHost.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedPodOfHost.getDomainId().equals(domainId) || domainIdInChildreanList))) {
                HostPodVO pod = _podDao.findById(host.getPodId());
                s_logger.error("Host's Pod " + pod.getName() + " is already dedicated");
                throw new CloudRuntimeException("Host's Pod " + pod.getName() + " is already dedicated");
            }
        }
        if (dedicatedZoneOfHost != null) {
            boolean domainIdInChildreanList = getDomainChildIds(dedicatedZoneOfHost.getDomainId()).contains(domainId);
            //can dedicate a host to an account/domain if zone is dedicated to parent-domain
            if (dedicatedZoneOfHost.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedZoneOfHost.getDomainId().equals(domainId) || domainIdInChildreanList))) {
                DataCenterVO zone = _zoneDao.findById(host.getDataCenterId());
                s_logger.error("Host's Data Center " + zone.getName() + " is already dedicated");
                throw new CloudRuntimeException("Host's Data Center " + zone.getName() + " is already dedicated");
            }
        }
    }
    List<Long> childDomainIds = getDomainChildIds(domainId);
    childDomainIds.add(domainId);
    checkHostSuitabilityForExplicitDedication(accountId, childDomainIds, hostId);
    final Long accountIdFinal = accountId;
    return Transaction.execute(new TransactionCallback<List<DedicatedResourceVO>>() {

        @Override
        public List<DedicatedResourceVO> doInTransaction(TransactionStatus status) {
            // find or create the affinity group by name under this account/domain
            AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountIdFinal);
            if (group == null) {
                s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group");
                throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support.");
            }
            DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, null, null, hostId, null, null, group.getId());
            try {
                dedicatedResource.setDomainId(domainId);
                if (accountIdFinal != null) {
                    dedicatedResource.setAccountId(accountIdFinal);
                }
                dedicatedResource = _dedicatedDao.persist(dedicatedResource);
            } catch (Exception e) {
                s_logger.error("Unable to dedicate host due to " + e.getMessage(), e);
                throw new CloudRuntimeException("Failed to dedicate host. Please contact Cloud Support.");
            }
            List<DedicatedResourceVO> result = new ArrayList<DedicatedResourceVO>();
            result.add(dedicatedResource);
            return result;
        }
    });
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) ClusterVO(com.cloud.dc.ClusterVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) AffinityGroup(org.apache.cloudstack.affinity.AffinityGroup) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) List(java.util.List) ArrayList(java.util.ArrayList) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

DedicatedResourceVO (com.cloud.dc.DedicatedResourceVO)29 ArrayList (java.util.ArrayList)15 List (java.util.List)13 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)11 DB (com.cloud.utils.db.DB)10 TransactionStatus (com.cloud.utils.db.TransactionStatus)9 DataCenterVO (com.cloud.dc.DataCenterVO)8 ClusterVO (com.cloud.dc.ClusterVO)7 HostPodVO (com.cloud.dc.HostPodVO)7 HostVO (com.cloud.host.HostVO)7 ActionEvent (com.cloud.event.ActionEvent)6 ConfigurationException (javax.naming.ConfigurationException)6 AffinityGroup (org.apache.cloudstack.affinity.AffinityGroup)5 DedicatedResources (com.cloud.dc.DedicatedResources)4 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)4 ServerApiException (org.apache.cloudstack.api.ServerApiException)4 ListResponse (org.apache.cloudstack.api.response.ListResponse)4 Test (org.junit.Test)4 Account (com.cloud.user.Account)3