Search in sources :

Example 26 with HostPodVO

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

the class DedicatedResourceManagerImpl method dedicateZone.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Zone")
public List<DedicatedResourceVO> dedicateZone(final Long zoneId, 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);
    final DataCenterVO dc = _zoneDao.findById(zoneId);
    if (dc == null) {
        throw new InvalidParameterValueException("Unable to find zone by id " + zoneId);
    } else {
        DedicatedResourceVO dedicatedZone = _dedicatedDao.findByZoneId(zoneId);
        //check if zone is dedicated
        if (dedicatedZone != null) {
            s_logger.error("Zone " + dc.getName() + " is already dedicated");
            throw new CloudRuntimeException("Zone  " + dc.getName() + " is already dedicated");
        }
        //check if any resource under this zone is dedicated to different account or sub-domain
        List<HostPodVO> pods = _podDao.listByDataCenterId(dc.getId());
        List<DedicatedResourceVO> podsToRelease = new ArrayList<DedicatedResourceVO>();
        List<DedicatedResourceVO> clustersToRelease = new ArrayList<DedicatedResourceVO>();
        List<DedicatedResourceVO> hostsToRelease = new ArrayList<DedicatedResourceVO>();
        for (HostPodVO pod : pods) {
            DedicatedResourceVO dPod = _dedicatedDao.findByPodId(pod.getId());
            if (dPod != null) {
                if (!(childDomainIds.contains(dPod.getDomainId()))) {
                    throw new CloudRuntimeException("Pod " + pod.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                }
                if (accountId != null) {
                    if (dPod.getAccountId().equals(accountId)) {
                        podsToRelease.add(dPod);
                    } else {
                        s_logger.error("Pod " + pod.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                        throw new CloudRuntimeException("Pod " + pod.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                    }
                } else {
                    if (dPod.getAccountId() == null && dPod.getDomainId().equals(domainId)) {
                        podsToRelease.add(dPod);
                    }
                }
            }
        }
        for (DedicatedResourceVO dr : podsToRelease) {
            releaseDedicatedResource(null, dr.getPodId(), null, null);
        }
        List<ClusterVO> clusters = _clusterDao.listClustersByDcId(dc.getId());
        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 Zone " + dc.getName() + " is dedicated to different account/domain");
                }
                if (accountId != null) {
                    if (dCluster.getAccountId().equals(accountId)) {
                        clustersToRelease.add(dCluster);
                    } else {
                        s_logger.error("Cluster " + cluster.getName() + " under this Zone " + dc.getName() + " is dedicated to different account/domain");
                        throw new CloudRuntimeException("Cluster " + cluster.getName() + " under this Zone " + dc.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.listByDataCenterId(dc.getId());
        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 Zone " + dc.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 Zone " + dc.getName() + " is dedicated to different account/domain");
                        throw new CloudRuntimeException("Host " + host.getName() + " under this Zone " + dc.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(zoneId, null, null, null, null, null, group.getId());
            try {
                dedicatedResource.setDomainId(domainId);
                if (accountIdFinal != null) {
                    dedicatedResource.setAccountId(accountIdFinal);
                }
                dedicatedResource = _dedicatedDao.persist(dedicatedResource);
                // save the domainId in the zone
                dc.setDomainId(domainId);
                if (!_zoneDao.update(zoneId, dc)) {
                    throw new CloudRuntimeException("Failed to dedicate zone, could not set domainId. Please contact Cloud Support.");
                }
            } catch (Exception e) {
                s_logger.error("Unable to dedicate zone due to " + e.getMessage(), e);
                throw new CloudRuntimeException("Failed to dedicate zone. 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 27 with HostPodVO

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

the class ManagementServerImpl method searchForPods.

@Override
public Pair<List<? extends Pod>, Integer> searchForPods(final ListPodsByCmd cmd) {
    final String podName = cmd.getPodName();
    final Long id = cmd.getId();
    Long zoneId = cmd.getZoneId();
    final Object keyword = cmd.getKeyword();
    final Object allocationState = cmd.getAllocationState();
    zoneId = _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(), zoneId);
    final Filter searchFilter = new Filter(HostPodVO.class, "dataCenterId", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final SearchBuilder<HostPodVO> sb = _hostPodDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("allocationState", sb.entity().getAllocationState(), SearchCriteria.Op.EQ);
    final SearchCriteria<HostPodVO> sc = sb.create();
    if (keyword != null) {
        final SearchCriteria<HostPodVO> ssc = _hostPodDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (id != null) {
        sc.setParameters("id", id);
    }
    if (podName != null) {
        sc.setParameters("name", "%" + podName + "%");
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (allocationState != null) {
        sc.setParameters("allocationState", allocationState);
    }
    final Pair<List<HostPodVO>, Integer> result = _hostPodDao.searchAndCount(sc, searchFilter);
    return new Pair<List<? extends Pod>, Integer>(result.first(), result.second());
}
Also used : Filter(com.cloud.utils.db.Filter) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) HostPodVO(com.cloud.dc.HostPodVO) Pair(com.cloud.utils.Pair) SSHKeyPair(com.cloud.user.SSHKeyPair)

Example 28 with HostPodVO

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

the class RouterDeploymentDefinition method findDestinations.

protected List<DeployDestination> findDestinations() {
    // dest has pod=null, for Basic Zone findOrDeployVRs for all Pods
    final List<DeployDestination> destinations = new ArrayList<DeployDestination>();
    // restart scenario otherwise it is a vm deployment scenario
    if (isBasic() && dest.getPod() == null) {
        // Find all pods in the data center with running or starting user vms
        final long dcId = dest.getDataCenter().getId();
        final List<HostPodVO> pods = listByDataCenterIdVMTypeAndStates(dcId, VirtualMachine.Type.User, VirtualMachine.State.Starting, VirtualMachine.State.Running);
        // Loop through all the pods skip those with running or starting VRs
        for (final HostPodVO pod : pods) {
            // Get list of VRs in starting or running state
            final long podId = pod.getId();
            final List<DomainRouterVO> virtualRouters = routerDao.listByPodIdAndStates(podId, VirtualMachine.State.Starting, VirtualMachine.State.Running);
            if (virtualRouters.size() > 1) {
                // FIXME Find or create a better and more specific exception for this
                throw new CloudRuntimeException("Pod can have utmost one VR in Basic Zone, please check!");
            }
            // Add virtualRouters to the routers, this avoids the situation when
            // all routers are skipped and VirtualRouterElement throws exception
            routers.addAll(virtualRouters);
            // If List size is one, we already have a starting or running VR, skip deployment
            if (virtualRouters.size() == 1) {
                logger.debug("Skipping VR deployment: Found a running or starting VR in Pod " + pod.getName() + " id=" + podId);
                continue;
            }
            // Add new DeployDestination for this pod
            destinations.add(new DeployDestination(dest.getDataCenter(), pod, null, null));
        }
    } else {
        // Else, just add the supplied dest
        destinations.add(dest);
    }
    return destinations;
}
Also used : DeployDestination(com.cloud.deploy.DeployDestination) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) HostPodVO(com.cloud.dc.HostPodVO) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 29 with HostPodVO

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

the class ConfigurationManagerTest method checkIfZoneIsDeletableFailureOnHostTest.

@Test(expected = CloudRuntimeException.class)
public void checkIfZoneIsDeletableFailureOnHostTest() {
    HostVO hostVO = Mockito.mock(HostVO.class);
    ArrayList<HostVO> arrayList = new ArrayList<HostVO>();
    arrayList.add(hostVO);
    Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(arrayList);
    Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
    Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
    Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
    Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
    Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
    Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
    configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) Random(java.util.Random) ArrayList(java.util.ArrayList) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) Test(org.junit.Test)

Example 30 with HostPodVO

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

the class ConfigurationManagerTest method checkIfPodIsDeletableSuccessTest.

@Test
public void checkIfPodIsDeletableSuccessTest() {
    HostPodVO hostPodVO = Mockito.mock(HostPodVO.class);
    Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong());
    Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO);
    Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0);
    Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList<VolumeVO>());
    Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList<HostVO>());
    Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
    Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
    configurationMgr.checkIfPodIsDeletable(new Random().nextLong());
}
Also used : ClusterVO(com.cloud.dc.ClusterVO) Random(java.util.Random) VolumeVO(com.cloud.storage.VolumeVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) Test(org.junit.Test)

Aggregations

HostPodVO (com.cloud.dc.HostPodVO)68 DataCenterVO (com.cloud.dc.DataCenterVO)32 HostVO (com.cloud.host.HostVO)32 ClusterVO (com.cloud.dc.ClusterVO)31 ArrayList (java.util.ArrayList)29 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)23 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)16 VolumeVO (com.cloud.storage.VolumeVO)14 VMInstanceVO (com.cloud.vm.VMInstanceVO)14 Account (com.cloud.user.Account)13 DB (com.cloud.utils.db.DB)12 Test (org.junit.Test)12 Random (java.util.Random)11 TransactionStatus (com.cloud.utils.db.TransactionStatus)10 ConfigurationException (javax.naming.ConfigurationException)10 HashMap (java.util.HashMap)8 List (java.util.List)8 DataCenter (com.cloud.dc.DataCenter)7 DedicatedResourceVO (com.cloud.dc.DedicatedResourceVO)7 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)6