Search in sources :

Example 86 with DomainVO

use of com.cloud.domain.DomainVO in project cosmic by MissionCriticalCloud.

the class HypervisorGuruBase method toVirtualMachineTO.

protected VirtualMachineTO toVirtualMachineTO(final VirtualMachineProfile vmProfile) {
    final ServiceOffering offering = _serviceOfferingDao.findById(vmProfile.getId(), vmProfile.getServiceOfferingId());
    final VirtualMachine vm = vmProfile.getVirtualMachine();
    final Long minMemory = (long) (offering.getRamSize() / vmProfile.getMemoryOvercommitRatio());
    final VirtualMachineTO to = new VirtualMachineTO(vm.getId(), vm.getInstanceName(), vm.getType(), offering.getCpu(), minMemory * 1024l * 1024l, offering.getRamSize() * 1024l * 1024l, null, null, vm.isHaEnabled(), vm.limitCpuUse(), vm.getVncPassword());
    to.setBootArgs(vmProfile.getBootArgs());
    final List<NicProfile> nicProfiles = vmProfile.getNics();
    final NicTO[] nics = new NicTO[nicProfiles.size()];
    int i = 0;
    for (final NicProfile nicProfile : nicProfiles) {
        nics[i++] = toNicTO(nicProfile);
    }
    to.setNics(nics);
    to.setDisks(vmProfile.getDisks().toArray(new DiskTO[vmProfile.getDisks().size()]));
    if (vmProfile.getTemplate().getBits() == 32) {
        to.setArch("i686");
    } else {
        to.setArch("x86_64");
    }
    final Map<String, String> detailsInVm = _userVmDetailsDao.listDetailsKeyPairs(vm.getId());
    if (detailsInVm != null) {
        to.setDetails(detailsInVm);
    }
    // Set GPU details
    ServiceOfferingDetailsVO offeringDetail;
    if ((offeringDetail = _serviceOfferingDetailsDao.findDetail(offering.getId(), GPU.Keys.vgpuType.toString())) != null) {
        final ServiceOfferingDetailsVO groupName = _serviceOfferingDetailsDao.findDetail(offering.getId(), GPU.Keys.pciDevice.toString());
        to.setGpuDevice(_resourceMgr.getGPUDevice(vm.getHostId(), groupName.getValue(), offeringDetail.getValue()));
    }
    // Workaround to make sure the TO has the UUID we need for Niciri integration
    final VMInstanceVO vmInstance = _virtualMachineDao.findById(to.getId());
    // check if XStools tools are present in the VM and dynamic scaling feature is enabled (per zone/global)
    final Boolean isDynamicallyScalable = vmInstance.isDynamicallyScalable() && UserVmManager.EnableDynamicallyScaleVm.valueIn(vm.getDataCenterId());
    to.setEnableDynamicallyScaleVm(isDynamicallyScalable);
    to.setUuid(vmInstance.getUuid());
    to.setVmData(vmProfile.getVmData());
    to.setConfigDriveLabel(vmProfile.getConfigDriveLabel());
    to.setConfigDriveIsoRootFolder(vmProfile.getConfigDriveIsoRootFolder());
    to.setConfigDriveIsoFile(vmProfile.getConfigDriveIsoFile());
    final MetadataTO metadataTO = new MetadataTO();
    final DomainVO domain = _domainDao.findById(vm.getDomainId());
    metadataTO.setDomainUuid(domain.getUuid());
    to.setMetadata(metadataTO);
    return to;
}
Also used : ServiceOffering(com.cloud.offering.ServiceOffering) MetadataTO(com.cloud.agent.api.to.MetadataTO) ServiceOfferingDetailsVO(com.cloud.service.ServiceOfferingDetailsVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) NicProfile(com.cloud.vm.NicProfile) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) DomainVO(com.cloud.domain.DomainVO) VirtualMachine(com.cloud.vm.VirtualMachine) NicTO(com.cloud.agent.api.to.NicTO) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 87 with DomainVO

