Search in sources :

Example 1 with DiskOfferingDetailVO

use of org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO in project cloudstack by apache.

the class VolumeOrchestrator method allocateTemplatedVolume.

private DiskProfile allocateTemplatedVolume(Type type, String name, DiskOffering offering, Long rootDisksize, Long minIops, Long maxIops, VirtualMachineTemplate template, VirtualMachine vm, Account owner, long deviceId, String configurationId) {
    assert (template.getFormat() != ImageFormat.ISO) : "ISO is not a template really....";
    Long size = _tmpltMgr.getTemplateSize(template.getId(), vm.getDataCenterId());
    if (rootDisksize != null) {
        if (template.isDeployAsIs()) {
            // Volume size specified from template deploy-as-is
            size = rootDisksize;
        } else {
            rootDisksize = rootDisksize * 1024 * 1024 * 1024;
            if (rootDisksize > size) {
                s_logger.debug("Using root disk size of " + toHumanReadableSize(rootDisksize) + " Bytes for volume " + name);
                size = rootDisksize;
            } else {
                s_logger.debug("Using root disk size of " + toHumanReadableSize(rootDisksize) + " Bytes for volume " + name + "since specified root disk size of " + rootDisksize + " Bytes is smaller than template");
            }
        }
    }
    minIops = minIops != null ? minIops : offering.getMinIops();
    maxIops = maxIops != null ? maxIops : offering.getMaxIops();
    VolumeVO vol = new VolumeVO(type, name, vm.getDataCenterId(), owner.getDomainId(), owner.getId(), offering.getId(), offering.getProvisioningType(), size, minIops, maxIops, null);
    vol.setFormat(getSupportedImageFormatForCluster(template.getHypervisorType()));
    if (vm != null) {
        vol.setInstanceId(vm.getId());
    }
    vol.setTemplateId(template.getId());
    vol.setDeviceId(deviceId);
    if (type.equals(Type.ROOT) && !vm.getType().equals(VirtualMachine.Type.User)) {
        vol.setRecreatable(true);
    }
    if (vm.getType() == VirtualMachine.Type.User) {
        UserVmVO userVm = _userVmDao.findById(vm.getId());
        vol.setDisplayVolume(userVm.isDisplayVm());
    }
    vol = _volsDao.persist(vol);
    List<VolumeDetailVO> volumeDetailsVO = new ArrayList<VolumeDetailVO>();
    DiskOfferingDetailVO bandwidthLimitDetail = _diskOfferingDetailDao.findDetail(offering.getId(), Volume.BANDWIDTH_LIMIT_IN_MBPS);
    if (bandwidthLimitDetail != null) {
        volumeDetailsVO.add(new VolumeDetailVO(vol.getId(), Volume.BANDWIDTH_LIMIT_IN_MBPS, bandwidthLimitDetail.getValue(), false));
    }
    DiskOfferingDetailVO iopsLimitDetail = _diskOfferingDetailDao.findDetail(offering.getId(), Volume.IOPS_LIMIT);
    if (iopsLimitDetail != null) {
        volumeDetailsVO.add(new VolumeDetailVO(vol.getId(), Volume.IOPS_LIMIT, iopsLimitDetail.getValue(), false));
    }
    if (!volumeDetailsVO.isEmpty()) {
        _volDetailDao.saveDetails(volumeDetailsVO);
    }
    if (StringUtils.isNotBlank(configurationId)) {
        VolumeDetailVO deployConfigurationDetail = new VolumeDetailVO(vol.getId(), VmDetailConstants.DEPLOY_AS_IS_CONFIGURATION, configurationId, false);
        _volDetailDao.persist(deployConfigurationDetail);
    }
    // Create event and update resource count for volumes if vm is a user vm
    if (vm.getType() == VirtualMachine.Type.User) {
        Long offeringId = null;
        if (!offering.isComputeOnly()) {
            offeringId = offering.getId();
        }
        UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_CREATE, vol.getAccountId(), vol.getDataCenterId(), vol.getId(), vol.getName(), offeringId, vol.getTemplateId(), size, Volume.class.getName(), vol.getUuid(), vol.isDisplayVolume());
        _resourceLimitMgr.incrementResourceCount(vm.getAccountId(), ResourceType.volume, vol.isDisplayVolume());
        _resourceLimitMgr.incrementResourceCount(vm.getAccountId(), ResourceType.primary_storage, vol.isDisplayVolume(), new Long(vol.getSize()));
    }
    return toDiskProfile(vol, offering);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VolumeVO(com.cloud.storage.VolumeVO) VmWorkMigrateVolume(com.cloud.vm.VmWorkMigrateVolume) VmWorkAttachVolume(com.cloud.vm.VmWorkAttachVolume) Volume(com.cloud.storage.Volume) ArrayList(java.util.ArrayList) VolumeDetailVO(com.cloud.storage.VolumeDetailVO) DiskOfferingDetailVO(org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO)

