Search in sources :

Example 1 with StorageProvisioningType

use of com.cloud.model.enumeration.StorageProvisioningType in project cosmic by MissionCriticalCloud.

the class ConfigurationManagerImpl method createServiceOffering.

protected ServiceOfferingVO createServiceOffering(final long userId, final boolean isSystem, final VirtualMachineType vmType, final String name, final Integer cpu, final Integer ramSize, final String displayText, final String provisioningType, final boolean localStorageRequired, final boolean offerHA, final boolean limitResourceUse, final boolean volatileVm, String tags, final Long domainId, final String hostTag, final Integer networkRate, final String deploymentPlanner, final Map<String, String> details, final Boolean isCustomizedIops, Long minIops, Long maxIops, Long bytesReadRate, Long bytesWriteRate, Long iopsReadRate, Long iopsWriteRate, Long iopsTotalRate, final boolean iopsRatePerGb, final Integer hypervisorSnapshotReserve) {
    // 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 (domainId == null) {
            throw new InvalidParameterValueException("Unable to create public service offering by id " + userId + " because it is domain-admin");
        }
        if (tags != null || hostTag != null) {
            throw new InvalidParameterValueException("Unable to create service offering with storage tags or host tags by id " + userId + " because it is domain-admin");
        }
        if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
            throw new InvalidParameterValueException("Unable to create service offering by another domain admin with id " + userId);
        }
    } else if (account.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        throw new InvalidParameterValueException("Unable to create service offering by id " + userId + " because it is not root-admin or domain-admin");
    }
    final StorageProvisioningType typedProvisioningType = StorageProvisioningType.valueOf(provisioningType);
    tags = StringUtils.cleanupTags(tags);
    ServiceOfferingVO offering = new ServiceOfferingVO(name, cpu, ramSize, networkRate, null, offerHA, limitResourceUse, volatileVm, displayText, typedProvisioningType, localStorageRequired, false, tags, isSystem, vmType, domainId, hostTag, deploymentPlanner);
    if (isCustomizedIops != null) {
        bytesReadRate = null;
        bytesWriteRate = null;
        iopsReadRate = null;
        iopsWriteRate = null;
        if (isCustomizedIops) {
            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.");
                }
            }
        }
    } else {
        minIops = null;
        maxIops = null;
    }
    offering.setCustomizedIops(isCustomizedIops);
    offering.setMinIops(minIops);
    offering.setMaxIops(maxIops);
    if (bytesReadRate != null && bytesReadRate > 0) {
        offering.setBytesReadRate(bytesReadRate);
    }
    if (bytesWriteRate != null && bytesWriteRate > 0) {
        offering.setBytesWriteRate(bytesWriteRate);
    }
    if (iopsReadRate != null && iopsReadRate > 0) {
        offering.setIopsReadRate(iopsReadRate);
    }
    if (iopsWriteRate != null && iopsWriteRate > 0) {
        offering.setIopsWriteRate(iopsWriteRate);
    }
    if (iopsTotalRate != null && iopsTotalRate > 0) {
        if (iopsWriteRate != null && iopsWriteRate > 0 || iopsReadRate != null && iopsReadRate > 0) {
            throw new InvalidParameterValueException("Total IOPS rate cannot be used together with IOPS read rate or IOPS write rate");
        }
        offering.setIopsTotalRate(iopsTotalRate);
    }
    if (iopsRatePerGb) {
        offering.setIopsRatePerGb(iopsRatePerGb);
    }
    if (hypervisorSnapshotReserve != null && hypervisorSnapshotReserve < 0) {
        throw new InvalidParameterValueException("If provided, Hypervisor Snapshot Reserve must be greater than or equal to 0.");
    }
    offering.setHypervisorSnapshotReserve(hypervisorSnapshotReserve);
    List<ServiceOfferingDetailsVO> detailsVO = null;
    if (details != null) {
        // To have correct input, either both gpu card name and VGPU type should be passed or nothing should be passed.
        // Use XOR condition to verify that.
        final boolean entry1 = details.containsKey(GPU.Keys.pciDevice.toString());
        final boolean entry2 = details.containsKey(GPU.Keys.vgpuType.toString());
        if ((entry1 || entry2) && !(entry1 && entry2)) {
            throw new InvalidParameterValueException("Please specify the pciDevice and vgpuType correctly.");
        }
        detailsVO = new ArrayList<>();
        for (final Entry<String, String> detailEntry : details.entrySet()) {
            if (detailEntry.getKey().equals(GPU.Keys.pciDevice.toString())) {
                if (detailEntry.getValue() == null) {
                    throw new InvalidParameterValueException("Please specify a GPU Card.");
                }
            }
            if (detailEntry.getKey().equals(GPU.Keys.vgpuType.toString())) {
                if (detailEntry.getValue() == null) {
                    throw new InvalidParameterValueException("vGPUType value cannot be null");
                }
            }
            detailsVO.add(new ServiceOfferingDetailsVO(offering.getId(), detailEntry.getKey(), detailEntry.getValue(), true));
        }
    }
    if ((offering = _serviceOfferingDao.persist(offering)) != null) {
        if (detailsVO != null && !detailsVO.isEmpty()) {
            for (int index = 0; index < detailsVO.size(); index++) {
                detailsVO.get(index).setResourceId(offering.getId());
            }
            _serviceOfferingDetailsDao.saveDetails(detailsVO);
        }
        CallContext.current().setEventDetails("Service offering id=" + offering.getId());
        return offering;
    } else {
        return null;
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) User(com.cloud.legacymodel.user.User) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ServiceOfferingDetailsVO(com.cloud.service.ServiceOfferingDetailsVO) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) StorageProvisioningType(com.cloud.model.enumeration.StorageProvisioningType)