use of com.cloud.domain.DomainVO in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method updateDomain.

@Override
@DB
public DomainVO updateDomain(final UpdateDomainCmd cmd) {
    final Long domainId = cmd.getId();
    final String domainName = cmd.getDomainName();
    final String networkDomain = cmd.getNetworkDomain();
    // check if domain exists in the system
    final DomainVO domain = _domainDao.findById(domainId);
    if (domain == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find domain with specified domain id");
        ex.addProxyObject(domainId.toString(), "domainId");
        throw ex;
    } else if (domain.getParent() == null && domainName != null) {
        // name
        throw new InvalidParameterValueException("ROOT domain can not be edited with a new name");
    }
    final Account caller = getCaller();
    _accountMgr.checkAccess(caller, domain);
    // domain name is unique under the parent domain
    if (domainName != null) {
        final SearchCriteria<DomainVO> sc = _domainDao.createSearchCriteria();
        sc.addAnd("name", SearchCriteria.Op.EQ, domainName);
        sc.addAnd("parent", SearchCriteria.Op.EQ, domain.getParent());
        final List<DomainVO> domains = _domainDao.search(sc, null);
        final boolean sameDomain = domains.size() == 1 && domains.get(0).getId() == domainId;
        if (!domains.isEmpty() && !sameDomain) {
            final InvalidParameterValueException ex = new InvalidParameterValueException("Failed to update specified domain id with name '" + domainName + "' since it already exists in the system");
            ex.addProxyObject(domain.getUuid(), "domainId");
            throw ex;
        }
    }
    // validate network domain
    if (networkDomain != null && !networkDomain.isEmpty()) {
        if (!NetUtils.verifyDomainName(networkDomain)) {
            throw new InvalidParameterValueException("Invalid network domain. Total length shouldn't exceed 190 chars. Each domain label must be between 1 and 63 characters long, can contain ASCII letters " + "'a' through 'z', the digits '0' through '9', " + "and the hyphen ('-'); can't start or end with \"-\"");
        }
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            if (domainName != null) {
                final String updatedDomainPath = getUpdatedDomainPath(domain.getPath(), domainName);
                updateDomainChildren(domain, updatedDomainPath);
                domain.setName(domainName);
                domain.setPath(updatedDomainPath);
            }
            if (networkDomain != null) {
                if (networkDomain.isEmpty()) {
                    domain.setNetworkDomain(null);
                } else {
                    domain.setNetworkDomain(networkDomain);
                }
            }
            _domainDao.update(domainId, domain);
        }
    });
    return _domainDao.findById(domainId);
}
Also used : DomainVO(com.cloud.domain.DomainVO) Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DB(com.cloud.utils.db.DB)

Example 88 with DomainVO

use of com.cloud.domain.DomainVO in project cosmic by MissionCriticalCloud.

the class ManagementServerImpl method deleteSSHKeyPair.

@Override
public boolean deleteSSHKeyPair(final DeleteSSHKeyPairCmd cmd) {
    final Account caller = getCaller();
    final String accountName = cmd.getAccountName();
    final Long domainId = cmd.getDomainId();
    final Long projectId = cmd.getProjectId();
    final Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, projectId);
    final SSHKeyPairVO s = _sshKeyPairDao.findByName(owner.getAccountId(), owner.getDomainId(), cmd.getName());
    if (s == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("A key pair with name '" + cmd.getName() + "' does not exist for account " + owner.getAccountName() + " in specified domain id");
        final DomainVO domain = ApiDBUtils.findDomainById(owner.getDomainId());
        String domainUuid = String.valueOf(owner.getDomainId());
        if (domain != null) {
            domainUuid = domain.getUuid();
        }
        ex.addProxyObject(domainUuid, "domainId");
        throw ex;
    }
    return _sshKeyPairDao.deleteByName(owner.getAccountId(), owner.getDomainId(), cmd.getName());
}
Also used : Account(com.cloud.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) SSHKeyPairVO(com.cloud.user.SSHKeyPairVO)

