use of com.cloud.storage.DiskOfferingVO in project cloudstack by apache.
the class ConfigurationManagerImpl method updateServiceOffering.
@Override
@ActionEvent(eventType = EventTypes.EVENT_SERVICE_OFFERING_EDIT, eventDescription = "updating service offering")
public ServiceOffering updateServiceOffering(final UpdateServiceOfferingCmd cmd) {
final String displayText = cmd.getDisplayText();
final Long id = cmd.getId();
final String name = cmd.getServiceOfferingName();
final Integer sortKey = cmd.getSortKey();
Long userId = CallContext.current().getCallingUserId();
final List<Long> domainIds = cmd.getDomainIds();
final List<Long> zoneIds = cmd.getZoneIds();
String storageTags = cmd.getStorageTags();
String hostTags = cmd.getHostTags();
if (userId == null) {
userId = Long.valueOf(User.UID_SYSTEM);
}
// Verify input parameters
final ServiceOffering offeringHandle = _entityMgr.findById(ServiceOffering.class, id);
if (offeringHandle == null) {
throw new InvalidParameterValueException("unable to find service offering " + id);
}
List<Long> existingDomainIds = _serviceOfferingDetailsDao.findDomainIds(id);
Collections.sort(existingDomainIds);
List<Long> existingZoneIds = _serviceOfferingDetailsDao.findZoneIds(id);
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");
}
}
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 service offering: %s by admin: %s as it is domain-admin", offeringHandle.getUuid(), user.getUuid()));
}
if (existingDomainIds.isEmpty()) {
throw new InvalidParameterValueException(String.format("Unable to update public service offering: %s by user: %s because it is domain-admin", offeringHandle.getUuid(), user.getUuid()));
} else {
if (filteredDomainIds.isEmpty()) {
throw new InvalidParameterValueException(String.format("Unable to update service offering: %s to a public offering by user: %s because it is domain-admin", offeringHandle.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 service offering: %s as it has linked domain(s) which are not child domain for domain-admin: %s", offeringHandle.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 service offering: %s by domain-admin: %s with domain: %3$s which is not a child domain", offeringHandle.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 service offering: %s by id user: %s because it is not root-admin or domain-admin", offeringHandle.getUuid(), user.getUuid()));
}
final boolean updateNeeded = name != null || displayText != null || sortKey != null || storageTags != null || hostTags != null;
final boolean detailsUpdateNeeded = !filteredDomainIds.equals(existingDomainIds) || !filteredZoneIds.equals(existingZoneIds);
if (!updateNeeded && !detailsUpdateNeeded) {
return _serviceOfferingDao.findById(id);
}
ServiceOfferingVO offering = _serviceOfferingDao.createForUpdate(id);
if (name != null) {
offering.setName(name);
}
if (displayText != null) {
offering.setDisplayText(displayText);
}
if (sortKey != null) {
offering.setSortKey(sortKey);
}
DiskOfferingVO diskOffering = _diskOfferingDao.findById(offeringHandle.getDiskOfferingId());
updateOfferingTagsIfIsNotNull(storageTags, diskOffering);
_diskOfferingDao.update(diskOffering.getId(), diskOffering);
updateServiceOfferingHostTagsIfNotNull(hostTags, offering);
if (updateNeeded && !_serviceOfferingDao.update(id, offering)) {
return null;
}
List<ServiceOfferingDetailsVO> detailsVO = new ArrayList<>();
if (detailsUpdateNeeded) {
SearchBuilder<ServiceOfferingDetailsVO> sb = _serviceOfferingDetailsDao.createSearchBuilder();
sb.and("offeringId", sb.entity().getResourceId(), SearchCriteria.Op.EQ);
sb.and("detailName", sb.entity().getName(), SearchCriteria.Op.EQ);
sb.done();
SearchCriteria<ServiceOfferingDetailsVO> sc = sb.create();
sc.setParameters("offeringId", String.valueOf(id));
if (!filteredDomainIds.equals(existingDomainIds)) {
sc.setParameters("detailName", ApiConstants.DOMAIN_ID);
_serviceOfferingDetailsDao.remove(sc);
for (Long domainId : filteredDomainIds) {
detailsVO.add(new ServiceOfferingDetailsVO(id, ApiConstants.DOMAIN_ID, String.valueOf(domainId), false));
}
}
if (!filteredZoneIds.equals(existingZoneIds)) {
sc.setParameters("detailName", ApiConstants.ZONE_ID);
_serviceOfferingDetailsDao.remove(sc);
for (Long zoneId : filteredZoneIds) {
detailsVO.add(new ServiceOfferingDetailsVO(id, ApiConstants.ZONE_ID, String.valueOf(zoneId), false));
}
}
}
if (!detailsVO.isEmpty()) {
for (ServiceOfferingDetailsVO detailVO : detailsVO) {
_serviceOfferingDetailsDao.persist(detailVO);
}
}
offering = _serviceOfferingDao.findById(id);
CallContext.current().setEventDetails("Service offering id=" + offering.getId());
return offering;
}
use of com.cloud.storage.DiskOfferingVO 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;
}
use of com.cloud.storage.DiskOfferingVO in project cloudstack by apache.
the class ConfigurationManagerImpl method createServiceOffering.
@Override
@ActionEvent(eventType = EventTypes.EVENT_SERVICE_OFFERING_CREATE, eventDescription = "creating service offering")
public ServiceOffering createServiceOffering(final CreateServiceOfferingCmd cmd) {
final Long userId = CallContext.current().getCallingUserId();
final Map<String, String> details = cmd.getDetails();
final String offeringName = cmd.getServiceOfferingName();
final String name = cmd.getServiceOfferingName();
if (name == null || name.length() == 0) {
throw new InvalidParameterValueException("Failed to create service offering: specify the name that has non-zero length");
}
final String displayText = cmd.getDisplayText();
if (displayText == null || displayText.length() == 0) {
throw new InvalidParameterValueException("Failed to create service offering " + name + ": specify the display text that has non-zero length");
}
final Integer cpuNumber = cmd.getCpuNumber();
final Integer cpuSpeed = cmd.getCpuSpeed();
final Integer memory = cmd.getMemory();
// Optional Custom Parameters
Integer maxCPU = cmd.getMaxCPUs();
Integer minCPU = cmd.getMinCPUs();
Integer maxMemory = cmd.getMaxMemory();
Integer minMemory = cmd.getMinMemory();
// Check if service offering is Custom,
// If Customized, the following conditions must hold
// 1. cpuNumber, cpuSpeed and memory should be all null
// 2. minCPU, maxCPU, minMemory and maxMemory should all be null or all specified
boolean isCustomized = cmd.isCustomized();
if (isCustomized) {
// restricting the createserviceoffering to allow setting all or none of the dynamic parameters to null
if (cpuNumber != null || memory != null) {
throw new InvalidParameterValueException("For creating a custom compute offering cpu and memory all should be null");
}
// if any of them is null, then all of them shoull be null
if (maxCPU == null || minCPU == null || maxMemory == null || minMemory == null || cpuSpeed == null) {
if (maxCPU != null || minCPU != null || maxMemory != null || minMemory != null || cpuSpeed != null) {
throw new InvalidParameterValueException("For creating a custom compute offering min/max cpu and min/max memory/cpu speed should all be null or all specified");
}
} else {
if (cpuSpeed.intValue() < 0 || cpuSpeed.longValue() > Integer.MAX_VALUE) {
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the cpu speed value between 1 and " + Integer.MAX_VALUE);
}
if ((maxCPU <= 0 || maxCPU.longValue() > Integer.MAX_VALUE) || (minCPU <= 0 || minCPU.longValue() > Integer.MAX_VALUE)) {
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the minimum or minimum cpu number value between 1 and " + Integer.MAX_VALUE);
}
if (minMemory < 32 || (minMemory.longValue() > Integer.MAX_VALUE) || (maxMemory.longValue() > Integer.MAX_VALUE)) {
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the memory value between 32 and " + Integer.MAX_VALUE + " MB");
}
// Persist min/max CPU and Memory parameters in the service_offering_details table
details.put(ApiConstants.MIN_MEMORY, minMemory.toString());
details.put(ApiConstants.MAX_MEMORY, maxMemory.toString());
details.put(ApiConstants.MIN_CPU_NUMBER, minCPU.toString());
details.put(ApiConstants.MAX_CPU_NUMBER, maxCPU.toString());
}
} else {
Integer maxCPUCores = VM_SERVICE_OFFERING_MAX_CPU_CORES.value() == 0 ? Integer.MAX_VALUE : VM_SERVICE_OFFERING_MAX_CPU_CORES.value();
Integer maxRAMSize = VM_SERVICE_OFFERING_MAX_RAM_SIZE.value() == 0 ? Integer.MAX_VALUE : VM_SERVICE_OFFERING_MAX_RAM_SIZE.value();
if (cpuNumber != null && (cpuNumber.intValue() <= 0 || cpuNumber.longValue() > maxCPUCores)) {
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the cpu number value between 1 and " + maxCPUCores);
}
if (cpuSpeed == null || (cpuSpeed.intValue() < 0 || cpuSpeed.longValue() > Integer.MAX_VALUE)) {
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the cpu speed value between 0 and " + Integer.MAX_VALUE);
}
if (memory != null && (memory.intValue() < 32 || memory.longValue() > maxRAMSize)) {
throw new InvalidParameterValueException("Failed to create service offering " + offeringName + ": specify the memory value between 32 and " + maxRAMSize + " MB");
}
}
// check if valid domain
if (CollectionUtils.isNotEmpty(cmd.getDomainIds())) {
for (final Long domainId : cmd.getDomainIds()) {
if (_domainDao.findById(domainId) == null) {
throw new InvalidParameterValueException("Please specify a valid domain id");
}
}
}
// check if valid zone
if (CollectionUtils.isNotEmpty(cmd.getZoneIds())) {
for (Long zoneId : cmd.getZoneIds()) {
if (_zoneDao.findById(zoneId) == null)
throw new InvalidParameterValueException("Please specify a valid zone id");
}
}
// check if cache_mode parameter is valid
validateCacheMode(cmd.getCacheMode());
final Boolean offerHA = cmd.isOfferHa();
boolean localStorageRequired = false;
final String storageType = cmd.getStorageType();
if (storageType != null) {
if (storageType.equalsIgnoreCase(ServiceOffering.StorageType.local.toString())) {
if (offerHA) {
throw new InvalidParameterValueException("HA offering with local storage is not supported. ");
}
localStorageRequired = true;
} else if (!storageType.equalsIgnoreCase(ServiceOffering.StorageType.shared.toString())) {
throw new InvalidParameterValueException("Invalid storage type " + storageType + " specified, valid types are: 'local' and 'shared'");
}
}
final Boolean limitCpuUse = cmd.isLimitCpuUse();
final Boolean volatileVm = cmd.isVolatileVm();
final String vmTypeString = cmd.getSystemVmType();
VirtualMachine.Type vmType = null;
boolean allowNetworkRate = false;
Boolean isCustomizedIops;
if (cmd.isSystem()) {
if (vmTypeString == null || VirtualMachine.Type.DomainRouter.toString().toLowerCase().equals(vmTypeString)) {
vmType = VirtualMachine.Type.DomainRouter;
allowNetworkRate = true;
} else if (VirtualMachine.Type.ConsoleProxy.toString().toLowerCase().equals(vmTypeString)) {
vmType = VirtualMachine.Type.ConsoleProxy;
} else if (VirtualMachine.Type.SecondaryStorageVm.toString().toLowerCase().equals(vmTypeString)) {
vmType = VirtualMachine.Type.SecondaryStorageVm;
} else if (VirtualMachine.Type.InternalLoadBalancerVm.toString().toLowerCase().equals(vmTypeString)) {
vmType = VirtualMachine.Type.InternalLoadBalancerVm;
} else {
throw new InvalidParameterValueException("Invalid systemVmType. Supported types are: " + VirtualMachine.Type.DomainRouter + ", " + VirtualMachine.Type.ConsoleProxy + ", " + VirtualMachine.Type.SecondaryStorageVm);
}
if (cmd.isCustomizedIops() != null) {
throw new InvalidParameterValueException("Customized IOPS is not a valid parameter for a system VM.");
}
isCustomizedIops = false;
if (cmd.getHypervisorSnapshotReserve() != null) {
throw new InvalidParameterValueException("Hypervisor snapshot reserve is not a valid parameter for a system VM.");
}
} else {
allowNetworkRate = true;
isCustomizedIops = cmd.isCustomizedIops();
}
if (cmd.getNetworkRate() != null) {
if (!allowNetworkRate) {
throw new InvalidParameterValueException("Network rate can be specified only for non-System offering and system offerings having \"domainrouter\" systemvmtype");
}
if (cmd.getNetworkRate().intValue() < 0) {
throw new InvalidParameterValueException("Failed to create service offering " + name + ": specify the network rate value more than 0");
}
}
if (cmd.getDeploymentPlanner() != null) {
final List<String> planners = _mgr.listDeploymentPlanners();
if (planners != null && !planners.isEmpty()) {
if (!planners.contains(cmd.getDeploymentPlanner())) {
throw new InvalidParameterValueException("Invalid name for Deployment Planner specified, please use listDeploymentPlanners to get the valid set");
}
} else {
throw new InvalidParameterValueException("No deployment planners found");
}
}
final Long storagePolicyId = cmd.getStoragePolicy();
if (storagePolicyId != null) {
if (vsphereStoragePolicyDao.findById(storagePolicyId) == null) {
throw new InvalidParameterValueException("Please specify a valid vSphere storage policy id");
}
}
final Long diskOfferingId = cmd.getDiskOfferingId();
if (diskOfferingId != null) {
DiskOfferingVO diskOffering = _diskOfferingDao.findById(diskOfferingId);
if ((diskOffering == null) || diskOffering.isComputeOnly()) {
throw new InvalidParameterValueException("Please specify a valid disk offering.");
}
}
return createServiceOffering(userId, cmd.isSystem(), vmType, cmd.getServiceOfferingName(), cpuNumber, memory, cpuSpeed, cmd.getDisplayText(), cmd.getProvisioningType(), localStorageRequired, offerHA, limitCpuUse, volatileVm, cmd.getTags(), cmd.getDomainIds(), cmd.getZoneIds(), cmd.getHostTag(), cmd.getNetworkRate(), cmd.getDeploymentPlanner(), details, cmd.getRootDiskSize(), isCustomizedIops, cmd.getMinIops(), cmd.getMaxIops(), cmd.getBytesReadRate(), cmd.getBytesReadRateMax(), cmd.getBytesReadRateMaxLength(), cmd.getBytesWriteRate(), cmd.getBytesWriteRateMax(), cmd.getBytesWriteRateMaxLength(), cmd.getIopsReadRate(), cmd.getIopsReadRateMax(), cmd.getIopsReadRateMaxLength(), cmd.getIopsWriteRate(), cmd.getIopsWriteRateMax(), cmd.getIopsWriteRateMaxLength(), cmd.getHypervisorSnapshotReserve(), cmd.getCacheMode(), storagePolicyId, cmd.getDynamicScalingEnabled(), diskOfferingId, cmd.getDiskOfferingStrictness(), cmd.isCustomized());
}
use of com.cloud.storage.DiskOfferingVO 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;
}
use of com.cloud.storage.DiskOfferingVO in project cloudstack by apache.
the class UserVmJoinDaoImpl method newUserVmResponse.
@Override
public UserVmResponse newUserVmResponse(ResponseView view, String objectName, UserVmJoinVO userVm, EnumSet<VMDetails> details, Account caller) {
UserVmResponse userVmResponse = new UserVmResponse();
if (userVm.getHypervisorType() != null) {
userVmResponse.setHypervisor(userVm.getHypervisorType().toString());
}
userVmResponse.setId(userVm.getUuid());
userVmResponse.setName(userVm.getName());
if (userVm.getDisplayName() != null) {
userVmResponse.setDisplayName(userVm.getDisplayName());
} else {
userVmResponse.setDisplayName(userVm.getName());
}
if (userVm.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
userVmResponse.setProjectId(userVm.getProjectUuid());
userVmResponse.setProjectName(userVm.getProjectName());
} else {
userVmResponse.setAccountName(userVm.getAccountName());
}
User user = _userDao.getUser(userVm.getUserId());
if (user != null) {
userVmResponse.setUserId(user.getUuid());
userVmResponse.setUserName(user.getUsername());
}
userVmResponse.setDomainId(userVm.getDomainUuid());
userVmResponse.setDomainName(userVm.getDomainName());
userVmResponse.setCreated(userVm.getCreated());
userVmResponse.setLastUpdated(userVm.getLastUpdated());
userVmResponse.setDisplayVm(userVm.isDisplayVm());
if (userVm.getState() != null) {
userVmResponse.setState(userVm.getState().toString());
}
userVmResponse.setHaEnable(userVm.isHaEnabled());
if (details.contains(VMDetails.all) || details.contains(VMDetails.group)) {
userVmResponse.setGroupId(userVm.getInstanceGroupUuid());
userVmResponse.setGroup(userVm.getInstanceGroupName());
}
userVmResponse.setZoneId(userVm.getDataCenterUuid());
userVmResponse.setZoneName(userVm.getDataCenterName());
if (view == ResponseView.Full) {
userVmResponse.setInstanceName(userVm.getInstanceName());
userVmResponse.setHostId(userVm.getHostUuid());
userVmResponse.setHostName(userVm.getHostName());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.tmpl)) {
userVmResponse.setTemplateId(userVm.getTemplateUuid());
userVmResponse.setTemplateName(userVm.getTemplateName());
userVmResponse.setTemplateDisplayText(userVm.getTemplateDisplayText());
userVmResponse.setPasswordEnabled(userVm.isPasswordEnabled());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.iso)) {
userVmResponse.setIsoId(userVm.getIsoUuid());
userVmResponse.setIsoName(userVm.getIsoName());
userVmResponse.setIsoDisplayText(userVm.getIsoDisplayText());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.servoff)) {
userVmResponse.setServiceOfferingId(userVm.getServiceOfferingUuid());
userVmResponse.setServiceOfferingName(userVm.getServiceOfferingName());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.diskoff)) {
DiskOfferingVO diskOfferingVO = ApiDBUtils.findDiskOfferingById(userVm.getDiskOfferingId());
if (diskOfferingVO != null) {
userVmResponse.setDiskOfferingId(userVm.getDiskOfferingUuid());
userVmResponse.setDiskOfferingName(userVm.getDiskOfferingName());
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.backoff)) {
userVmResponse.setBackupOfferingId(userVm.getBackupOfferingUuid());
userVmResponse.setBackupOfferingName(userVm.getBackupOfferingName());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.servoff) || details.contains(VMDetails.stats)) {
userVmResponse.setCpuNumber(userVm.getCpu());
userVmResponse.setCpuSpeed(userVm.getSpeed());
userVmResponse.setMemory(userVm.getRamSize());
ServiceOfferingDetailsVO serviceOfferingDetail = ApiDBUtils.findServiceOfferingDetail(userVm.getServiceOfferingId(), GPU.Keys.vgpuType.toString());
if (serviceOfferingDetail != null) {
userVmResponse.setVgpu(serviceOfferingDetail.getValue());
}
}
userVmResponse.setGuestOsId(userVm.getGuestOsUuid());
if (details.contains(VMDetails.all) || details.contains(VMDetails.volume)) {
userVmResponse.setRootDeviceId(userVm.getVolumeDeviceId());
if (userVm.getVolumeType() != null) {
userVmResponse.setRootDeviceType(userVm.getVolumeType().toString());
}
}
userVmResponse.setPassword(userVm.getPassword());
if (userVm.getJobId() != null) {
userVmResponse.setJobId(userVm.getJobUuid());
userVmResponse.setJobStatus(userVm.getJobStatus());
}
// userVmResponse.setForVirtualNetwork(userVm.getForVirtualNetwork());
userVmResponse.setPublicIpId(userVm.getPublicIpUuid());
userVmResponse.setPublicIp(userVm.getPublicIpAddress());
userVmResponse.setKeyPairName(userVm.getKeypairName());
userVmResponse.setOsTypeId(userVm.getGuestOsUuid());
GuestOS guestOS = ApiDBUtils.findGuestOSById(userVm.getGuestOsId());
if (guestOS != null) {
userVmResponse.setOsDisplayName(guestOS.getDisplayName());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.stats)) {
// stats calculation
VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId());
if (vmStats != null) {
userVmResponse.setCpuUsed(new DecimalFormat("#.##").format(vmStats.getCPUUtilization()) + "%");
userVmResponse.setNetworkKbsRead((long) vmStats.getNetworkReadKBs());
userVmResponse.setNetworkKbsWrite((long) vmStats.getNetworkWriteKBs());
userVmResponse.setDiskKbsRead((long) vmStats.getDiskReadKBs());
userVmResponse.setDiskKbsWrite((long) vmStats.getDiskWriteKBs());
userVmResponse.setDiskIORead((long) vmStats.getDiskReadIOs());
userVmResponse.setDiskIOWrite((long) vmStats.getDiskWriteIOs());
long totalMemory = (long) vmStats.getMemoryKBs();
long freeMemory = (long) vmStats.getIntFreeMemoryKBs();
long correctedFreeMemory = freeMemory >= totalMemory ? 0 : freeMemory;
userVmResponse.setMemoryKBs(totalMemory);
userVmResponse.setMemoryIntFreeKBs(correctedFreeMemory);
userVmResponse.setMemoryTargetKBs((long) vmStats.getTargetMemoryKBs());
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.secgrp)) {
Long securityGroupId = userVm.getSecurityGroupId();
if (securityGroupId != null && securityGroupId.longValue() != 0) {
SecurityGroupResponse resp = new SecurityGroupResponse();
resp.setId(userVm.getSecurityGroupUuid());
resp.setName(userVm.getSecurityGroupName());
resp.setDescription(userVm.getSecurityGroupDescription());
resp.setObjectName("securitygroup");
if (userVm.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
resp.setProjectId(userVm.getProjectUuid());
resp.setProjectName(userVm.getProjectName());
} else {
resp.setAccountName(userVm.getAccountName());
}
userVmResponse.addSecurityGroup(resp);
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.nics)) {
long nic_id = userVm.getNicId();
if (nic_id > 0) {
NicResponse nicResponse = new NicResponse();
nicResponse.setId(userVm.getNicUuid());
nicResponse.setIpaddress(userVm.getIpAddress());
nicResponse.setGateway(userVm.getGateway());
nicResponse.setNetmask(userVm.getNetmask());
nicResponse.setNetworkid(userVm.getNetworkUuid());
nicResponse.setNetworkName(userVm.getNetworkName());
nicResponse.setMacAddress(userVm.getMacAddress());
nicResponse.setIp6Address(userVm.getIp6Address());
nicResponse.setIp6Gateway(userVm.getIp6Gateway());
nicResponse.setIp6Cidr(userVm.getIp6Cidr());
if (userVm.getBroadcastUri() != null) {
nicResponse.setBroadcastUri(userVm.getBroadcastUri().toString());
}
if (userVm.getIsolationUri() != null) {
nicResponse.setIsolationUri(userVm.getIsolationUri().toString());
}
if (userVm.getTrafficType() != null) {
nicResponse.setTrafficType(userVm.getTrafficType().toString());
}
if (userVm.getGuestType() != null) {
nicResponse.setType(userVm.getGuestType().toString());
}
nicResponse.setIsDefault(userVm.isDefaultNic());
nicResponse.setDeviceId(String.valueOf(userVm.getNicDeviceId()));
List<NicSecondaryIpVO> secondaryIps = ApiDBUtils.findNicSecondaryIps(userVm.getNicId());
if (secondaryIps != null) {
List<NicSecondaryIpResponse> ipList = new ArrayList<NicSecondaryIpResponse>();
for (NicSecondaryIpVO ip : secondaryIps) {
NicSecondaryIpResponse ipRes = new NicSecondaryIpResponse();
ipRes.setId(ip.getUuid());
ApiResponseHelper.setResponseIpAddress(ip, ipRes);
ipList.add(ipRes);
}
nicResponse.setSecondaryIps(ipList);
}
nicResponse.setObjectName("nic");
List<NicExtraDhcpOptionResponse> nicExtraDhcpOptionResponses = _nicExtraDhcpOptionDao.listByNicId(nic_id).stream().map(vo -> new NicExtraDhcpOptionResponse(Dhcp.DhcpOptionCode.valueOfInt(vo.getCode()).getName(), vo.getCode(), vo.getValue())).collect(Collectors.toList());
nicResponse.setExtraDhcpOptions(nicExtraDhcpOptionResponses);
userVmResponse.addNic(nicResponse);
}
}
// update tag information
long tag_id = userVm.getTagId();
if (tag_id > 0 && !userVmResponse.containTag(tag_id)) {
addTagInformation(userVm, userVmResponse);
}
userVmResponse.setHasAnnotation(annotationDao.hasAnnotations(userVm.getUuid(), AnnotationService.EntityType.VM.name(), _accountMgr.isRootAdmin(caller.getId())));
if (details.contains(VMDetails.all) || details.contains(VMDetails.affgrp)) {
Long affinityGroupId = userVm.getAffinityGroupId();
if (affinityGroupId != null && affinityGroupId.longValue() != 0) {
AffinityGroupResponse resp = new AffinityGroupResponse();
resp.setId(userVm.getAffinityGroupUuid());
resp.setName(userVm.getAffinityGroupName());
resp.setDescription(userVm.getAffinityGroupDescription());
resp.setObjectName("affinitygroup");
resp.setAccountName(userVm.getAccountName());
userVmResponse.addAffinityGroup(resp);
}
}
// set resource details map
// Allow passing details to end user
// Honour the display field and only return if display is set to true
List<UserVmDetailVO> vmDetails = _userVmDetailsDao.listDetails(userVm.getId(), true);
if (vmDetails != null) {
Map<String, String> resourceDetails = new HashMap<String, String>();
for (UserVmDetailVO userVmDetailVO : vmDetails) {
if (!userVmDetailVO.getName().startsWith(ApiConstants.PROPERTIES) || (UserVmManager.DisplayVMOVFProperties.value() && userVmDetailVO.getName().startsWith(ApiConstants.PROPERTIES))) {
resourceDetails.put(userVmDetailVO.getName(), userVmDetailVO.getValue());
}
if ((ApiConstants.BootType.UEFI.toString()).equalsIgnoreCase(userVmDetailVO.getName())) {
userVmResponse.setBootType("Uefi");
userVmResponse.setBootMode(userVmDetailVO.getValue().toLowerCase());
}
}
if (vmDetails.size() == 0) {
userVmResponse.setBootType("Bios");
userVmResponse.setBootMode("legacy");
}
if (userVm.getPoolType() != null) {
userVmResponse.setPoolType(userVm.getPoolType().toString());
}
// Remove deny listed settings if user is not admin
if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
String[] userVmSettingsToHide = QueryService.UserVMDeniedDetails.value().split(",");
for (String key : userVmSettingsToHide) {
resourceDetails.remove(key.trim());
}
}
userVmResponse.setDetails(resourceDetails);
if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
userVmResponse.setReadOnlyDetails(QueryService.UserVMReadOnlyDetails.value());
}
}
userVmResponse.setObjectName(objectName);
if (userVm.isDynamicallyScalable() == null) {
userVmResponse.setDynamicallyScalable(false);
} else {
userVmResponse.setDynamicallyScalable(userVm.isDynamicallyScalable());
}
addVmRxTxDataToResponse(userVm, userVmResponse);
return userVmResponse;
}
Aggregations