Example 2 with DiskOfferingDetailVO

use of org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO in project cloudstack by apache.

the class ConfigurationManagerImpl method updateDiskOffering.

@Override
@ActionEvent(eventType = EventTypes.EVENT_DISK_OFFERING_EDIT, eventDescription = "updating disk offering")
public DiskOffering updateDiskOffering(final UpdateDiskOfferingCmd cmd) {
    final Long diskOfferingId = cmd.getId();
    final String name = cmd.getDiskOfferingName();
    final String displayText = cmd.getDisplayText();
    final Integer sortKey = cmd.getSortKey();
    final Boolean displayDiskOffering = cmd.getDisplayOffering();
    final List<Long> domainIds = cmd.getDomainIds();
    final List<Long> zoneIds = cmd.getZoneIds();
    final String tags = cmd.getTags();
    Long bytesReadRate = cmd.getBytesReadRate();
    Long bytesReadRateMax = cmd.getBytesReadRateMax();
    Long bytesReadRateMaxLength = cmd.getBytesReadRateMaxLength();
    Long bytesWriteRate = cmd.getBytesWriteRate();
    Long bytesWriteRateMax = cmd.getBytesWriteRateMax();
    Long bytesWriteRateMaxLength = cmd.getBytesWriteRateMaxLength();
    Long iopsReadRate = cmd.getIopsReadRate();
    Long iopsReadRateMax = cmd.getIopsReadRateMax();
    Long iopsReadRateMaxLength = cmd.getIopsReadRateMaxLength();
    Long iopsWriteRate = cmd.getIopsWriteRate();
    Long iopsWriteRateMax = cmd.getIopsWriteRateMax();
    Long iopsWriteRateMaxLength = cmd.getIopsWriteRateMaxLength();
    String cacheMode = cmd.getCacheMode();
    // Check if diskOffering exists
    final DiskOffering diskOfferingHandle = _entityMgr.findById(DiskOffering.class, diskOfferingId);
    if (diskOfferingHandle == null) {
        throw new InvalidParameterValueException("Unable to find disk offering by id " + diskOfferingId);
    }
    List<Long> existingDomainIds = diskOfferingDetailsDao.findDomainIds(diskOfferingId);
    Collections.sort(existingDomainIds);
    List<Long> existingZoneIds = diskOfferingDetailsDao.findZoneIds(diskOfferingId);
    Collections.sort(existingZoneIds);
    // check if valid domain
    if (CollectionUtils.isNotEmpty(domainIds)) {
        for (final Long domainId : domainIds) {
            if (_domainDao.findById(domainId) == null) {
                throw new InvalidParameterValueException("Please specify a valid domain id");
            }
        }
    }
    // check if valid zone
    if (CollectionUtils.isNotEmpty(zoneIds)) {
        for (Long zoneId : zoneIds) {
            if (_zoneDao.findById(zoneId) == null)
                throw new InvalidParameterValueException("Please specify a valid zone id");
        }
    }
    Long userId = CallContext.current().getCallingUserId();
    if (userId == null) {
        userId = Long.valueOf(User.UID_SYSTEM);
    }
    final User user = _userDao.findById(userId);
    if (user == null || user.getRemoved() != null) {
        throw new InvalidParameterValueException("Unable to find active user by id " + userId);
    }
    final Account account = _accountDao.findById(user.getAccountId());
    // Filter child domains when both parent and child domains are present
    List<Long> filteredDomainIds = filterChildSubDomains(domainIds);
    Collections.sort(filteredDomainIds);
    List<Long> filteredZoneIds = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(zoneIds)) {
        filteredZoneIds.addAll(zoneIds);
    }
    Collections.sort(filteredZoneIds);
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
        if (!filteredZoneIds.equals(existingZoneIds)) {
            // Domain-admins cannot update zone(s) for offerings
            throw new InvalidParameterValueException(String.format("Unable to update zone(s) for disk offering: %s by admin: %s as it is domain-admin", diskOfferingHandle.getUuid(), user.getUuid()));
        }
        if (existingDomainIds.isEmpty()) {
            throw new InvalidParameterValueException(String.format("Unable to update public disk offering: %s by user: %s because it is domain-admin", diskOfferingHandle.getUuid(), user.getUuid()));
        } else {
            if (filteredDomainIds.isEmpty()) {
                throw new InvalidParameterValueException(String.format("Unable to update disk offering: %s to a public offering by user: %s because it is domain-admin", diskOfferingHandle.getUuid(), user.getUuid()));
            }
        }
        List<Long> nonChildDomains = new ArrayList<>();
        for (Long domainId : existingDomainIds) {
            if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
                if (name != null || displayText != null || sortKey != null) {
                    // Domain-admins cannot update name, display text, sort key for offerings with domain which are not child domains for domain-admin
                    throw new InvalidParameterValueException(String.format("Unable to update disk offering: %s as it has linked domain(s) which are not child domain for domain-admin: %s", diskOfferingHandle.getUuid(), user.getUuid()));
                }
                nonChildDomains.add(domainId);
            }
        }
        for (Long domainId : filteredDomainIds) {
            if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
                Domain domain = _entityMgr.findById(Domain.class, domainId);
                throw new InvalidParameterValueException(String.format("Unable to update disk offering: %s by domain-admin: %s with domain: %3$s which is not a child domain", diskOfferingHandle.getUuid(), user.getUuid(), domain.getUuid()));
            }
        }
        // Final list must include domains which were not child domain for domain-admin but specified for this offering prior to update
        filteredDomainIds.addAll(nonChildDomains);
    } else if (account.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        throw new InvalidParameterValueException(String.format("Unable to update disk offering: %s by id user: %s because it is not root-admin or domain-admin", diskOfferingHandle.getUuid(), user.getUuid()));
    }
    boolean updateNeeded = shouldUpdateDiskOffering(name, displayText, sortKey, displayDiskOffering, tags, cacheMode) || shouldUpdateIopsRateParameters(iopsReadRate, iopsReadRateMax, iopsReadRateMaxLength, iopsWriteRate, iopsWriteRateMax, iopsWriteRateMaxLength) || shouldUpdateBytesRateParameters(bytesReadRate, bytesReadRateMax, bytesReadRateMaxLength, bytesWriteRate, bytesWriteRateMax, bytesWriteRateMaxLength);
    final boolean detailsUpdateNeeded = !filteredDomainIds.equals(existingDomainIds) || !filteredZoneIds.equals(existingZoneIds);
    if (!updateNeeded && !detailsUpdateNeeded) {
        return _diskOfferingDao.findById(diskOfferingId);
    }
    final DiskOfferingVO diskOffering = _diskOfferingDao.createForUpdate(diskOfferingId);
    if (name != null) {
        diskOffering.setName(name);
    }
    if (displayText != null) {
        diskOffering.setDisplayText(displayText);
    }
    if (sortKey != null) {
        diskOffering.setSortKey(sortKey);
    }
    if (displayDiskOffering != null) {
        diskOffering.setDisplayOffering(displayDiskOffering);
    }
    updateOfferingTagsIfIsNotNull(tags, diskOffering);
    validateMaxRateEqualsOrGreater(iopsReadRate, iopsReadRateMax, IOPS_READ_RATE);
    validateMaxRateEqualsOrGreater(iopsWriteRate, iopsWriteRateMax, IOPS_WRITE_RATE);
    validateMaxRateEqualsOrGreater(bytesReadRate, bytesReadRateMax, BYTES_READ_RATE);
    validateMaxRateEqualsOrGreater(bytesWriteRate, bytesWriteRateMax, BYTES_WRITE_RATE);
    validateMaximumIopsAndBytesLength(iopsReadRateMaxLength, iopsWriteRateMaxLength, bytesReadRateMaxLength, bytesWriteRateMaxLength);
    setBytesRate(diskOffering, bytesReadRate, bytesReadRateMax, bytesReadRateMaxLength, bytesWriteRate, bytesWriteRateMax, bytesWriteRateMaxLength);
    setIopsRate(diskOffering, iopsReadRate, iopsReadRateMax, iopsReadRateMaxLength, iopsWriteRate, iopsWriteRateMax, iopsWriteRateMaxLength);
    if (cacheMode != null) {
        validateCacheMode(cacheMode);
        diskOffering.setCacheMode(DiskOffering.DiskCacheMode.valueOf(cacheMode.toUpperCase()));
    }
    if (updateNeeded && !_diskOfferingDao.update(diskOfferingId, diskOffering)) {
        return null;
    }
    List<DiskOfferingDetailVO> detailsVO = new ArrayList<>();
    if (detailsUpdateNeeded) {
        SearchBuilder<DiskOfferingDetailVO> sb = diskOfferingDetailsDao.createSearchBuilder();
        sb.and("offeringId", sb.entity().getResourceId(), SearchCriteria.Op.EQ);
        sb.and("detailName", sb.entity().getName(), SearchCriteria.Op.EQ);
        sb.done();
        SearchCriteria<DiskOfferingDetailVO> sc = sb.create();
        sc.setParameters("offeringId", String.valueOf(diskOfferingId));
        if (!filteredDomainIds.equals(existingDomainIds)) {
            sc.setParameters("detailName", ApiConstants.DOMAIN_ID);
            diskOfferingDetailsDao.remove(sc);
            for (Long domainId : filteredDomainIds) {
                detailsVO.add(new DiskOfferingDetailVO(diskOfferingId, ApiConstants.DOMAIN_ID, String.valueOf(domainId), false));
            }
        }
        if (!filteredZoneIds.equals(existingZoneIds)) {
            sc.setParameters("detailName", ApiConstants.ZONE_ID);
            diskOfferingDetailsDao.remove(sc);
            for (Long zoneId : filteredZoneIds) {
                detailsVO.add(new DiskOfferingDetailVO(diskOfferingId, ApiConstants.ZONE_ID, String.valueOf(zoneId), false));
            }
        }
    }
    if (!detailsVO.isEmpty()) {
        for (DiskOfferingDetailVO detailVO : detailsVO) {
            diskOfferingDetailsDao.persist(detailVO);
        }
    }
    CallContext.current().setEventDetails("Disk offering id=" + diskOffering.getId());
    return _diskOfferingDao.findById(diskOfferingId);
}
Also used : Account(com.cloud.user.Account) DiskOffering(com.cloud.offering.DiskOffering) User(com.cloud.user.User) ArrayList(java.util.ArrayList) DiskOfferingDetailVO(org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) Domain(com.cloud.domain.Domain) ActionEvent(com.cloud.event.ActionEvent)

