Skip to content

Commit

Permalink
Update 8808: part2 for local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
weizhouapache committed Mar 20, 2024
1 parent 6160dd9 commit 2e332a2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public class VolumeForImportResponse extends BaseResponse {
@Param(description = "name of the primary storage hosting the volume")
private String storagePoolName;

@SerializedName(ApiConstants.STORAGE_TYPE)
@Param(description = "type of the primary storage hosting the volume")
private String storagePoolType;

@SerializedName(ApiConstants.DETAILS)
@Param(description = "volume details in key/value pairs.")
private Map details;
Expand Down Expand Up @@ -146,6 +150,14 @@ public void setStoragePoolName(String storagePoolName) {
this.storagePoolName = storagePoolName;
}

public String getStoragePoolType() {
return storagePoolType;
}

public void setStoragePoolType(String storagePoolType) {
this.storagePoolType = storagePoolType;
}

public Map getDetails() {
return details;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public DataStore initialize(Map<String, Object> dsInfos) {
String uri = String.format("%s://%s%s", scheme, storageHost, hostPath);

Object localStorage = dsInfos.get("localStorage");
if (localStorage != null) {
if (localStorage != null) {
hostPath = hostPath.contains("//") ? hostPath.replaceFirst("/", "") : hostPath;
hostPath = hostPath.replace("+", " ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public class VolumeImportUnmanagedManagerImpl implements VolumeImportUnmanageSer
private VMTemplatePoolDao templatePoolDao;

private static final String DEFAULT_DISK_OFFERING_NAME = "Default Custom Offering for Volume Import";
private static final String DEFAULT_DISK_OFFERING_UNIQUE_NAME = "Custom-Offering-Volume-Import";
private static final String DEFAULT_DISK_OFFERING_UNIQUE_NAME = "Custom-Volume-Import";
private static final String DEFAULT_DISK_OFFERING_NAME_LOCAL = DEFAULT_DISK_OFFERING_NAME + " - Local Storage";
private static final String DEFAULT_DISK_OFFERING_UNIQUE_NAME_LOCAL = DEFAULT_DISK_OFFERING_UNIQUE_NAME + "-Local";

private void logFailureAndThrowException(String msg) {
logger.error(msg);
Expand Down Expand Up @@ -180,7 +182,7 @@ public VolumeResponse importVolume(ImportVolumeCmd cmd) {
checkResourceLimitForImportVolume(owner, volume);

// 6. get disk offering
DiskOfferingVO diskOffering = getOrCreateDiskOffering(owner, cmd.getDiskOfferingId(), pool.getDataCenterId());
DiskOfferingVO diskOffering = getOrCreateDiskOffering(owner, cmd.getDiskOfferingId(), pool.getDataCenterId(), pool.isLocal());

// 7. create records
VolumeVO volumeVO = createRecordsForVolumeImport(volume, diskOffering, owner, pool);
Expand Down Expand Up @@ -284,6 +286,7 @@ private VolumeForImportResponse createVolumeForImportResponse(VolumeOnStorageTO
response.setQemuEncryptFormat(volume.getQemuEncryptFormat());
response.setStoragePoolId(pool.getUuid());
response.setStoragePoolName(pool.getName());
response.setStoragePoolType(String.valueOf(pool.getPoolType()));
response.setDetails(volume.getDetails());
response.setObjectName("volumeforimport");
return response;
Expand All @@ -297,7 +300,7 @@ private boolean checkIfVolumeForTemplate(StoragePoolVO pool, String volumePath)
return templatePoolDao.findByPoolPath(pool.getId(), volumePath) != null;
}

private DiskOfferingVO getOrCreateDiskOffering(Account owner, Long diskOfferingId, Long zoneId) {
private DiskOfferingVO getOrCreateDiskOffering(Account owner, Long diskOfferingId, Long zoneId, boolean isLocal) {
if (diskOfferingId != null) {
// check if disk offering exists and active
DiskOfferingVO diskOfferingVO = diskOfferingDao.findById(diskOfferingId);
Expand All @@ -307,28 +310,43 @@ private DiskOfferingVO getOrCreateDiskOffering(Account owner, Long diskOfferingI
if (!DiskOffering.State.Active.equals(diskOfferingVO.getState())) {
logFailureAndThrowException(String.format("Disk offering with ID %s is not active", diskOfferingId));
}
if (diskOfferingVO.isUseLocalStorage() != isLocal) {
logFailureAndThrowException(String.format("Disk offering with ID %s should use %s storage", diskOfferingId, isLocal ? "local": "shared"));
}
// check if disk offering is accessible by the account/owner
try {
configMgr.checkDiskOfferingAccess(owner, diskOfferingVO, dcDao.findById(zoneId));
return diskOfferingVO;
} catch (PermissionDeniedException ignored) {
logFailureAndThrowException(String.format("Disk offering with ID %s is not accessible by account %s", diskOfferingId, owner.getAccountName()));
} catch (PermissionDeniedException ex) {
logFailureAndThrowException(String.format("Disk offering with ID %s is not accessible by owner %s", diskOfferingId, owner));
}
}
return getOrCreateDefaultDiskOfferingIdForVolumeImport();
return getOrCreateDefaultDiskOfferingIdForVolumeImport(isLocal);
}


private DiskOfferingVO getOrCreateDefaultDiskOfferingIdForVolumeImport() {
DiskOfferingVO diskOffering = diskOfferingDao.findByUniqueName(DEFAULT_DISK_OFFERING_UNIQUE_NAME);
if (diskOffering != null) {
return diskOffering;
private DiskOfferingVO getOrCreateDefaultDiskOfferingIdForVolumeImport(boolean isLocalStorage) {
if (isLocalStorage) {
DiskOfferingVO diskOffering = diskOfferingDao.findByUniqueName(DEFAULT_DISK_OFFERING_UNIQUE_NAME_LOCAL);
if (diskOffering != null) {
return diskOffering;
}
DiskOfferingVO newDiskOffering = new DiskOfferingVO(DEFAULT_DISK_OFFERING_NAME_LOCAL, DEFAULT_DISK_OFFERING_NAME_LOCAL,
Storage.ProvisioningType.THIN, 0, null, true, null, null, null);
newDiskOffering.setUseLocalStorage(true);
newDiskOffering.setUniqueName(DEFAULT_DISK_OFFERING_UNIQUE_NAME_LOCAL);
newDiskOffering = diskOfferingDao.persistDefaultDiskOffering(newDiskOffering);
return newDiskOffering;
} else {
DiskOfferingVO diskOffering = diskOfferingDao.findByUniqueName(DEFAULT_DISK_OFFERING_UNIQUE_NAME);
if (diskOffering != null) {
return diskOffering;
}
DiskOfferingVO newDiskOffering = new DiskOfferingVO(DEFAULT_DISK_OFFERING_NAME, DEFAULT_DISK_OFFERING_NAME,
Storage.ProvisioningType.THIN, 0, null, true, null, null, null);
newDiskOffering.setUniqueName(DEFAULT_DISK_OFFERING_UNIQUE_NAME);
newDiskOffering = diskOfferingDao.persistDefaultDiskOffering(newDiskOffering);
return newDiskOffering;
}
DiskOfferingVO newDiskOffering = new DiskOfferingVO(DEFAULT_DISK_OFFERING_NAME, DEFAULT_DISK_OFFERING_NAME,
Storage.ProvisioningType.THIN, 0, null, true, null, null, null);
newDiskOffering.setUniqueName(DEFAULT_DISK_OFFERING_UNIQUE_NAME);
newDiskOffering = diskOfferingDao.persistDefaultDiskOffering(newDiskOffering);
return newDiskOffering;
}

private VolumeVO createRecordsForVolumeImport(VolumeOnStorageTO volume, DiskOfferingVO diskOffering,
Expand Down

0 comments on commit 2e332a2

Please sign in to comment.