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 27b592aa8f15..84dd4525265b 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 @@ -32,6 +32,7 @@ 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,13 @@ 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. Cannot be used with the snapshotid parameter.", + authorized = {RoleType.Admin}) + private Long storageId; + ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// ///////////////////////////////////////////////////// @@ -153,6 +161,13 @@ private Long getProjectId() { return projectId; } + public Long getStorageId() { + if (snapshotId != null && storageId != null) { + throw new IllegalArgumentException("StorageId parameter cannot be specified with the SnapshotId parameter."); + } + return storageId; + } + public Boolean getDisplayVolume() { return displayVolume; } diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index ca3d31d4fad3..d9f54e7de026 100644 --- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java @@ -1043,6 +1043,36 @@ public boolean validateVolumeSizeInBytes(long size) { return true; } + private VolumeVO createVolumeOnStoragePool(Long volumeId, Long storageId) throws ExecutionException, InterruptedException { + VolumeVO volume = _volsDao.findById(volumeId); + StoragePool storagePool = (StoragePool) dataStoreMgr.getDataStore(storageId, DataStoreRole.Primary); + if (storagePool == null) { + throw new InvalidParameterValueException("Failed to find the storage pool: " + storageId); + } else if (!storagePool.getStatus().equals(StoragePoolStatus.Up)) { + throw new InvalidParameterValueException(String.format("Cannot create volume %s on storage pool %s as the storage pool is not in Up state.", + volume.getUuid(), storagePool.getName())); + } + + if (storagePool.getDataCenterId() != volume.getDataCenterId()) { + throw new InvalidParameterValueException(String.format("Cannot create volume %s in zone %s on storage pool %s in zone %s.", + volume.getUuid(), volume.getDataCenterId(), storagePool.getUuid(), storagePool.getDataCenterId())); + } + + DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume.getDiskOfferingId()); + if (!doesStoragePoolSupportDiskOffering(storagePool, diskOffering)) { + throw new InvalidParameterValueException(String.format("Disk offering: %s is not compatible with the storage pool", diskOffering.getUuid())); + } + + DataStore dataStore = dataStoreMgr.getDataStore(storageId, DataStoreRole.Primary); + VolumeInfo volumeInfo = volFactory.getVolume(volumeId, dataStore); + AsyncCallFuture createVolumeFuture = volService.createVolumeAsync(volumeInfo, dataStore); + VolumeApiResult createVolumeResult = createVolumeFuture.get(); + if (createVolumeResult.isFailed()) { + throw new CloudRuntimeException("Volume creation on storage failed: " + createVolumeResult.getResult()); + } + return _volsDao.findById(volumeInfo.getId()); + } + @Override @DB @ActionEvent(eventType = EventTypes.EVENT_VOLUME_CREATE, eventDescription = "creating volume", async = true) @@ -1074,6 +1104,8 @@ public VolumeVO createVolume(CreateVolumeCmd cmd) { throw new CloudRuntimeException(message.toString()); } } + } else if (cmd.getStorageId() != null) { + volume = createVolumeOnStoragePool(cmd.getEntityId(), cmd.getStorageId()); } return volume; } catch (Exception e) { diff --git a/ui/public/locales/en.json b/ui/public/locales/en.json index 6494995bf223..4d8d32f87bbb 100644 --- a/ui/public/locales/en.json +++ b/ui/public/locales/en.json @@ -681,6 +681,7 @@ "label.create.sharedfs": "Create Shared FileSystem", "label.create.network": "Create new Network", "label.create.nfs.secondary.staging.storage": "Create NFS secondary staging storage", +"label.create.on.storage": "Create on Storage", "label.create.project": "Create Project", "label.create.project.role": "Create Project Role", "label.create.routing.policy": "Create Routing Policy", @@ -697,6 +698,7 @@ "label.create.tier.networkofferingid.description": "The Network offering for the Network Tier.", "label.create.tungsten.routing.policy": "Create Tungsten-Fabric routing policy", "label.create.user": "Create User", +"label.create.volume.on.primary.storage": "Create Volume on the specified Primary Storage", "label.create.vm": "Create Instance", "label.create.vm.and.stay": "Create Instance & stay on this page", "label.create.vpn.connection": "Create VPN connection", diff --git a/ui/src/views/storage/CreateVolume.vue b/ui/src/views/storage/CreateVolume.vue index e99cc7491707..f5185091f31b 100644 --- a/ui/src/views/storage/CreateVolume.vue +++ b/ui/src/views/storage/CreateVolume.vue @@ -116,6 +116,42 @@ :placeholder="apiParams.maxiops.description"/> + + + + + + + + + + + + + {{ pool.name }} + + + + +