Skip to content

Commit 3dbfbf7

Browse files
committed
Add checks
1 parent 30ee7e2 commit 3dbfbf7

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

api/src/main/java/org/apache/cloudstack/api/command/user/volume/CreateVolumeCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ public class CreateVolumeCmd extends BaseAsyncCreateCustomIdCmd implements UserC
113113
@Parameter(name = ApiConstants.STORAGE_ID,
114114
type = CommandType.UUID,
115115
entityType = StoragePoolResponse.class,
116-
description = "Storage pool ID to create the volume in. Exclusive with SnapshotId parameter.")
116+
description = "Storage pool ID to create the volume in. Exclusive with SnapshotId parameter.",
117+
authorized = {RoleType.Admin})
117118
private Long storageId;
118119

119120
/////////////////////////////////////////////////////

server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,26 @@ public boolean validateVolumeSizeInBytes(long size) {
10431043
return true;
10441044
}
10451045

1046-
private VolumeVO allocateVolumeOnStorage(Long volumeId, Long storageId) throws ExecutionException, InterruptedException {
1046+
private VolumeVO createVolumeOnStoragePool(Long volumeId, Long storageId) throws ExecutionException, InterruptedException {
1047+
VolumeVO volume = _volsDao.findById(volumeId);
1048+
StoragePool destPool = (StoragePool) dataStoreMgr.getDataStore(storageId, DataStoreRole.Primary);
1049+
if (destPool == null) {
1050+
throw new InvalidParameterValueException("Failed to find the destination storage pool: " + storageId);
1051+
} else if (destPool.isInMaintenance()) {
1052+
throw new InvalidParameterValueException(String.format("Cannot create volume %s on storage pool %s as the storage pool is in maintenance mode.",
1053+
volume.getUuid(), destPool.getName()));
1054+
}
1055+
1056+
if (destPool.getDataCenterId() != volume.getDataCenterId()) {
1057+
throw new InvalidParameterValueException(String.format("Cannot create volume %s in zone %s on storage pool %s in zone %s.",
1058+
volume.getUuid(), volume.getDataCenterId(), destPool.getUuid(), destPool.getDataCenterId()));
1059+
}
1060+
1061+
DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId());
1062+
if (!doesStoragePoolSupportDiskOffering(destPool, diskOffering)) {
1063+
throw new InvalidParameterValueException(String.format("Disk offering: %s is not compatible with the storage pool", diskOffering.getUuid()));
1064+
}
1065+
10471066
DataStore destStore = dataStoreMgr.getDataStore(storageId, DataStoreRole.Primary);
10481067
VolumeInfo destVolume = volFactory.getVolume(volumeId, destStore);
10491068
AsyncCallFuture<VolumeApiResult> createVolumeFuture = volService.createVolumeAsync(destVolume, destStore);
@@ -1086,7 +1105,7 @@ public VolumeVO createVolume(CreateVolumeCmd cmd) {
10861105
}
10871106
}
10881107
} else if (cmd.getStorageId() != null) {
1089-
volume = allocateVolumeOnStorage(cmd.getEntityId(), cmd.getStorageId());
1108+
volume = createVolumeOnStoragePool(cmd.getEntityId(), cmd.getStorageId());
10901109
}
10911110
return volume;
10921111
} catch (Exception e) {

0 commit comments

Comments
 (0)