Example 3 with DiskOfferingDetailVO

use of org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO in project cloudstack by apache.

the class VolumeOrchestrator method allocateRawVolume.

@Override
public DiskProfile allocateRawVolume(Type type, String name, DiskOffering offering, Long size, Long minIops, Long maxIops, VirtualMachine vm, VirtualMachineTemplate template, Account owner, Long deviceId) {
    if (size == null) {
        size = offering.getDiskSize();
    } else {
        size = (size * 1024 * 1024 * 1024);
    }
    minIops = minIops != null ? minIops : offering.getMinIops();
    maxIops = maxIops != null ? maxIops : offering.getMaxIops();
    VolumeVO vol = new VolumeVO(type, name, vm.getDataCenterId(), owner.getDomainId(), owner.getId(), offering.getId(), offering.getProvisioningType(), size, minIops, maxIops, null);
    if (vm != null) {
        vol.setInstanceId(vm.getId());
    }
    if (deviceId != null) {
        vol.setDeviceId(deviceId);
    } else if (type.equals(Type.ROOT)) {
        vol.setDeviceId(0l);
    } else {
        vol.setDeviceId(1l);
    }
    if (template.getFormat() == ImageFormat.ISO) {
        vol.setIsoId(template.getId());
    } else if (template.getTemplateType().equals(Storage.TemplateType.DATADISK)) {
        vol.setTemplateId(template.getId());
    }
    // display flag matters only for the User vms
    if (vm.getType() == VirtualMachine.Type.User) {
        UserVmVO userVm = _userVmDao.findById(vm.getId());
        vol.setDisplayVolume(userVm.isDisplayVm());
    }
    vol.setFormat(getSupportedImageFormatForCluster(vm.getHypervisorType()));
    vol = _volsDao.persist(vol);
    List<VolumeDetailVO> volumeDetailsVO = new ArrayList<VolumeDetailVO>();
    DiskOfferingDetailVO bandwidthLimitDetail = _diskOfferingDetailDao.findDetail(offering.getId(), Volume.BANDWIDTH_LIMIT_IN_MBPS);
    if (bandwidthLimitDetail != null) {
        volumeDetailsVO.add(new VolumeDetailVO(vol.getId(), Volume.BANDWIDTH_LIMIT_IN_MBPS, bandwidthLimitDetail.getValue(), false));
    }
    DiskOfferingDetailVO iopsLimitDetail = _diskOfferingDetailDao.findDetail(offering.getId(), Volume.IOPS_LIMIT);
    if (iopsLimitDetail != null) {
        volumeDetailsVO.add(new VolumeDetailVO(vol.getId(), Volume.IOPS_LIMIT, iopsLimitDetail.getValue(), false));
    }
    if (!volumeDetailsVO.isEmpty()) {
        _volDetailDao.saveDetails(volumeDetailsVO);
    }
    // Save usage event and update resource count for user vm volumes
    if (vm.getType() == VirtualMachine.Type.User) {
        UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_CREATE, vol.getAccountId(), vol.getDataCenterId(), vol.getId(), vol.getName(), offering.getId(), null, size, Volume.class.getName(), vol.getUuid(), vol.isDisplayVolume());
        _resourceLimitMgr.incrementResourceCount(vm.getAccountId(), ResourceType.volume, vol.isDisplayVolume());
        _resourceLimitMgr.incrementResourceCount(vm.getAccountId(), ResourceType.primary_storage, vol.isDisplayVolume(), new Long(vol.getSize()));
    }
    return toDiskProfile(vol, offering);
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VolumeVO(com.cloud.storage.VolumeVO) VmWorkMigrateVolume(com.cloud.vm.VmWorkMigrateVolume) VmWorkAttachVolume(com.cloud.vm.VmWorkAttachVolume) Volume(com.cloud.storage.Volume) ArrayList(java.util.ArrayList) VolumeDetailVO(com.cloud.storage.VolumeDetailVO) DiskOfferingDetailVO(org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO)

