Search in sources :

Example 21 with DiskOfferingVO

use of com.cloud.storage.DiskOfferingVO in project cloudstack by apache.

the class VirtualMachineManagerImpl method getCandidateStoragePoolsToMigrateLocalVolume.

/**
 * We use {@link StoragePoolAllocator} objects to find storage pools for given DataCenterDeployment where we would be able to allocate the given volume.
 */
protected List<StoragePool> getCandidateStoragePoolsToMigrateLocalVolume(VirtualMachineProfile profile, DataCenterDeployment plan, Volume volume) {
    List<StoragePool> poolList = new ArrayList<>();
    DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId());
    DiskProfile diskProfile = new DiskProfile(volume, diskOffering, profile.getHypervisorType());
    ExcludeList avoid = new ExcludeList();
    StoragePoolVO volumeStoragePool = _storagePoolDao.findById(volume.getPoolId());
    if (volumeStoragePool.isLocal()) {
        diskProfile.setUseLocalStorage(true);
    }
    for (StoragePoolAllocator allocator : _storagePoolAllocators) {
        List<StoragePool> poolListFromAllocator = allocator.allocateToPool(diskProfile, profile, plan, avoid, StoragePoolAllocator.RETURN_UPTO_ALL);
        if (CollectionUtils.isEmpty(poolListFromAllocator)) {
            continue;
        }
        for (StoragePool pool : poolListFromAllocator) {
            if (pool.isLocal() || isStorageCrossClusterMigration(plan.getClusterId(), volumeStoragePool)) {
                poolList.add(pool);
            }
        }
    }
    return poolList;
}
Also used : ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) StoragePool(com.cloud.storage.StoragePool) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) ArrayList(java.util.ArrayList) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) StoragePoolAllocator(org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator)

Example 22 with DiskOfferingVO

use of com.cloud.storage.DiskOfferingVO 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 23 with DiskOfferingVO

use of com.cloud.storage.DiskOfferingVO in project cloudstack by apache.

the class ConfigurationManagerImpl method createServiceOffering.

