Search in sources :

Example 6 with DiskOfferingDetailVO

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

the class VolumeApiServiceImpl method allocVolume.

/*
     * Just allocate a volume in the database, don't send the createvolume cmd
     * to hypervisor. The volume will be finally created only when it's attached
     * to a VM.
     */
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_CREATE, eventDescription = "creating volume", create = true)
public VolumeVO allocVolume(CreateVolumeCmd cmd) throws ResourceAllocationException {
    Account caller = CallContext.current().getCallingAccount();
    long ownerId = cmd.getEntityOwnerId();
    Account owner = _accountMgr.getActiveAccountById(ownerId);
    Boolean displayVolume = cmd.getDisplayVolume();
    // permission check
    _accountMgr.checkAccess(caller, null, true, _accountMgr.getActiveAccountById(ownerId));
    if (displayVolume == null) {
        displayVolume = true;
    } else {
        if (!_accountMgr.isRootAdmin(caller.getId())) {
            throw new PermissionDeniedException("Cannot update parameter displayvolume, only admin permitted ");
        }
    }
    // Check that the resource limit for volumes won't be exceeded
    _resourceLimitMgr.checkResourceLimit(owner, ResourceType.volume, displayVolume);
    Long zoneId = cmd.getZoneId();
    Long diskOfferingId = null;
    DiskOfferingVO diskOffering = null;
    Long size = null;
    Long minIops = null;
    Long maxIops = null;
    // Volume VO used for extracting the source template id
    VolumeVO parentVolume = null;
    // validate input parameters before creating the volume
    if (cmd.getSnapshotId() == null && cmd.getDiskOfferingId() == null) {
        throw new InvalidParameterValueException("At least one of disk Offering ID or snapshot ID must be passed whilst creating volume");
    }
    // disallow passing disk offering ID with DATA disk volume snapshots
    if (cmd.getSnapshotId() != null && cmd.getDiskOfferingId() != null) {
        SnapshotVO snapshot = _snapshotDao.findById(cmd.getSnapshotId());
        if (snapshot != null) {
            parentVolume = _volsDao.findByIdIncludingRemoved(snapshot.getVolumeId());
            if (parentVolume != null && parentVolume.getVolumeType() != Volume.Type.ROOT)
                throw new InvalidParameterValueException("Disk Offering ID cannot be passed whilst creating volume from snapshot other than ROOT disk snapshots");
        }
        parentVolume = null;
    }
    Map<String, String> details = new HashMap<>();
    if (cmd.getDiskOfferingId() != null) {
        // create a new volume
        diskOfferingId = cmd.getDiskOfferingId();
        size = cmd.getSize();
        Long sizeInGB = size;
        if (size != null) {
            if (size > 0) {
                // user specify size in GB
                size = size * 1024 * 1024 * 1024;
            } else {
                throw new InvalidParameterValueException("Disk size must be larger than 0");
            }
        }
        // Check that the the disk offering is specified
        diskOffering = _diskOfferingDao.findById(diskOfferingId);
        if ((diskOffering == null) || diskOffering.getRemoved() != null || diskOffering.isComputeOnly()) {
            throw new InvalidParameterValueException("Please specify a valid disk offering.");
        }
        if (diskOffering.isCustomized()) {
            if (size == null) {
                throw new InvalidParameterValueException("This disk offering requires a custom size specified");
            }
            Long customDiskOfferingMaxSize = VolumeOrchestrationService.CustomDiskOfferingMaxSize.value();
            Long customDiskOfferingMinSize = VolumeOrchestrationService.CustomDiskOfferingMinSize.value();
            if ((sizeInGB < customDiskOfferingMinSize) || (sizeInGB > customDiskOfferingMaxSize)) {
                throw new InvalidParameterValueException("Volume size: " + sizeInGB + "GB is out of allowed range. Max: " + customDiskOfferingMaxSize + " Min:" + customDiskOfferingMinSize);
            }
        }
        if (!diskOffering.isCustomized() && size != null) {
            throw new InvalidParameterValueException("This disk offering does not allow custom size");
        }
        _configMgr.checkDiskOfferingAccess(owner, diskOffering, _dcDao.findById(zoneId));
        if (diskOffering.getDiskSize() > 0) {
            size = diskOffering.getDiskSize();
        }
        DiskOfferingDetailVO bandwidthLimitDetail = _diskOfferingDetailsDao.findDetail(diskOfferingId, Volume.BANDWIDTH_LIMIT_IN_MBPS);
        if (bandwidthLimitDetail != null) {
            details.put(Volume.BANDWIDTH_LIMIT_IN_MBPS, bandwidthLimitDetail.getValue());
        }
        DiskOfferingDetailVO iopsLimitDetail = _diskOfferingDetailsDao.findDetail(diskOfferingId, Volume.IOPS_LIMIT);
        if (iopsLimitDetail != null) {
            details.put(Volume.IOPS_LIMIT, iopsLimitDetail.getValue());
        }
        Boolean isCustomizedIops = diskOffering.isCustomizedIops();
        if (isCustomizedIops != null) {
            if (isCustomizedIops) {
                minIops = cmd.getMinIops();
                maxIops = cmd.getMaxIops();
                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.");
                    }
                }
            } else {
                minIops = diskOffering.getMinIops();
                maxIops = diskOffering.getMaxIops();
            }
        } else {
            minIops = diskOffering.getMinIops();
            maxIops = diskOffering.getMaxIops();
        }
        if (!validateVolumeSizeInBytes(size)) {
            throw new InvalidParameterValueException(String.format("Invalid size for custom volume creation: %s, max volume size is: %s GB", NumbersUtil.toReadableSize(size), VolumeOrchestrationService.MaxVolumeSize.value()));
        }
    }
    if (cmd.getSnapshotId() != null) {
        // create volume from snapshot
        Long snapshotId = cmd.getSnapshotId();
        SnapshotVO snapshotCheck = _snapshotDao.findById(snapshotId);
        if (snapshotCheck == null) {
            throw new InvalidParameterValueException("unable to find a snapshot with id " + snapshotId);
        }
        if (snapshotCheck.getState() != Snapshot.State.BackedUp) {
            throw new InvalidParameterValueException("Snapshot id=" + snapshotId + " is not in " + Snapshot.State.BackedUp + " state yet and can't be used for volume creation");
        }
        SnapshotDataStoreVO snapshotStore = _snapshotDataStoreDao.findBySnapshot(snapshotId, DataStoreRole.Primary);
        if (snapshotStore != null) {
            StoragePoolVO storagePoolVO = _storagePoolDao.findById(snapshotStore.getDataStoreId());
            if (storagePoolVO.getPoolType() == Storage.StoragePoolType.PowerFlex) {
                throw new InvalidParameterValueException("Create volume from snapshot is not supported for PowerFlex volume snapshots");
            }
        }
        parentVolume = _volsDao.findByIdIncludingRemoved(snapshotCheck.getVolumeId());
        if (zoneId == null) {
            // if zoneId is not provided, we default to create volume in the same zone as the snapshot zone.
            zoneId = snapshotCheck.getDataCenterId();
        }
        if (diskOffering == null) {
            // Pure snapshot is being used to create volume.
            diskOfferingId = snapshotCheck.getDiskOfferingId();
            diskOffering = _diskOfferingDao.findById(diskOfferingId);
            minIops = snapshotCheck.getMinIops();
            maxIops = snapshotCheck.getMaxIops();
            // ; disk offering is used for tags purposes
            size = snapshotCheck.getSize();
        } else {
            if (size < snapshotCheck.getSize()) {
                throw new InvalidParameterValueException(String.format("Invalid size for volume creation: %dGB, snapshot size is: %dGB", size / (1024 * 1024 * 1024), snapshotCheck.getSize() / (1024 * 1024 * 1024)));
            }
        }
        _configMgr.checkDiskOfferingAccess(null, diskOffering, _dcDao.findById(zoneId));
        // check snapshot permissions
        _accountMgr.checkAccess(caller, null, true, snapshotCheck);
        // one step operation - create volume in VM's cluster and attach it
        // to the VM
        Long vmId = cmd.getVirtualMachineId();
        if (vmId != null) {
            // Check that the virtual machine ID is valid and it's a user vm
            UserVmVO vm = _userVmDao.findById(vmId);
            if (vm == null || vm.getType() != VirtualMachine.Type.User) {
                throw new InvalidParameterValueException("Please specify a valid User VM.");
            }
            // Check that the VM is in the correct state
            if (vm.getState() != State.Running && vm.getState() != State.Stopped) {
                throw new InvalidParameterValueException("Please specify a VM that is either running or stopped.");
            }
            // permission check
            _accountMgr.checkAccess(caller, null, false, vm);
        }
    }
    Storage.ProvisioningType provisioningType = diskOffering.getProvisioningType();
    // Check that the resource limit for primary storage won't be exceeded
    _resourceLimitMgr.checkResourceLimit(owner, ResourceType.primary_storage, displayVolume, new Long(size));
    // Verify that zone exists
    DataCenterVO zone = _dcDao.findById(zoneId);
    if (zone == null) {
        throw new InvalidParameterValueException("Unable to find zone by id " + zoneId);
    }
    // Check if zone is disabled
    if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(caller.getId())) {
        throw new PermissionDeniedException("Cannot perform this operation, Zone is currently disabled: " + zoneId);
    }
    // offering not allowed
    if (!zone.isLocalStorageEnabled() && diskOffering.isUseLocalStorage()) {
        throw new InvalidParameterValueException("Zone is not configured to use local storage but volume's disk offering " + diskOffering.getName() + " uses it");
    }
    String userSpecifiedName = getVolumeNameFromCommand(cmd);
    return commitVolume(cmd, caller, owner, displayVolume, zoneId, diskOfferingId, provisioningType, size, minIops, maxIops, parentVolume, userSpecifiedName, _uuidMgr.generateUuid(Volume.class, cmd.getCustomId()), details);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.user.Account) UserVmVO(com.cloud.vm.UserVmVO) HashMap(java.util.HashMap) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) DiskOfferingDetailVO(org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VmWorkDetachVolume(com.cloud.vm.VmWorkDetachVolume) VmWorkMigrateVolume(com.cloud.vm.VmWorkMigrateVolume) VmWorkResizeVolume(com.cloud.vm.VmWorkResizeVolume) VmWorkAttachVolume(com.cloud.vm.VmWorkAttachVolume) VmWorkExtractVolume(com.cloud.vm.VmWorkExtractVolume) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 7 with DiskOfferingDetailVO

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

the class DiskOfferingDetailsDaoImpl method getDetail.

@Override
public String getDetail(Long diskOfferingId, String key) {
    String detailValue = null;
    DiskOfferingDetailVO diskOfferingDetail = findDetail(diskOfferingId, key);
    if (diskOfferingDetail != null) {
        detailValue = diskOfferingDetail.getValue();
    }
    return detailValue;
}
Also used : DiskOfferingDetailVO(org.apache.cloudstack.resourcedetail.DiskOfferingDetailVO)

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