Example 89 with DomainVO

use of com.cloud.domain.DomainVO in project cosmic by MissionCriticalCloud.

the class ResourceLimitManagerImpl method updateResourceLimit.

@Override
public ResourceLimitVO updateResourceLimit(final Long accountId, final Long domainId, final Integer typeId, Long max) {
    final Account caller = CallContext.current().getCallingAccount();
    if (max == null) {
        max = new Long(Resource.RESOURCE_UNLIMITED);
    } else if (max.longValue() < Resource.RESOURCE_UNLIMITED) {
        throw new InvalidParameterValueException("Please specify either '-1' for an infinite limit, or a limit that is at least '0'.");
    }
    // Map resource type
    ResourceType resourceType = null;
    if (typeId != null) {
        for (final ResourceType type : Resource.ResourceType.values()) {
            if (type.getOrdinal() == typeId.intValue()) {
                resourceType = type;
            }
        }
        if (resourceType == null) {
            throw new InvalidParameterValueException("Please specify valid resource type");
        }
    }
    // Convert max storage size from GiB to bytes
    if ((resourceType == ResourceType.primary_storage || resourceType == ResourceType.secondary_storage) && max >= 0) {
        max = max * ResourceType.bytesToGiB;
    }
    ResourceOwnerType ownerType = null;
    Long ownerId = null;
    if (accountId != null) {
        final Account account = _entityMgr.findById(Account.class, accountId);
        if (account == null) {
            throw new InvalidParameterValueException("Unable to find account " + accountId);
        }
        if (account.getId() == Account.ACCOUNT_ID_SYSTEM) {
            throw new InvalidParameterValueException("Can't update system account");
        }
        // only Unlimited value is accepted if account is  Root Admin
        if (_accountMgr.isRootAdmin(account.getId()) && max.shortValue() != Resource.RESOURCE_UNLIMITED) {
            throw new InvalidParameterValueException("Only " + Resource.RESOURCE_UNLIMITED + " limit is supported for Root Admin accounts");
        }
        if ((caller.getAccountId() == accountId.longValue()) && (_accountMgr.isDomainAdmin(caller.getId()) || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN)) {
            // If the admin is trying to update his own account, disallow.
            throw new PermissionDeniedException("Unable to update resource limit for his own account " + accountId + ", permission denied");
        }
        if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
            _accountMgr.checkAccess(caller, AccessType.ModifyProject, true, account);
        } else {
            _accountMgr.checkAccess(caller, null, true, account);
        }
        ownerType = ResourceOwnerType.Account;
        ownerId = accountId;
    } else if (domainId != null) {
        final Domain domain = _entityMgr.findById(Domain.class, domainId);
        _accountMgr.checkAccess(caller, domain);
        if (Domain.ROOT_DOMAIN == domainId.longValue()) {
            // no one can add limits on ROOT domain, disallow...
            throw new PermissionDeniedException("Cannot update resource limit for ROOT domain " + domainId + ", permission denied");
        }
        if ((caller.getDomainId() == domainId.longValue()) && caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
            // if the admin is trying to update their own domain, disallow...
            throw new PermissionDeniedException("Unable to update resource limit for domain " + domainId + ", permission denied");
        }
        final Long parentDomainId = domain.getParent();
        if (parentDomainId != null) {
            final DomainVO parentDomain = _domainDao.findById(parentDomainId);
            final long parentMaximum = findCorrectResourceLimitForDomain(parentDomain, resourceType);
            if ((parentMaximum >= 0) && (max.longValue() > parentMaximum)) {
                throw new InvalidParameterValueException("Domain " + domain.getName() + "(id: " + parentDomain.getId() + ") has maximum allowed resource limit " + parentMaximum + " for " + resourceType + ", please specify a value less that or equal to " + parentMaximum);
            }
        }
        ownerType = ResourceOwnerType.Domain;
        ownerId = domainId;
    }
    if (ownerId == null) {
        throw new InvalidParameterValueException("AccountId or domainId have to be specified in order to update resource limit");
    }
    final ResourceLimitVO limit = _resourceLimitDao.findByOwnerIdAndType(ownerId, ownerType, resourceType);
    if (limit != null) {
        // Update the existing limit
        _resourceLimitDao.update(limit.getId(), max);
        return _resourceLimitDao.findById(limit.getId());
    } else {
        return _resourceLimitDao.persist(new ResourceLimitVO(resourceType, max, ownerId, ownerType));
    }
}
Also used : Account(com.cloud.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ResourceOwnerType(com.cloud.configuration.Resource.ResourceOwnerType) ResourceType(com.cloud.configuration.Resource.ResourceType) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) Domain(com.cloud.domain.Domain) ResourceLimitVO(com.cloud.configuration.ResourceLimitVO)

