This is an automated email from the ASF dual-hosted git repository.

dahn pushed a commit to branch 4.17
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/4.17 by this push:
     new 3dc0e499dcf storage: validate disk size range of custom disk offering 
when resize volume (#7073)
3dc0e499dcf is described below

commit 3dc0e499dcf0656ecf718722e4c8f41cde87dbfe
Author: Wei Zhou <[email protected]>
AuthorDate: Wed Jan 18 09:40:43 2023 +0100

    storage: validate disk size range of custom disk offering when resize 
volume (#7073)
---
 .../java/com/cloud/storage/VolumeApiService.java   |  2 ++
 .../com/cloud/storage/VolumeApiServiceImpl.java    | 23 ++++++++++++++++------
 .../main/java/com/cloud/vm/UserVmManagerImpl.java  |  7 +------
 3 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/api/src/main/java/com/cloud/storage/VolumeApiService.java 
b/api/src/main/java/com/cloud/storage/VolumeApiService.java
index 84559e66a9e..f663f9b073a 100644
--- a/api/src/main/java/com/cloud/storage/VolumeApiService.java
+++ b/api/src/main/java/com/cloud/storage/VolumeApiService.java
@@ -154,6 +154,8 @@ public interface VolumeApiService {
 
     Volume recoverVolume(long volumeId);
 
+    void validateCustomDiskOfferingSizeRange(Long sizeInGB);
+
     boolean validateVolumeSizeInBytes(long size);
 
     Volume changeDiskOfferingForVolume(ChangeOfferingForVolumeCmd cmd) throws 
ResourceAllocationException;
diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java 
b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java
index bbebc7a2f3b..609e4e37206 100644
--- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java
+++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java
@@ -715,12 +715,7 @@ public class VolumeApiServiceImpl extends ManagerBase 
implements VolumeApiServic
                 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);
-                }
+                validateCustomDiskOfferingSizeRange(sizeInGB);
             }
 
             if (!diskOffering.isCustomized() && size != null) {
@@ -872,6 +867,16 @@ public class VolumeApiServiceImpl extends ManagerBase 
implements VolumeApiServic
                 _uuidMgr.generateUuid(Volume.class, cmd.getCustomId()), 
details);
     }
 
+    @Override
+    public void validateCustomDiskOfferingSizeRange(Long sizeInGB) {
+        Long customDiskOfferingMaxSize = 
VolumeOrchestrationService.CustomDiskOfferingMaxSize.value();
+        Long customDiskOfferingMinSize = 
VolumeOrchestrationService.CustomDiskOfferingMinSize.value();
+
+        if ((sizeInGB < customDiskOfferingMinSize) || (sizeInGB > 
customDiskOfferingMaxSize)) {
+            throw new InvalidParameterValueException(String.format("Volume 
size: %s GB is out of allowed range. Min: %s. Max: %s", sizeInGB, 
customDiskOfferingMinSize, customDiskOfferingMaxSize));
+        }
+    }
+
     private VolumeVO commitVolume(final CreateVolumeCmd cmd, final Account 
caller, final Account owner, final Boolean displayVolume, final Long zoneId, 
final Long diskOfferingId,
                                   final Storage.ProvisioningType 
provisioningType, final Long size, final Long minIops, final Long maxIops, 
final VolumeVO parentVolume, final String userSpecifiedName, final String uuid, 
final Map<String, String> details) {
         return Transaction.execute(new TransactionCallback<VolumeVO>() {
@@ -1060,6 +1065,10 @@ public class VolumeApiServiceImpl extends ManagerBase 
implements VolumeApiServic
                     throw new 
InvalidParameterValueException(String.format("Resize of volume %s is not 
allowed, since disk size is strictly fixed as per the disk offering", 
volume.getUuid()));
                 }
 
+                if (diskOffering.isCustomized()) {
+                    validateCustomDiskOfferingSizeRange(newSize);
+                }
+
                 if (isNotPossibleToResize(volume, diskOffering)) {
                     throw new InvalidParameterValueException(
                             "Failed to resize Root volume. The service 
offering of this Volume has been configured with a root disk size; "
@@ -1138,6 +1147,8 @@ public class VolumeApiServiceImpl extends ManagerBase 
implements VolumeApiServic
                     throw new InvalidParameterValueException("The new disk 
offering requires that a size be specified.");
                 }
 
+                validateCustomDiskOfferingSizeRange(newSize);
+
                 // convert from GiB to bytes
                 newSize = newSize << 30;
             } else {
diff --git a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java 
b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
index 3e17cb75809..6a2f37808b7 100644
--- a/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
+++ b/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
@@ -4187,12 +4187,7 @@ public class UserVmManagerImpl extends ManagerBase 
implements UserVmManager, Vir
             if (diskSize == null) {
                 throw new InvalidParameterValueException("This disk offering 
requires a custom size specified");
             }
-            Long customDiskOfferingMaxSize = 
VolumeOrchestrationService.CustomDiskOfferingMaxSize.value();
-            Long customDiskOfferingMinSize = 
VolumeOrchestrationService.CustomDiskOfferingMinSize.value();
-            if ((diskSize < customDiskOfferingMinSize) || (diskSize > 
customDiskOfferingMaxSize)) {
-                throw new InvalidParameterValueException("VM Creation failed. 
Volume size: " + diskSize + "GB is out of allowed range. Max: " + 
customDiskOfferingMaxSize
-                        + " Min:" + customDiskOfferingMinSize);
-            }
+            _volumeService.validateCustomDiskOfferingSizeRange(diskSize);
             size = diskSize * GiB_TO_BYTES;
         } else {
             size = diskOffering.getDiskSize();

Reply via email to