Skip to content

Commit 13bd8be

Browse files
author
Prachi Damle
committed
CLOUDSTACK-6941: Can't choose storage for the volume, when attaching uploaded data volume to VM
Changes: - Only way to choose a certain storage pool is by using disk_offering_tags - Added a parameter to take in a disk offering Id. - Admin will have to create a custom sized disk offering and tag it as necessary for the user - This custom offering Id should be passed during uploadVolume to associate the volume with this disk offering
1 parent 008162a commit 13bd8be

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎api/src/org/apache/cloudstack/api/command/user/volume/UploadVolumeCmd.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.cloudstack.api.Parameter;
2626
import org.apache.cloudstack.api.ResponseObject.ResponseView;
2727
import org.apache.cloudstack.api.ServerApiException;
28+
import org.apache.cloudstack.api.response.DiskOfferingResponse;
2829
import org.apache.cloudstack.api.response.DomainResponse;
2930
import org.apache.cloudstack.api.response.ProjectResponse;
3031
import org.apache.cloudstack.api.response.VolumeResponse;
@@ -89,6 +90,9 @@ public class UploadVolumeCmd extends BaseAsyncCmd {
8990
@Parameter(name = ApiConstants.PROJECT_ID, type = CommandType.UUID, entityType = ProjectResponse.class, description = "Upload volume for the project")
9091
private Long projectId;
9192

93+
@Parameter(name = ApiConstants.DISK_OFFERING_ID, required = false, type = CommandType.UUID, entityType = DiskOfferingResponse.class, description = "the ID of the disk offering. This must be a custom sized offering since during uploadVolume volume size is unknown.")
94+
private Long diskOfferingId;
95+
9296
/////////////////////////////////////////////////////
9397
/////////////////// Accessors ///////////////////////
9498
/////////////////////////////////////////////////////
@@ -125,6 +129,10 @@ public String getImageStoreUuid() {
125129
return imageStoreUuid;
126130
}
127131

132+
public Long getDiskOfferingId() {
133+
return diskOfferingId;
134+
}
135+
128136
/////////////////////////////////////////////////////
129137
/////////////// API Implementation///////////////////
130138
/////////////////////////////////////////////////////

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,13 @@ public VolumeVO uploadVolume(UploadVolumeCmd cmd) throws ResourceAllocationExcep
254254
String volumeName = cmd.getVolumeName();
255255
String url = cmd.getUrl();
256256
String format = cmd.getFormat();
257+
Long diskOfferingId = cmd.getDiskOfferingId();
257258
String imageStoreUuid = cmd.getImageStoreUuid();
258259
DataStore store = _tmpltMgr.getImageStore(imageStoreUuid, zoneId);
259260

260-
validateVolume(caller, ownerId, zoneId, volumeName, url, format);
261+
validateVolume(caller, ownerId, zoneId, volumeName, url, format, diskOfferingId);
261262

262-
VolumeVO volume = persistVolume(owner, zoneId, volumeName, url, cmd.getFormat());
263+
VolumeVO volume = persistVolume(owner, zoneId, volumeName, url, cmd.getFormat(), diskOfferingId);
263264

264265
VolumeInfo vol = volFactory.getVolume(volume.getId());
265266

@@ -270,13 +271,15 @@ public VolumeVO uploadVolume(UploadVolumeCmd cmd) throws ResourceAllocationExcep
270271
return volume;
271272
}
272273

273-
private boolean validateVolume(Account caller, long ownerId, Long zoneId, String volumeName, String url, String format) throws ResourceAllocationException {
274+
private boolean validateVolume(Account caller, long ownerId, Long zoneId, String volumeName, String url,
275+
String format, Long diskOfferingId) throws ResourceAllocationException {
274276

275277
// permission check
276-
_accountMgr.checkAccess(caller, null, true, _accountMgr.getActiveAccountById(ownerId));
278+
Account volumeOwner = _accountMgr.getActiveAccountById(ownerId);
279+
_accountMgr.checkAccess(caller, null, true, volumeOwner);
277280

278281
// Check that the resource limit for volumes won't be exceeded
279-
_resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(ownerId), ResourceType.volume);
282+
_resourceLimitMgr.checkResourceLimit(volumeOwner, ResourceType.volume);
280283

281284
// Verify that zone exists
282285
DataCenterVO zone = _dcDao.findById(zoneId);
@@ -323,9 +326,28 @@ private boolean validateVolume(Account caller, long ownerId, Long zoneId, String
323326
}
324327
UriUtils.validateUrl(url);
325328

329+
326330
// Check that the resource limit for secondary storage won't be exceeded
327331
_resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(ownerId), ResourceType.secondary_storage, UriUtils.getRemoteSize(url));
328332

333+
// Check that the the disk offering specified is valid
334+
if (diskOfferingId != null) {
335+
DiskOfferingVO diskOffering = _diskOfferingDao.findById(diskOfferingId);
336+
if ((diskOffering == null) || diskOffering.getRemoved() != null
337+
|| !DiskOfferingVO.Type.Disk.equals(diskOffering.getType())) {
338+
throw new InvalidParameterValueException("Please specify a valid disk offering.");
339+
}
340+
if (!diskOffering.isCustomized()) {
341+
throw new InvalidParameterValueException("Please specify a custom sized disk offering.");
342+
}
343+
344+
if (diskOffering.getDomainId() == null) {
345+
// do nothing as offering is public
346+
} else {
347+
_configMgr.checkDiskOfferingAccess(volumeOwner, diskOffering);
348+
}
349+
}
350+
329351
return false;
330352
}
331353

@@ -334,7 +356,8 @@ public String getRandomVolumeName() {
334356
}
335357

336358
@DB
337-
protected VolumeVO persistVolume(final Account owner, final Long zoneId, final String volumeName, final String url, final String format) {
359+
protected VolumeVO persistVolume(final Account owner, final Long zoneId, final String volumeName, final String url,
360+
final String format, final Long diskOfferingId) {
338361
return Transaction.execute(new TransactionCallback<VolumeVO>() {
339362
@Override
340363
public VolumeVO doInTransaction(TransactionStatus status) {
@@ -346,8 +369,13 @@ public VolumeVO doInTransaction(TransactionStatus status) {
346369
// TODO Decide if this is valid or whether throwing a CloudRuntimeException is more appropriate
347370
volume.setAccountId((owner == null) ? Account.ACCOUNT_ID_SYSTEM : owner.getAccountId());
348371
volume.setDomainId((owner == null) ? Domain.ROOT_DOMAIN : owner.getDomainId());
349-
long diskOfferingId = _diskOfferingDao.findByUniqueName("Cloud.com-Custom").getId();
350-
volume.setDiskOfferingId(diskOfferingId);
372+
373+
if (diskOfferingId == null) {
374+
long defaultDiskOfferingId = _diskOfferingDao.findByUniqueName("Cloud.com-Custom").getId();
375+
volume.setDiskOfferingId(defaultDiskOfferingId);
376+
} else {
377+
volume.setDiskOfferingId(diskOfferingId);
378+
}
351379
// volume.setSize(size);
352380
volume.setInstanceId(null);
353381
volume.setUpdated(new Date());

0 commit comments

Comments
 (0)