protected ServiceOfferingVO createServiceOffering(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 String provisioningType, 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, final boolean dynamicScalingEnabled, final Long diskOfferingId, final boolean diskOfferingStrictness, final boolean isCustomized) {
    // 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 service offering by admin: %s because it is domain-admin", user.getUuid()));
        }
        if (tags != null || hostTag != null) {
            throw new InvalidParameterValueException(String.format("Unable to create service offering with storage tags or host tags by admin: %s because it is domain-admin", user.getUuid()));
        }
        for (Long domainId : filteredDomainIds) {
            if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
                throw new InvalidParameterValueException(String.format("Unable to create service 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 service offering by user: %s because it is not root-admin or domain-admin", user.getUuid()));
    }
    final ProvisioningType typedProvisioningType = ProvisioningType.getProvisioningType(provisioningType);
    tags = com.cloud.utils.StringUtils.cleanupTags(tags);
    ServiceOfferingVO serviceOffering = new ServiceOfferingVO(name, cpu, ramSize, speed, networkRate, null, offerHA, limitResourceUse, volatileVm, displayText, isSystem, vmType, hostTag, deploymentPlanner, dynamicScalingEnabled, isCustomized);
    List<ServiceOfferingDetailsVO> detailsVO = new ArrayList<ServiceOfferingDetailsVO>();
    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.");
        }
        for (final Entry<String, String> detailEntry : details.entrySet()) {
            String detailEntryValue = detailEntry.getValue();
            if (detailEntry.getKey().equals(GPU.Keys.pciDevice.toString())) {
                if (detailEntryValue == null) {
                    throw new InvalidParameterValueException("Please specify a GPU Card.");
                }
            }
            if (detailEntry.getKey().equals(GPU.Keys.vgpuType.toString())) {
                if (detailEntryValue == null) {
                    throw new InvalidParameterValueException("vGPUType value cannot be null");
                }
            }
            if (detailEntry.getKey().startsWith(ApiConstants.EXTRA_CONFIG)) {
                try {
                    detailEntryValue = URLDecoder.decode(detailEntry.getValue(), "UTF-8");
                } catch (UnsupportedEncodingException | IllegalArgumentException e) {
                    s_logger.error("Cannot decode extra configuration value for key: " + detailEntry.getKey() + ", skipping it");
                    continue;
                }
            }
            if (detailEntry.getKey().equalsIgnoreCase(Volume.BANDWIDTH_LIMIT_IN_MBPS) || detailEntry.getKey().equalsIgnoreCase(Volume.IOPS_LIMIT)) {
                // Add in disk offering details
                continue;
            }
            detailsVO.add(new ServiceOfferingDetailsVO(serviceOffering.getId(), detailEntry.getKey(), detailEntryValue, true));
        }
    }
    if (storagePolicyID != null) {
        detailsVO.add(new ServiceOfferingDetailsVO(serviceOffering.getId(), ApiConstants.STORAGE_POLICY, String.valueOf(storagePolicyID), false));
    }
    serviceOffering.setDiskOfferingStrictness(diskOfferingStrictness);
    DiskOfferingVO diskOffering = null;
    if (diskOfferingId == null) {
        diskOffering = createDiskOfferingInternal(userId, isSystem, vmType, name, cpu, ramSize, speed, displayText, typedProvisioningType, localStorageRequired, offerHA, limitResourceUse, volatileVm, tags, domainIds, zoneIds, hostTag, networkRate, deploymentPlanner, details, rootDiskSizeInGiB, isCustomizedIops, minIops, maxIops, bytesReadRate, bytesReadRateMax, bytesReadRateMaxLength, bytesWriteRate, bytesWriteRateMax, bytesWriteRateMaxLength, iopsReadRate, iopsReadRateMax, iopsReadRateMaxLength, iopsWriteRate, iopsWriteRateMax, iopsWriteRateMaxLength, hypervisorSnapshotReserve, cacheMode, storagePolicyID);
    } else {
        diskOffering = _diskOfferingDao.findById(diskOfferingId);
    }
    if (diskOffering != null) {
        serviceOffering.setDiskOfferingId(diskOffering.getId());
    } else {
        return null;
    }
    if ((serviceOffering = _serviceOfferingDao.persist(serviceOffering)) != null) {
        for (Long domainId : filteredDomainIds) {
            detailsVO.add(new ServiceOfferingDetailsVO(serviceOffering.getId(), ApiConstants.DOMAIN_ID, String.valueOf(domainId), false));
        }
        if (CollectionUtils.isNotEmpty(zoneIds)) {
            for (Long zoneId : zoneIds) {
                detailsVO.add(new ServiceOfferingDetailsVO(serviceOffering.getId(), ApiConstants.ZONE_ID, String.valueOf(zoneId), false));
            }
        }
        if (CollectionUtils.isNotEmpty(detailsVO)) {
            for (ServiceOfferingDetailsVO detail : detailsVO) {
                detail.setResourceId(serviceOffering.getId());
            }
            _serviceOfferingDetailsDao.saveDetails(detailsVO);
        }
        CallContext.current().setEventDetails("Service offering id=" + serviceOffering.getId());
        return serviceOffering;
    } else {
        return null;
    }
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) ArrayList(java.util.ArrayList) ServiceOfferingDetailsVO(com.cloud.service.ServiceOfferingDetailsVO) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) ProvisioningType(com.cloud.storage.Storage.ProvisioningType) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) Domain(com.cloud.domain.Domain)

Example 24 with DiskOfferingVO

use of com.cloud.storage.DiskOfferingVO in project cloudstack by apache.

the class ConfigurationManagerImpl method deleteServiceOffering.

