Create volume on the given storage pool

This commit is contained in:
abh1sar 2026-01-19 11:53:03 +05:30 committed by Abhishek Kumar
parent f396c5cc74
commit 73df3cbef7
2 changed files with 38 additions and 0 deletions

View File

@ -32,6 +32,7 @@ import org.apache.cloudstack.api.response.DiskOfferingResponse;
import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.ProjectResponse;
import org.apache.cloudstack.api.response.SnapshotResponse;
import org.apache.cloudstack.api.response.StoragePoolResponse;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.cloudstack.api.response.VolumeResponse;
import org.apache.cloudstack.api.response.ZoneResponse;
@ -109,6 +110,12 @@ public class CreateVolumeCmd extends BaseAsyncCreateCustomIdCmd implements UserC
description = "The ID of the Instance; to be used with snapshot Id, Instance to which the volume gets attached after creation")
private Long virtualMachineId;
@Parameter(name = ApiConstants.STORAGE_ID,
type = CommandType.UUID,
entityType = StoragePoolResponse.class,
description = "Storage pool ID to create the volume in.")
private Long storageId;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
@ -153,6 +160,10 @@ public class CreateVolumeCmd extends BaseAsyncCreateCustomIdCmd implements UserC
return projectId;
}
public Long getStorageId() {
return storageId;
}
public Boolean getDisplayVolume() {
return displayVolume;
}

View File

@ -1046,6 +1046,31 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic
return true;
}
private VolumeVO allocateVolumeOnStorage(Long volumeId, Long storageId) {
DataStore destStore = dataStoreMgr.getDataStore(storageId, DataStoreRole.Primary);
VolumeInfo destVolume = volFactory.getVolume(volumeId, destStore);
try {
AsyncCallFuture<VolumeApiResult> 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");
}
return null;
}
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_VOLUME_CREATE, eventDescription = "creating volume", async = true)
@ -1077,6 +1102,8 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic
throw new CloudRuntimeException(message.toString());
}
}
} else if (cmd.getStorageId() != null) {
allocateVolumeOnStorage(cmd.getEntityId(), cmd.getStorageId());
}
return volume;
} catch (Exception e) {