Example 4 with DiskOfferingDetailVO

use of org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO in project cloudstack by apache.

the class ConfigurationManagerImpl method createDiskOfferingInternal.

private DiskOfferingVO createDiskOfferingInternal(final long userId, final boolean isSystem, final VirtualMachine.Type vmType, final String name, final Integer cpu, final Integer ramSize, final Integer speed, final String displayText, final ProvisioningType typedProvisioningType, final boolean localStorageRequired, final boolean offerHA, final boolean limitResourceUse, final boolean volatileVm, String tags, final List<Long> domainIds, List<Long> zoneIds, final String hostTag, final Integer networkRate, final String deploymentPlanner, final Map<String, String> details, Long rootDiskSizeInGiB, final Boolean isCustomizedIops, Long minIops, Long maxIops, Long bytesReadRate, Long bytesReadRateMax, Long bytesReadRateMaxLength, Long bytesWriteRate, Long bytesWriteRateMax, Long bytesWriteRateMaxLength, Long iopsReadRate, Long iopsReadRateMax, Long iopsReadRateMaxLength, Long iopsWriteRate, Long iopsWriteRateMax, Long iopsWriteRateMaxLength, final Integer hypervisorSnapshotReserve, String cacheMode, final Long storagePolicyID) {
    DiskOfferingVO diskOffering = new DiskOfferingVO(name, displayText, typedProvisioningType, false, tags, false, localStorageRequired, false);
    if (Boolean.TRUE.equals(isCustomizedIops) || isCustomizedIops == null) {
        minIops = null;
        maxIops = null;
    } else {
        if (minIops == null && maxIops == null) {
            minIops = 0L;
            maxIops = 0L;
        } else {
            if (minIops == null || minIops <= 0) {
                throw new InvalidParameterValueException("The min IOPS must be greater than 0.");
            }
            if (maxIops == null) {
                maxIops = 0L;
            }
            if (minIops > maxIops) {
                throw new InvalidParameterValueException("The min IOPS must be less than or equal to the max IOPS.");
            }
        }
    }
    if (rootDiskSizeInGiB != null && rootDiskSizeInGiB <= 0L) {
        throw new InvalidParameterValueException(String.format("The Root disk size is of %s GB but it must be greater than 0.", rootDiskSizeInGiB));
    } else if (rootDiskSizeInGiB != null) {
        long maxVolumeSizeInGb = VolumeOrchestrationService.MaxVolumeSize.value();
        if (rootDiskSizeInGiB > maxVolumeSizeInGb) {
            throw new InvalidParameterValueException(String.format("The maximum size for a disk is %d GB.", maxVolumeSizeInGb));
        }
        long rootDiskSizeInBytes = rootDiskSizeInGiB * GiB_TO_BYTES;
        diskOffering.setDiskSize(rootDiskSizeInBytes);
    }
    diskOffering.setCustomizedIops(isCustomizedIops);
    diskOffering.setMinIops(minIops);
    diskOffering.setMaxIops(maxIops);
    setBytesRate(diskOffering, bytesReadRate, bytesReadRateMax, bytesReadRateMaxLength, bytesWriteRate, bytesWriteRateMax, bytesWriteRateMaxLength);
    setIopsRate(diskOffering, iopsReadRate, iopsReadRateMax, iopsReadRateMaxLength, iopsWriteRate, iopsWriteRateMax, iopsWriteRateMaxLength);
    if (cacheMode != null) {
        diskOffering.setCacheMode(DiskOffering.DiskCacheMode.valueOf(cacheMode.toUpperCase()));
    }
    if (hypervisorSnapshotReserve != null && hypervisorSnapshotReserve < 0) {
        throw new InvalidParameterValueException("If provided, Hypervisor Snapshot Reserve must be greater than or equal to 0.");
    }
    diskOffering.setHypervisorSnapshotReserve(hypervisorSnapshotReserve);
    if ((diskOffering = _diskOfferingDao.persist(diskOffering)) != null) {
        if (details != null && !details.isEmpty()) {
            List<DiskOfferingDetailVO> diskDetailsVO = new ArrayList<DiskOfferingDetailVO>();
            // Support disk offering details for below parameters
            if (details.containsKey(Volume.BANDWIDTH_LIMIT_IN_MBPS)) {
                diskDetailsVO.add(new DiskOfferingDetailVO(diskOffering.getId(), Volume.BANDWIDTH_LIMIT_IN_MBPS, details.get(Volume.BANDWIDTH_LIMIT_IN_MBPS), false));
            }
            if (details.containsKey(Volume.IOPS_LIMIT)) {
                diskDetailsVO.add(new DiskOfferingDetailVO(diskOffering.getId(), Volume.IOPS_LIMIT, details.get(Volume.IOPS_LIMIT), false));
            }
            if (!diskDetailsVO.isEmpty()) {
                diskOfferingDetailsDao.saveDetails(diskDetailsVO);
            }
        }
    } else {
        return null;
    }
    return diskOffering;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) ArrayList(java.util.ArrayList) DiskOfferingDetailVO(org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO)

