From d6055c9ae2ae21b16592e4142d28c41f48d24f02 Mon Sep 17 00:00:00 2001 From: Abhisar Sinha <63767682+abh1sar@users.noreply.github.com> Date: Sun, 5 Apr 2026 15:44:36 +0530 Subject: [PATCH] create volume on storage refactor --- .../command/user/volume/CreateVolumeCmd.java | 5 +++- .../cloud/storage/VolumeApiServiceImpl.java | 28 +++++-------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/api/src/main/java/org/apache/cloudstack/api/command/user/volume/CreateVolumeCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/user/volume/CreateVolumeCmd.java index 6371a3598ab..15926c55e87 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/user/volume/CreateVolumeCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/user/volume/CreateVolumeCmd.java @@ -113,7 +113,7 @@ public class CreateVolumeCmd extends BaseAsyncCreateCustomIdCmd implements UserC @Parameter(name = ApiConstants.STORAGE_ID, type = CommandType.UUID, entityType = StoragePoolResponse.class, - description = "Storage pool ID to create the volume in.") + description = "Storage pool ID to create the volume in. Exclusive with SnapshotId parameter.") private Long storageId; ///////////////////////////////////////////////////// @@ -161,6 +161,9 @@ public class CreateVolumeCmd extends BaseAsyncCreateCustomIdCmd implements UserC } public Long getStorageId() { + if (snapshotId != null && storageId != null) { + throw new IllegalArgumentException("StorageId parameter cannot be specified with the SnapshotId parameter."); + } return storageId; } diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index 3b833a1c150..1e91c300e9b 100644 --- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java @@ -1048,29 +1048,15 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic return true; } - private VolumeVO allocateVolumeOnStorage(Long volumeId, Long storageId) { + private VolumeVO allocateVolumeOnStorage(Long volumeId, Long storageId) throws ExecutionException, InterruptedException { DataStore destStore = dataStoreMgr.getDataStore(storageId, DataStoreRole.Primary); VolumeInfo destVolume = volFactory.getVolume(volumeId, destStore); - try { - AsyncCallFuture createVolumeFuture = volService.createVolumeAsync(destVolume, destStore); - VolumeApiResult createVolumeResult = createVolumeFuture.get(); - if (createVolumeResult.isFailed()) { - logger.debug("Failed to create dest volume {}, volume can be removed", destVolume); - destroyVolume(destVolume.getId()); - destVolume.processEvent(ObjectInDataStoreStateMachine.Event.ExpungeRequested); - destVolume.processEvent(ObjectInDataStoreStateMachine.Event.OperationSucceeded); - _volsDao.remove(destVolume.getId()); - throw new CloudRuntimeException("Creation of a dest volume failed: " + createVolumeResult.getResult()); - } else { - destVolume = volFactory.getVolume(destVolume.getId(), destStore); - destVolume.processEvent(ObjectInDataStoreStateMachine.Event.CreateRequested); - destVolume.processEvent(ObjectInDataStoreStateMachine.Event.OperationSucceeded); - } - } catch (Exception e) { - logger.debug("Failed to create dest volume {}", destVolume, e); - throw new CloudRuntimeException("Creation of a dest volume failed: volume needs cleanup"); + AsyncCallFuture createVolumeFuture = volService.createVolumeAsync(destVolume, destStore); + VolumeApiResult createVolumeResult = createVolumeFuture.get(); + if (createVolumeResult.isFailed()) { + throw new CloudRuntimeException("Creation of a dest volume failed: " + createVolumeResult.getResult()); } - return null; + return _volsDao.findById(destVolume.getId()); } @Override @@ -1113,7 +1099,7 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic } } } else if (storageId != null) { - allocateVolumeOnStorage(volumeId, storageId); + volume = allocateVolumeOnStorage(volumeId, storageId); } return volume; } catch (Exception e) {