Example 2 with StorageProvisioningType

use of com.cloud.model.enumeration.StorageProvisioningType in project cosmic by MissionCriticalCloud.

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(final CreateVolumeCmd cmd) throws ResourceAllocationException {
    // FIXME: some of the scheduled event stuff might be missing here...
    final Account caller = CallContext.current().getCallingAccount();
    final long ownerId = cmd.getEntityOwnerId();
    final Account owner = this._accountMgr.getActiveAccountById(ownerId);
    Boolean displayVolume = cmd.getDisplayVolume();
    // permission check
    this._accountMgr.checkAccess(caller, null, true, this._accountMgr.getActiveAccountById(ownerId));
    if (displayVolume == null) {
        displayVolume = true;
    } else {
        if (!this._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
    this._resourceLimitMgr.checkResourceLimit(owner, ResourceType.volume, displayVolume);
    Long zoneId = cmd.getZoneId();
    final Long diskOfferingId;
    final DiskOfferingVO diskOffering;
    final StorageProvisioningType provisioningType;
    Long size;
    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 || cmd.getSnapshotId() != null && cmd.getDiskOfferingId() != null) {
        throw new InvalidParameterValueException("Either disk Offering Id or snapshot Id must be passed whilst creating volume");
    }
    if (cmd.getSnapshotId() == null) {
        // create a new volume
        diskOfferingId = cmd.getDiskOfferingId();
        size = cmd.getSize();
        final 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 = this._diskOfferingDao.findById(diskOfferingId);
        if (diskOffering == null || diskOffering.getRemoved() != null || !DiskOfferingVO.Type.Disk.equals(diskOffering.getType())) {
            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");
            }
            final Long customDiskOfferingMaxSize = this._volumeMgr.CustomDiskOfferingMaxSize.value();
            final Long customDiskOfferingMinSize = this._volumeMgr.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");
        }
        if (diskOffering.getDomainId() == null) {
        // do nothing as offering is public
        } else {
            this._configMgr.checkDiskOfferingAccess(caller, diskOffering);
        }
        if (diskOffering.getDiskSize() > 0) {
            size = diskOffering.getDiskSize();
        }
        final 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();
            }
        }
        provisioningType = diskOffering.getProvisioningType();
        if (!validateVolumeSizeRange(size)) {
            // for validation
            throw new InvalidParameterValueException("Invalid size for custom volume creation: " + size + " ,max volume size is:" + this._maxVolumeSizeInGb);
        }
    } else {
        // create volume from snapshot
        final Long snapshotId = cmd.getSnapshotId();
        final SnapshotVO snapshotCheck = this._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");
        }
        parentVolume = this._volsDao.findByIdIncludingRemoved(snapshotCheck.getVolumeId());
        diskOfferingId = snapshotCheck.getDiskOfferingId();
        diskOffering = this._diskOfferingDao.findById(diskOfferingId);
        if (zoneId == null) {
            // if zoneId is not provided, we default to create volume in the same zone as the snapshot zone.
            zoneId = snapshotCheck.getDataCenterId();
        }
        // ; disk offering is used for tags
        size = snapshotCheck.getSize();
        // purposes
        minIops = snapshotCheck.getMinIops();
        maxIops = snapshotCheck.getMaxIops();
        provisioningType = diskOffering.getProvisioningType();
        // check snapshot permissions
        this._accountMgr.checkAccess(caller, null, true, snapshotCheck);
        // one step operation - create volume in VM's cluster and attach it
        // to the VM
        final Long vmId = cmd.getVirtualMachineId();
        if (vmId != null) {
            // Check that the virtual machine ID is valid and it's a user vm
            final UserVmVO vm = this._userVmDao.findById(vmId);
            if (vm == null || vm.getType() != VirtualMachineType.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
            this._accountMgr.checkAccess(caller, null, false, vm);
        }
    }
    // Check that the resource limit for primary storage won't be exceeded
    this._resourceLimitMgr.checkResourceLimit(owner, ResourceType.primary_storage, displayVolume, new Long(size));
    // Verify that zone exists
    final DataCenterVO zone = this._dcDao.findById(zoneId);
    if (zone == null) {
        throw new InvalidParameterValueException("Unable to find zone by id " + zoneId);
    }
    // Check if zone is disabled
    if (AllocationState.Disabled == zone.getAllocationState() && !this._accountMgr.isRootAdmin(caller.getId())) {
        throw new PermissionDeniedException("Cannot perform this operation, Zone is currently disabled: " + zoneId);
    }
    final String userSpecifiedName = getVolumeNameFromCommand(cmd);
    DiskControllerType diskControllerType = getDiskControllerType();
    if (cmd.getDiskController() != null) {
        diskControllerType = DiskControllerType.valueOf(cmd.getDiskController().toUpperCase());
    }
    ImageFormat fileFormat = ImageFormat.QCOW2;
    if (cmd.getFileFormat() != null) {
        fileFormat = ImageFormat.valueOf(cmd.getFileFormat().toUpperCase());
    }
    return commitVolume(cmd, caller, owner, displayVolume, zoneId, diskOfferingId, provisioningType, size, minIops, maxIops, parentVolume, userSpecifiedName, this._uuidMgr.generateUuid(Volume.class, cmd.getCustomId()), diskControllerType, fileFormat);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) Account(com.cloud.legacymodel.user.Account) UserVmVO(com.cloud.vm.UserVmVO) StorageProvisioningType(com.cloud.model.enumeration.StorageProvisioningType) ImageFormat(com.cloud.model.enumeration.ImageFormat) DiskControllerType(com.cloud.model.enumeration.DiskControllerType) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Volume(com.cloud.legacymodel.storage.Volume) 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) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 3 with StorageProvisioningType