@Override
@ActionEvent(eventType = EventTypes.EVENT_SERVICE_OFFERING_DELETE, eventDescription = "deleting service offering")
public boolean deleteServiceOffering(final DeleteServiceOfferingCmd cmd) {
    final Long offeringId = cmd.getId();
    Long userId = CallContext.current().getCallingUserId();
    if (userId == null) {
        userId = Long.valueOf(User.UID_SYSTEM);
    }
    // Verify service offering id
    final ServiceOfferingVO offering = _serviceOfferingDao.findById(offeringId);
    if (offering == null) {
        throw new InvalidParameterValueException("unable to find service offering " + offeringId);
    }
    // Verify disk offering id mapped to the service offering
    final DiskOfferingVO diskOffering = _diskOfferingDao.findById(offering.getDiskOfferingId());
    if (diskOffering == null) {
        throw new InvalidParameterValueException("unable to find disk offering " + offering.getDiskOfferingId() + " mapped to the service offering " + offeringId);
    }
    if (offering.getDefaultUse()) {
        throw new InvalidParameterValueException("Default service offerings cannot be deleted");
    }
    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) {
        List<Long> existingDomainIds = _serviceOfferingDetailsDao.findDomainIds(offeringId);
        if (existingDomainIds.isEmpty()) {
            throw new InvalidParameterValueException(String.format("Unable to delete public service offering: %s by admin: %s because it is domain-admin", offering.getUuid(), user.getUuid()));
        }
        for (Long domainId : existingDomainIds) {
            if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
                throw new InvalidParameterValueException(String.format("Unable to delete service offering: %s as it has linked domain(s) which are not child domain for domain-admin: %s", offering.getUuid(), user.getUuid()));
            }
        }
    } else if (account.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        throw new InvalidParameterValueException(String.format("Unable to delete service offering: %s by user: %s because it is not root-admin or domain-admin", offering.getUuid(), user.getUuid()));
    }
    annotationDao.removeByEntityType(AnnotationService.EntityType.SERVICE_OFFERING.name(), offering.getUuid());
    if (diskOffering.isComputeOnly()) {
        diskOffering.setState(DiskOffering.State.Inactive);
        if (!_diskOfferingDao.update(diskOffering.getId(), diskOffering)) {
            throw new CloudRuntimeException(String.format("Unable to delete disk offering %s mapped to the service offering %s", diskOffering.getUuid(), offering.getUuid()));
        }
    }
    offering.setState(ServiceOffering.State.Inactive);
    if (_serviceOfferingDao.update(offeringId, offering)) {
        CallContext.current().setEventDetails("Service offering id=" + offeringId);
        return true;
    } else {
        return false;
    }
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) ActionEvent(com.cloud.event.ActionEvent)

Example 25 with DiskOfferingVO

use of com.cloud.storage.DiskOfferingVO in project cloudstack by apache.

the class ConfigurationManagerImpl method deleteDiskOffering.

@Override
@ActionEvent(eventType = EventTypes.EVENT_DISK_OFFERING_DELETE, eventDescription = "deleting disk offering")
public boolean deleteDiskOffering(final DeleteDiskOfferingCmd cmd) {
    final Long diskOfferingId = cmd.getId();
    final DiskOfferingVO offering = _diskOfferingDao.findById(diskOfferingId);
    if (offering == null) {
        throw new InvalidParameterValueException("Unable to find disk offering by id " + diskOfferingId);
    }
    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());
    if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
        List<Long> existingDomainIds = diskOfferingDetailsDao.findDomainIds(diskOfferingId);
        if (existingDomainIds.isEmpty()) {
            throw new InvalidParameterValueException(String.format("Unable to delete public disk offering: %s by admin: %s because it is domain-admin", offering.getUuid(), user.getUuid()));
        }
        for (Long domainId : existingDomainIds) {
            if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
                throw new InvalidParameterValueException(String.format("Unable to delete disk offering: %s as it has linked domain(s) which are not child domain for domain-admin: %s", offering.getUuid(), user.getUuid()));
            }
        }
    } else if (account.getType() != Account.ACCOUNT_TYPE_ADMIN) {
        throw new InvalidParameterValueException(String.format("Unable to delete disk offering: %s by user: %s because it is not root-admin or domain-admin", offering.getUuid(), user.getUuid()));
    }
    annotationDao.removeByEntityType(AnnotationService.EntityType.DISK_OFFERING.name(), offering.getUuid());
    offering.setState(DiskOffering.State.Inactive);
    if (_diskOfferingDao.update(offering.getId(), offering)) {
        CallContext.current().setEventDetails("Disk offering id=" + diskOfferingId);
        return true;
    } else {
        return false;
    }
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

DiskOfferingVO (com.cloud.storage.DiskOfferingVO)86 ArrayList (java.util.ArrayList)34 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)32 VolumeVO (com.cloud.storage.VolumeVO)25 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)24 Account (com.cloud.user.Account)23 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)22 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)18 List (java.util.List)16 StoragePool (com.cloud.storage.StoragePool)15 HypervisorType (com.cloud.hypervisor.Hypervisor.HypervisorType)14 DiskProfile (com.cloud.vm.DiskProfile)14 VMInstanceVO (com.cloud.vm.VMInstanceVO)14 HostVO (com.cloud.host.HostVO)13 NetworkVO (com.cloud.network.dao.NetworkVO)13 Pair (com.cloud.utils.Pair)13 DataCenterDeployment (com.cloud.deploy.DataCenterDeployment)12 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)12 User (com.cloud.user.User)12 HashMap (java.util.HashMap)12