Example 5 with DiskOfferingDetailVO

use of org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO in project cloudstack by apache.

the class ConfigurationManagerImpl method createDiskOffering.

protected DiskOfferingVO createDiskOffering(final Long userId, final List<Long> domainIds, final List<Long> zoneIds, final String name, final String description, final String provisioningType, final Long numGibibytes, String tags, boolean isCustomized, final boolean localStorageRequired, final boolean isDisplayOfferingEnabled, final Boolean isCustomizedIops, Long minIops, Long maxIops, Long bytesReadRate, Long bytesReadRateMax, Long bytesReadRateMaxLength, Long bytesWriteRate, Long bytesWriteRateMax, Long bytesWriteRateMaxLength, Long iopsReadRate, Long iopsReadRateMax, Long iopsReadRateMaxLength, Long iopsWriteRate, Long iopsWriteRateMax, Long iopsWriteRateMaxLength, final Integer hypervisorSnapshotReserve, String cacheMode, final Map<String, String> details, final Long storagePolicyID, final boolean diskSizeStrictness) {
    // special case for custom disk offerings
    long diskSize = 0;
    long maxVolumeSizeInGb = VolumeOrchestrationService.MaxVolumeSize.value();
    if (numGibibytes != null && numGibibytes <= 0) {
        throw new InvalidParameterValueException("Please specify a disk size of at least 1 GB.");
    } else if (numGibibytes != null && numGibibytes > maxVolumeSizeInGb) {
        throw new InvalidParameterValueException(String.format("The maximum size for a disk is %d GB.", maxVolumeSizeInGb));
    }
    final ProvisioningType typedProvisioningType = ProvisioningType.getProvisioningType(provisioningType);
    if (numGibibytes != null) {
        diskSize = numGibibytes * 1024 * 1024 * 1024;
    }
    if (diskSize == 0) {
        isCustomized = true;
    }
    if (Boolean.TRUE.equals(isCustomizedIops) || isCustomizedIops == null) {
        minIops = null;
        maxIops = null;
    } else {
        if (minIops == null && maxIops == null) {
            minIops = 0L;
            maxIops = 0L;
        } else {
            if (minIops == null || minIops <= 0) {
                throw new InvalidParameterValueException("The min IOPS must be greater than 0.");
            }
            if (maxIops == null) {
                maxIops = 0L;
            }
            if (minIops > maxIops) {
                throw new InvalidParameterValueException("The min IOPS must be less than or equal to the max IOPS.");
            }
        }
    }
    // Filter child domains when both parent and child domains are present
    List<Long> filteredDomainIds = filterChildSubDomains(domainIds);
    // Check if user exists in the system
    final User user = _userDao.findById(userId);
    if (user == null || user.getRemoved() != null) {
        throw new InvalidParameterValueException("Unable to find active user by id " + userId);
    }
    final Account account = _accountDao.findById(user.getAccountId());
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
        if (filteredDomainIds.isEmpty()) {
            throw new InvalidParameterValueException(String.format("Unable to create public disk offering by admin: %s because it is domain-admin", user.getUuid()));
        }
        if (tags != null) {
            throw new InvalidParameterValueException(String.format("Unable to create disk offering with storage tags by admin: %s because it is domain-admin", user.getUuid()));
        }
        for (Long domainId : filteredDomainIds) {
            if (domainId == null || !_domainDao.isChildDomain(account.getDomainId(), domainId)) {
                throw new InvalidParameterValueException(String.format("Unable to create disk offering by another domain-admin: %s for domain: %s", user.getUuid(), _entityMgr.findById(Domain.class, domainId).getUuid()));
            }
        }
    } else if (account.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        throw new InvalidParameterValueException(String.format("Unable to create disk offering by user: %s because it is not root-admin or domain-admin", user.getUuid()));
    }
    tags = com.cloud.utils.StringUtils.cleanupTags(tags);
    final DiskOfferingVO newDiskOffering = new DiskOfferingVO(name, description, typedProvisioningType, diskSize, tags, isCustomized, isCustomizedIops, minIops, maxIops);
    newDiskOffering.setUseLocalStorage(localStorageRequired);
    newDiskOffering.setDisplayOffering(isDisplayOfferingEnabled);
    setBytesRate(newDiskOffering, bytesReadRate, bytesReadRateMax, bytesReadRateMaxLength, bytesWriteRate, bytesWriteRateMax, bytesWriteRateMaxLength);
    setIopsRate(newDiskOffering, iopsReadRate, iopsReadRateMax, iopsReadRateMaxLength, iopsWriteRate, iopsWriteRateMax, iopsWriteRateMaxLength);
    if (cacheMode != null) {
        newDiskOffering.setCacheMode(DiskOffering.DiskCacheMode.valueOf(cacheMode.toUpperCase()));
    }
    if (hypervisorSnapshotReserve != null && hypervisorSnapshotReserve < 0) {
        throw new InvalidParameterValueException("If provided, Hypervisor Snapshot Reserve must be greater than or equal to 0.");
    }
    newDiskOffering.setHypervisorSnapshotReserve(hypervisorSnapshotReserve);
    newDiskOffering.setDiskSizeStrictness(diskSizeStrictness);
    CallContext.current().setEventDetails("Disk offering id=" + newDiskOffering.getId());
    final DiskOfferingVO offering = _diskOfferingDao.persist(newDiskOffering);
    if (offering != null) {
        List<DiskOfferingDetailVO> detailsVO = new ArrayList<>();
        for (Long domainId : filteredDomainIds) {
            detailsVO.add(new DiskOfferingDetailVO(offering.getId(), ApiConstants.DOMAIN_ID, String.valueOf(domainId), false));
        }
        if (CollectionUtils.isNotEmpty(zoneIds)) {
            for (Long zoneId : zoneIds) {
                detailsVO.add(new DiskOfferingDetailVO(offering.getId(), ApiConstants.ZONE_ID, String.valueOf(zoneId), false));
            }
        }
        if (details != null && !details.isEmpty()) {
            // Support disk offering details for below parameters
            if (details.containsKey(Volume.BANDWIDTH_LIMIT_IN_MBPS)) {
                detailsVO.add(new DiskOfferingDetailVO(offering.getId(), Volume.BANDWIDTH_LIMIT_IN_MBPS, details.get(Volume.BANDWIDTH_LIMIT_IN_MBPS), false));
            }
            if (details.containsKey(Volume.IOPS_LIMIT)) {
                detailsVO.add(new DiskOfferingDetailVO(offering.getId(), Volume.IOPS_LIMIT, details.get(Volume.IOPS_LIMIT), false));
            }
        }
        if (storagePolicyID != null) {
            detailsVO.add(new DiskOfferingDetailVO(offering.getId(), ApiConstants.STORAGE_POLICY, String.valueOf(storagePolicyID), false));
        }
        if (!detailsVO.isEmpty()) {
            diskOfferingDetailsDao.saveDetails(detailsVO);
        }
        CallContext.current().setEventDetails("Disk offering id=" + newDiskOffering.getId());
        return offering;
    }
    return null;
}
Also used : ProvisioningType(com.cloud.storage.Storage.ProvisioningType) Account(com.cloud.user.Account) User(com.cloud.user.User) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) ArrayList(java.util.ArrayList) DiskOfferingDetailVO(org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO) Domain(com.cloud.domain.Domain)

Aggregations

DiskOfferingDetailVO (org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO)7 ArrayList (java.util.ArrayList)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 DiskOfferingVO (com.cloud.storage.DiskOfferingVO)3 Account (com.cloud.user.Account)3 UserVmVO (com.cloud.vm.UserVmVO)3 VmWorkAttachVolume (com.cloud.vm.VmWorkAttachVolume)3 VmWorkMigrateVolume (com.cloud.vm.VmWorkMigrateVolume)3 Domain (com.cloud.domain.Domain)2 ActionEvent (com.cloud.event.ActionEvent)2 Volume (com.cloud.storage.Volume)2 VolumeDetailVO (com.cloud.storage.VolumeDetailVO)2 VolumeVO (com.cloud.storage.VolumeVO)2 User (com.cloud.user.User)2 DataCenterVO (com.cloud.dc.DataCenterVO)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 DiskOffering (com.cloud.offering.DiskOffering)1 ProvisioningType (com.cloud.storage.Storage.ProvisioningType)1 DB (com.cloud.utils.db.DB)1 VmWorkDetachVolume (com.cloud.vm.VmWorkDetachVolume)1