use of com.cloud.model.enumeration.StorageProvisioningType in project cosmic by MissionCriticalCloud.

the class ConfigurationManagerImpl method createDiskOffering.

protected DiskOfferingVO createDiskOffering(final Long userId, final Long domainId, 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 bytesWriteRate, Long iopsReadRate, Long iopsWriteRate, Long iopsTotalRate, boolean iopsRatePerGb, final Integer hypervisorSnapshotReserve) {
    // special case for custom disk offerings
    long diskSize = 0;
    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("The maximum size for a disk is " + _maxVolumeSizeInGb + " Gb.");
    }
    final StorageProvisioningType typedProvisioningType = StorageProvisioningType.valueOf(provisioningType);
    if (numGibibytes != null) {
        diskSize = numGibibytes * 1024 * 1024 * 1024;
    }
    if (diskSize == 0) {
        isCustomized = true;
    }
    if (isCustomizedIops != null) {
        bytesReadRate = null;
        bytesWriteRate = null;
        iopsReadRate = null;
        iopsWriteRate = null;
        if (isCustomizedIops) {
            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.");
                }
            }
        }
    } else {
        minIops = null;
        maxIops = null;
    }
    // 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 (domainId == null) {
            throw new InvalidParameterValueException("Unable to create public disk offering by id " + userId + " because it is domain-admin");
        }
        if (tags != null) {
            throw new InvalidParameterValueException("Unable to create disk offering with storage tags by id " + userId + " because it is domain-admin");
        }
        if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
            throw new InvalidParameterValueException("Unable to create disk offering by another domain admin with id " + userId);
        }
    } else if (account.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        throw new InvalidParameterValueException("Unable to create disk offering by id " + userId + " because it is not root-admin or domain-admin");
    }
    tags = StringUtils.cleanupTags(tags);
    final DiskOfferingVO newDiskOffering = new DiskOfferingVO(domainId, name, description, typedProvisioningType, diskSize, tags, isCustomized, isCustomizedIops, minIops, maxIops);
    newDiskOffering.setUseLocalStorage(localStorageRequired);
    newDiskOffering.setDisplayOffering(isDisplayOfferingEnabled);
    if (bytesReadRate != null && bytesReadRate > 0) {
        newDiskOffering.setBytesReadRate(bytesReadRate);
    }
    if (bytesWriteRate != null && bytesWriteRate > 0) {
        newDiskOffering.setBytesWriteRate(bytesWriteRate);
    }
    if (iopsReadRate != null && iopsReadRate > 0) {
        newDiskOffering.setIopsReadRate(iopsReadRate);
    }
    if (iopsWriteRate != null && iopsWriteRate > 0) {
        newDiskOffering.setIopsWriteRate(iopsWriteRate);
    }
    if (iopsTotalRate != null && iopsTotalRate > 0) {
        if (iopsWriteRate != null && iopsWriteRate > 0 || iopsReadRate != null && iopsReadRate > 0) {
            throw new InvalidParameterValueException("Total IOPS rate cannot be used together with IOPS read rate or IOPS write rate");
        }
        newDiskOffering.setIopsTotalRate(iopsTotalRate);
    }
    newDiskOffering.setIopsRatePerGb(iopsRatePerGb);
    if (hypervisorSnapshotReserve != null && hypervisorSnapshotReserve < 0) {
        throw new InvalidParameterValueException("If provided, Hypervisor Snapshot Reserve must be greater than or equal to 0.");
    }
    newDiskOffering.setHypervisorSnapshotReserve(hypervisorSnapshotReserve);
    CallContext.current().setEventDetails("Disk offering id=" + newDiskOffering.getId());
    final DiskOfferingVO offering = _diskOfferingDao.persist(newDiskOffering);
    if (offering != null) {
        CallContext.current().setEventDetails("Disk offering id=" + newDiskOffering.getId());
        return offering;
    } else {
        return null;
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) User(com.cloud.legacymodel.user.User) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) StorageProvisioningType(com.cloud.model.enumeration.StorageProvisioningType)

