Search in sources :

Example 11 with DedicatedResourceVO

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

the class DomainChecker method checkAccess.

@Override
public boolean checkAccess(Account account, DataCenter zone) throws PermissionDeniedException {
    if (account == null || zone.getDomainId() == null) {
        //public zone
        return true;
    } else {
        //admin has all permissions
        if (_accountService.isRootAdmin(account.getId())) {
            return true;
        } else //check if account's domain is a child of zone's domain
        if (_accountService.isNormalUser(account.getId()) || account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
            // if zone is dedicated to an account check that the accountId
            // matches.
            DedicatedResourceVO dedicatedZone = _dedicatedDao.findByZoneId(zone.getId());
            if (dedicatedZone != null) {
                if (dedicatedZone.getAccountId() != null) {
                    if (dedicatedZone.getAccountId() == account.getId()) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
            if (account.getDomainId() == zone.getDomainId()) {
                //zone and account at exact node
                return true;
            } else {
                Domain domainRecord = _domainDao.findById(account.getDomainId());
                if (domainRecord != null) {
                    while (true) {
                        if (domainRecord.getId() == zone.getDomainId()) {
                            //found as a child
                            return true;
                        }
                        if (domainRecord.getParent() != null) {
                            domainRecord = _domainDao.findById(domainRecord.getParent());
                        } else {
                            break;
                        }
                    }
                }
            }
            //not found
            return false;
        } else //check if the account's domain is either child of zone's domain, or if zone's domain is child of account's domain
        if (_accountService.isDomainAdmin(account.getId())) {
            if (account.getDomainId() == zone.getDomainId()) {
                //zone and account at exact node
                return true;
            } else {
                Domain zoneDomainRecord = _domainDao.findById(zone.getDomainId());
                Domain accountDomainRecord = _domainDao.findById(account.getDomainId());
                if (accountDomainRecord != null) {
                    Domain localRecord = accountDomainRecord;
                    while (true) {
                        if (localRecord.getId() == zone.getDomainId()) {
                            //found as a child
                            return true;
                        }
                        if (localRecord.getParent() != null) {
                            localRecord = _domainDao.findById(localRecord.getParent());
                        } else {
                            break;
                        }
                    }
                }
                //didn't find in upper tree
                if (zoneDomainRecord != null && accountDomainRecord != null && zoneDomainRecord.getPath().contains(accountDomainRecord.getPath())) {
                    return true;
                }
            }
            //not found
            return false;
        }
    }
    return false;
}
Also used : DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) Domain(com.cloud.domain.Domain)

Example 12 with DedicatedResourceVO

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

the class DedicatedApiUnitTest method dedicateZoneExistTest.

/*    @Test
        public void runDedicateZoneTest() {
            DataCenterVO dc = new DataCenterVO(10L, "TestZone", "Dedicated",
                    "8.8.8.8", null, "10.0.0.1", null, "10.0.0.1/24", null, null,
                    NetworkType.Basic, null, null);
            when(_dcDao.findById(10L)).thenReturn(dc);
            try {
                List<DedicatedResourceVO> result = _dedicatedService.dedicateZone(10L, domainId, accountName);
                Assert.assertNotNull(result);
            } catch (Exception e) {
                s_logger.info("exception in testing dedication of zone "
                        + e.toString());
            }
        }

        @Test
        public void runDedicatePodTest() {
            HostPodVO pod = new HostPodVO("TestPod", 20L, "10.0.0.1", "10.0.0.0",
                    22, null);
            when(_podDao.findById(10L)).thenReturn(pod);
            try {
                List<DedicatedResourceVO> result = _dedicatedService.dedicatePod(10L, domainId, accountName);
                Assert.assertNotNull(result);
            } catch (Exception e) {
                s_logger.info("exception in testing dedication of pod "
                        + e.toString());
            }
        }

        @Test
        public void runDedicateClusterTest() {
            ClusterVO cluster = new ClusterVO(10L, 10L, "TestCluster");
            when(_clusterDao.findById(10L)).thenReturn(cluster);
            try {
                List<DedicatedResourceVO> result = _dedicatedService.dedicateCluster(10L, domainId, accountName);
                Assert.assertNotNull(result);
            } catch (Exception e) {
                s_logger.info("exception in testing dedication of cluster "
                        + e.toString());
            }
        }

        @Test
        public void runDedicateHostTest() {
            HostVO host = new HostVO(10L, "Host-1", Host.Type.Routing, null,
                    "10.0.0.0", null, null, null, null, null, null, null, null,
                    Status.Up, null, null, null, 10L, 10L, 30L, 10233, null, null,
                    null, 0, null);
            when(_hostDao.findById(10L)).thenReturn(host);
            try {
                List<DedicatedResourceVO> result = _dedicatedService.dedicateHost(10L, domainId, accountName);
                Assert.assertNotNull(result);
            } catch (Exception e) {
                s_logger.info("exception in testing dedication of host "
                        + e.toString());
            }
        }
    */
@Test(expected = CloudRuntimeException.class)
public void dedicateZoneExistTest() {
    DedicatedResourceVO dr = new DedicatedResourceVO(10L, null, null, null, domainId, accountId, 12L);
    when(_dedicatedDao.findByZoneId(10L)).thenReturn(dr);
    _dedicatedService.dedicateZone(10L, domainId, accountName);
}
Also used : DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) Test(org.junit.Test)

Example 13 with DedicatedResourceVO

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

the class DedicatedApiUnitTest method dedicateClusterExistTest.

@Test(expected = CloudRuntimeException.class)
public void dedicateClusterExistTest() {
    DedicatedResourceVO dr = new DedicatedResourceVO(null, null, 10L, null, domainId, accountId, 12L);
    when(_dedicatedDao.findByClusterId(10L)).thenReturn(dr);
    _dedicatedService.dedicateCluster(10L, domainId, accountName);
}
Also used : DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) Test(org.junit.Test)

Example 14 with DedicatedResourceVO

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

the class DedicatedApiUnitTest method dedicateHostExistTest.

@Test(expected = CloudRuntimeException.class)
public void dedicateHostExistTest() {
    DedicatedResourceVO dr = new DedicatedResourceVO(null, null, null, 10L, domainId, accountId, 12L);
    when(_dedicatedDao.findByHostId(10L)).thenReturn(dr);
    _dedicatedService.dedicateHost(10L, domainId, accountName);
}
Also used : DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) Test(org.junit.Test)

Example 15 with DedicatedResourceVO

use of com.cloud.dc.DedicatedResourceVO 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)

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