Search in sources :

Example 6 with ServiceOfferingJoinVO

use of com.cloud.api.query.vo.ServiceOfferingJoinVO in project cloudstack by apache.

the class VolumeApiServiceImplTest method prepareAndRunTestOfIsNotPossibleToResize.

private void prepareAndRunTestOfIsNotPossibleToResize(Type volumeType, Long rootDisk, Storage.ImageFormat imageFormat, boolean expectedIsNotPossibleToResize) {
    VolumeVO volume = Mockito.mock(VolumeVO.class);
    when(volume.getVolumeType()).thenReturn(volumeType);
    when(volume.getTemplateId()).thenReturn(1l);
    DiskOfferingVO diskOffering = Mockito.mock(DiskOfferingVO.class);
    ServiceOfferingJoinVO serviceOfferingJoinVO = Mockito.mock(ServiceOfferingJoinVO.class);
    when(serviceOfferingJoinVO.getRootDiskSize()).thenReturn(rootDisk);
    when(serviceOfferingJoinDao.findById(Mockito.anyLong())).thenReturn(serviceOfferingJoinVO);
    VMTemplateVO template = Mockito.mock(VMTemplateVO.class);
    when(template.getFormat()).thenReturn(imageFormat);
    when(templateDao.findByIdIncludingRemoved(Mockito.anyLong())).thenReturn(template);
    boolean result = volumeApiServiceImpl.isNotPossibleToResize(volume, diskOffering);
    Assert.assertEquals(expectedIsNotPossibleToResize, result);
}
Also used : ServiceOfferingJoinVO(com.cloud.api.query.vo.ServiceOfferingJoinVO)

Example 7 with ServiceOfferingJoinVO

use of com.cloud.api.query.vo.ServiceOfferingJoinVO in project cloudstack by apache.

the class QueryManagerImpl method searchForServiceOfferingsInternal.