Example 90 with DomainVO

use of com.cloud.domain.DomainVO in project cosmic by MissionCriticalCloud.

the class ResourceLimitManagerImpl method recalculateResourceCount.

@Override
public List<ResourceCountVO> recalculateResourceCount(final Long accountId, final Long domainId, final Integer typeId) throws InvalidParameterValueException, CloudRuntimeException, PermissionDeniedException {
    final Account callerAccount = CallContext.current().getCallingAccount();
    long count;
    final List<ResourceCountVO> counts = new ArrayList<>();
    List<ResourceType> resourceTypes = new ArrayList<>();
    ResourceType resourceType = null;
    if (typeId != null) {
        for (final ResourceType type : Resource.ResourceType.values()) {
            if (type.getOrdinal() == typeId.intValue()) {
                resourceType = type;
            }
        }
        if (resourceType == null) {
            throw new InvalidParameterValueException("Please specify valid resource type");
        }
    }
    final DomainVO domain = _domainDao.findById(domainId);
    if (domain == null) {
        throw new InvalidParameterValueException("Please specify a valid domain ID.");
    }
    _accountMgr.checkAccess(callerAccount, domain);
    if (resourceType != null) {
        resourceTypes.add(resourceType);
    } else {
        resourceTypes = Arrays.asList(Resource.ResourceType.values());
    }
    for (final ResourceType type : resourceTypes) {
        if (accountId != null) {
            if (type.supportsOwner(ResourceOwnerType.Account)) {
                count = recalculateAccountResourceCount(accountId, type);
                counts.add(new ResourceCountVO(type, count, accountId, ResourceOwnerType.Account));
            }
        } else {
            if (type.supportsOwner(ResourceOwnerType.Domain)) {
                count = recalculateDomainResourceCount(domainId, type);
                counts.add(new ResourceCountVO(type, count, domainId, ResourceOwnerType.Domain));
            }
        }
    }
    return counts;
}
Also used : Account(com.cloud.user.Account) DomainVO(com.cloud.domain.DomainVO) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ResourceCountVO(com.cloud.configuration.ResourceCountVO) ArrayList(java.util.ArrayList) ResourceType(com.cloud.configuration.Resource.ResourceType)

Aggregations

DomainVO (com.cloud.domain.DomainVO)196 Account (com.cloud.user.Account)85 AccountVO (com.cloud.user.AccountVO)64 Test (org.junit.Test)56 ArrayList (java.util.ArrayList)53 DomainDao (com.cloud.domain.dao.DomainDao)30 Field (java.lang.reflect.Field)30 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)29 SslCertDao (com.cloud.network.dao.SslCertDao)29 AccountManager (com.cloud.user.AccountManager)29 SslCertVO (com.cloud.network.dao.SslCertVO)27 List (java.util.List)26 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)24 Pair (com.cloud.utils.Pair)24 Domain (com.cloud.domain.Domain)23 Filter (com.cloud.utils.db.Filter)23 File (java.io.File)23 IOException (java.io.IOException)23 FileUtils.readFileToString (org.apache.commons.io.FileUtils.readFileToString)23 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)22