Aggregations

InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)3 Account (com.cloud.legacymodel.user.Account)3 StorageProvisioningType (com.cloud.model.enumeration.StorageProvisioningType)3 User (com.cloud.legacymodel.user.User)2 DataCenterVO (com.cloud.dc.DataCenterVO)1 ActionEvent (com.cloud.event.ActionEvent)1 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)1 Volume (com.cloud.legacymodel.storage.Volume)1 DiskControllerType (com.cloud.model.enumeration.DiskControllerType)1 ImageFormat (com.cloud.model.enumeration.ImageFormat)1 ServiceOfferingDetailsVO (com.cloud.service.ServiceOfferingDetailsVO)1 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)1 DiskOfferingVO (com.cloud.storage.DiskOfferingVO)1 DB (com.cloud.utils.db.DB)1 UserVmVO (com.cloud.vm.UserVmVO)1 VmWorkAttachVolume (com.cloud.vm.VmWorkAttachVolume)1 VmWorkDetachVolume (com.cloud.vm.VmWorkDetachVolume)1 VmWorkExtractVolume (com.cloud.vm.VmWorkExtractVolume)1 VmWorkMigrateVolume (com.cloud.vm.VmWorkMigrateVolume)1 VmWorkResizeVolume (com.cloud.vm.VmWorkResizeVolume)1