private Pair<List<ServiceOfferingJoinVO>, Integer> searchForServiceOfferingsInternal(ListServiceOfferingsCmd cmd) {
    // Note
    // The filteredOfferings method for offerings is being modified in accordance with
    // discussion with Will/Kevin
    // For now, we will be listing the following based on the usertype
    // 1. For root, we will filteredOfferings all offerings
    // 2. For domainAdmin and regular users, we will filteredOfferings everything in
    // their domains+parent domains ... all the way
    // till
    // root
    Filter searchFilter = new Filter(ServiceOfferingJoinVO.class, "sortKey", SortKeyAscending.value(), cmd.getStartIndex(), cmd.getPageSizeVal());
    searchFilter.addOrderBy(ServiceOfferingJoinVO.class, "id", true);
    Account caller = CallContext.current().getCallingAccount();
    Object name = cmd.getServiceOfferingName();
    Object id = cmd.getId();
    Object keyword = cmd.getKeyword();
    Long vmId = cmd.getVirtualMachineId();
    Long domainId = cmd.getDomainId();
    Boolean isSystem = cmd.getIsSystem();
    String vmTypeStr = cmd.getSystemVmType();
    ServiceOfferingVO currentVmOffering = null;
    Boolean isRecursive = cmd.isRecursive();
    Long zoneId = cmd.getZoneId();
    Integer cpuNumber = cmd.getCpuNumber();
    Integer memory = cmd.getMemory();
    Integer cpuSpeed = cmd.getCpuSpeed();
    SearchCriteria<ServiceOfferingJoinVO> sc = _srvOfferingJoinDao.createSearchCriteria();
    if (!_accountMgr.isRootAdmin(caller.getId()) && isSystem) {
        throw new InvalidParameterValueException("Only ROOT admins can access system's offering");
    }
    // domain
    if (domainId != null && !_accountMgr.isRootAdmin(caller.getId())) {
        // child of so's domain
        if (!isPermissible(caller.getDomainId(), domainId)) {
            throw new PermissionDeniedException("The account:" + caller.getAccountName() + " does not fall in the same domain hierarchy as the service offering");
        }
    }
    if (vmId != null) {
        VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId);
        if ((vmInstance == null) || (vmInstance.getRemoved() != null)) {
            InvalidParameterValueException ex = new InvalidParameterValueException("unable to find a virtual machine with specified id");
            ex.addProxyObject(vmId.toString(), "vmId");
            throw ex;
        }
        _accountMgr.checkAccess(caller, null, true, vmInstance);
        currentVmOffering = _srvOfferingDao.findByIdIncludingRemoved(vmInstance.getId(), vmInstance.getServiceOfferingId());
        if (!currentVmOffering.isDynamic()) {
            sc.addAnd("id", SearchCriteria.Op.NEQ, currentVmOffering.getId());
        }
        if (currentVmOffering.getDiskOfferingStrictness()) {
            sc.addAnd("diskOfferingId", Op.EQ, currentVmOffering.getDiskOfferingId());
            sc.addAnd("diskOfferingStrictness", Op.EQ, true);
        } else {
            sc.addAnd("diskOfferingStrictness", Op.EQ, false);
        }
        boolean isRootVolumeUsingLocalStorage = virtualMachineManager.isRootVolumeOnLocalStorage(vmId);
        // 1. Only return offerings with the same storage type than the storage pool where the VM's root volume is allocated
        sc.addAnd("useLocalStorage", SearchCriteria.Op.EQ, isRootVolumeUsingLocalStorage);
        // 2.In case vm is running return only offerings greater than equal to current offering compute and offering's dynamic scalability should match
        if (vmInstance.getState() == VirtualMachine.State.Running) {
            Integer vmCpu = currentVmOffering.getCpu();
            Integer vmMemory = currentVmOffering.getRamSize();
            Integer vmSpeed = currentVmOffering.getSpeed();
            if ((vmCpu == null || vmMemory == null || vmSpeed == null) && VirtualMachine.Type.User.equals(vmInstance.getType())) {
                UserVmVO userVmVO = _userVmDao.findById(vmId);
                _userVmDao.loadDetails(userVmVO);
                Map<String, String> details = userVmVO.getDetails();
                vmCpu = NumbersUtil.parseInt(details.get(ApiConstants.CPU_NUMBER), 0);
                if (vmSpeed == null) {
                    vmSpeed = NumbersUtil.parseInt(details.get(ApiConstants.CPU_SPEED), 0);
                }
                vmMemory = NumbersUtil.parseInt(details.get(ApiConstants.MEMORY), 0);
            }
            if (vmCpu != null && vmCpu > 0) {
                sc.addAnd("cpu", Op.SC, getMinimumCpuServiceOfferingJoinSearchCriteria(vmCpu));
            }
            if (vmSpeed != null && vmSpeed > 0) {
                sc.addAnd("speed", Op.SC, getMinimumCpuSpeedServiceOfferingJoinSearchCriteria(vmSpeed));
            }
            if (vmMemory != null && vmMemory > 0) {
                sc.addAnd("ramSize", Op.SC, getMinimumMemoryServiceOfferingJoinSearchCriteria(vmMemory));
            }
            sc.addAnd("dynamicScalingEnabled", Op.EQ, currentVmOffering.isDynamicScalingEnabled());
        }
    }
    // boolean includePublicOfferings = false;
    if ((_accountMgr.isNormalUser(caller.getId()) || _accountMgr.isDomainAdmin(caller.getId())) || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
        // For non-root users.
        if (isSystem) {
            throw new InvalidParameterValueException("Only root admins can access system's offering");
        }
        if (isRecursive) {
            // domain + all sub-domains
            if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
                throw new InvalidParameterValueException("Only ROOT admins and Domain admins can list service offerings with isrecursive=true");
            }
        }
    } else {
        // for root users
        if (caller.getDomainId() != 1 && isSystem) {
            // NON ROOT admin
            throw new InvalidParameterValueException("Non ROOT admins cannot access system's offering");
        }
        if (domainId != null) {
            sc.addAnd("domainId", Op.FIND_IN_SET, String.valueOf(domainId));
        }
    }
    if (keyword != null) {
        SearchCriteria<ServiceOfferingJoinVO> ssc = _srvOfferingJoinDao.createSearchCriteria();
        ssc.addOr("displayText", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (id != null) {
        sc.addAnd("id", SearchCriteria.Op.EQ, id);
    }
    if (isSystem != null) {
        // note that for non-root users, isSystem is always false when
        // control comes to here
        sc.addAnd("systemUse", SearchCriteria.Op.EQ, isSystem);
    }
    if (name != null) {
        sc.addAnd("name", SearchCriteria.Op.EQ, name);
    }
    if (vmTypeStr != null) {
        sc.addAnd("vmType", SearchCriteria.Op.EQ, vmTypeStr);
    }
    if (zoneId != null) {
        SearchBuilder<ServiceOfferingJoinVO> sb = _srvOfferingJoinDao.createSearchBuilder();
        sb.and("zoneId", sb.entity().getZoneId(), Op.FIND_IN_SET);
        sb.or("zId", sb.entity().getZoneId(), Op.NULL);
        sb.done();
        SearchCriteria<ServiceOfferingJoinVO> zoneSC = sb.create();
        zoneSC.setParameters("zoneId", String.valueOf(zoneId));
        sc.addAnd("zoneId", SearchCriteria.Op.SC, zoneSC);
    }
    if (cpuNumber != null) {
        SearchCriteria<ServiceOfferingJoinVO> cpuConstraintSearchCriteria = _srvOfferingJoinDao.createSearchCriteria();
        cpuConstraintSearchCriteria.addAnd("minCpu", Op.LTEQ, cpuNumber);
        cpuConstraintSearchCriteria.addAnd("maxCpu", Op.GTEQ, cpuNumber);
        SearchCriteria<ServiceOfferingJoinVO> cpuSearchCriteria = _srvOfferingJoinDao.createSearchCriteria();
        cpuSearchCriteria.addOr("minCpu", Op.NULL);
        cpuSearchCriteria.addOr("constraints", Op.SC, cpuConstraintSearchCriteria);
        cpuSearchCriteria.addOr("minCpu", Op.GTEQ, cpuNumber);
        sc.addAnd("cpuConstraints", SearchCriteria.Op.SC, cpuSearchCriteria);
    }
    if (memory != null) {
        SearchCriteria<ServiceOfferingJoinVO> memoryConstraintSearchCriteria = _srvOfferingJoinDao.createSearchCriteria();
        memoryConstraintSearchCriteria.addAnd("minMemory", Op.LTEQ, memory);
        memoryConstraintSearchCriteria.addAnd("maxMemory", Op.GTEQ, memory);
        SearchCriteria<ServiceOfferingJoinVO> memSearchCriteria = _srvOfferingJoinDao.createSearchCriteria();
        memSearchCriteria.addOr("minMemory", Op.NULL);
        memSearchCriteria.addOr("memconstraints", Op.SC, memoryConstraintSearchCriteria);
        memSearchCriteria.addOr("minMemory", Op.GTEQ, memory);
        sc.addAnd("memoryConstraints", SearchCriteria.Op.SC, memSearchCriteria);
    }
    if (cpuSpeed != null) {
        SearchCriteria<ServiceOfferingJoinVO> cpuSpeedSearchCriteria = _srvOfferingJoinDao.createSearchCriteria();
        cpuSpeedSearchCriteria.addOr("speed", Op.NULL);
        cpuSpeedSearchCriteria.addOr("speed", Op.GTEQ, cpuSpeed);
        sc.addAnd("cpuspeedconstraints", SearchCriteria.Op.SC, cpuSpeedSearchCriteria);
    }
    // Fetch the offering ids from the details table since theres no smart way to filter them in the join ... yet!
    if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        Domain callerDomain = _domainDao.findById(caller.getDomainId());
        List<Long> domainIds = findRelatedDomainIds(callerDomain, isRecursive);
        List<Long> ids = _srvOfferingDetailsDao.findOfferingIdsByDomainIds(domainIds);
        SearchBuilder<ServiceOfferingJoinVO> sb = _srvOfferingJoinDao.createSearchBuilder();
        if (ids != null && !ids.isEmpty()) {
            sb.and("id", sb.entity().getId(), Op.IN);
        }
        sb.or("domainId", sb.entity().getDomainId(), Op.NULL);
        sb.done();
        SearchCriteria<ServiceOfferingJoinVO> scc = sb.create();
        if (ids != null && !ids.isEmpty()) {
            scc.setParameters("id", ids.toArray());
        }
        sc.addAnd("domainId", SearchCriteria.Op.SC, scc);
    }
    if (currentVmOffering != null) {
        DiskOfferingVO diskOffering = _diskOfferingDao.findByIdIncludingRemoved(currentVmOffering.getDiskOfferingId());
        List<String> storageTags = com.cloud.utils.StringUtils.csvTagsToList(diskOffering.getTags());
        if (!storageTags.isEmpty() && VolumeApiServiceImpl.MatchStoragePoolTagsWithDiskOffering.value()) {
            SearchBuilder<ServiceOfferingJoinVO> sb = _srvOfferingJoinDao.createSearchBuilder();
            for (String tag : storageTags) {
                sb.and(tag, sb.entity().getTags(), Op.FIND_IN_SET);
            }
            sb.done();
            SearchCriteria<ServiceOfferingJoinVO> scc = sb.create();
            for (String tag : storageTags) {
                scc.setParameters(tag, tag);
            }
            sc.addAnd("storageTags", SearchCriteria.Op.SC, scc);
        }
        List<String> hostTags = com.cloud.utils.StringUtils.csvTagsToList(currentVmOffering.getHostTag());
        if (!hostTags.isEmpty()) {
            SearchBuilder<ServiceOfferingJoinVO> sb = _srvOfferingJoinDao.createSearchBuilder();
            for (String tag : hostTags) {
                sb.and(tag, sb.entity().getHostTag(), Op.FIND_IN_SET);
            }
            sb.done();
            SearchCriteria<ServiceOfferingJoinVO> scc = sb.create();
            for (String tag : hostTags) {
                scc.setParameters(tag, tag);
            }
            sc.addAnd("hostTags", SearchCriteria.Op.SC, scc);
        }
    }
    return _srvOfferingJoinDao.searchAndCount(sc, searchFilter);
}
Also used : Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) ServiceOfferingJoinVO(com.cloud.api.query.vo.ServiceOfferingJoinVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) Domain(com.cloud.domain.Domain)

Aggregations

ServiceOfferingJoinVO (com.cloud.api.query.vo.ServiceOfferingJoinVO)7 Account (com.cloud.user.Account)3 ArrayList (java.util.ArrayList)3 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)2 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)2 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)2 Filter (com.cloud.utils.db.Filter)2 UserVmVO (com.cloud.vm.UserVmVO)2 VMInstanceVO (com.cloud.vm.VMInstanceVO)2 DiskOfferingJoinVO (com.cloud.api.query.vo.DiskOfferingJoinVO)1 DataCenter (com.cloud.dc.DataCenter)1 Domain (com.cloud.domain.Domain)1 DomainVO (com.cloud.domain.DomainVO)1 CloudAuthenticationException (com.cloud.exception.CloudAuthenticationException)1 IpAddresses (com.cloud.network.Network.IpAddresses)1 DiskOffering (com.cloud.offering.DiskOffering)1 ServiceOffering (com.cloud.offering.ServiceOffering)1 ResourceTag (com.cloud.server.ResourceTag)1 DiskOfferingVO (com.cloud.storage.DiskOfferingVO)1