Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,7 @@ protected UserVm importUnmanagedInstanceFromVmwareToKvm(DataCenter zone, Cluster
ApiConstants.FORCE_MS_TO_IMPORT_VM_FILES, ApiConstants.USE_VDDK));
}

convertStoragePoolId = resolveImplicitConversionStoragePoolId(destinationCluster, convertStoragePoolId, forceConvertToPool, useVddk);
StoragePoolVO selectedConversionStoragePool = convertStoragePoolId == null ? null : primaryDataStoreDao.findById(convertStoragePoolId);
boolean directRbdVddkImport = useVddk && forceConvertToPool && selectedConversionStoragePool != null &&
selectedConversionStoragePool.getPoolType() == Storage.StoragePoolType.RBD;
Expand Down Expand Up @@ -2047,6 +2048,46 @@ protected void checkConversionStoragePool(Long convertStoragePoolId, boolean for
}
}

protected Long resolveImplicitConversionStoragePoolId(Cluster destinationCluster, Long convertStoragePoolId,
boolean forceConvertToPool, boolean useVddk) {
if (!forceConvertToPool || convertStoragePoolId != null) {
return convertStoragePoolId;
}

List<StoragePoolVO> candidatePools = getImplicitForceConvertToPoolStoragePools(destinationCluster, useVddk);
if (candidatePools.isEmpty()) {
logFailureAndThrowException(String.format("The parameter forceconverttopool is set to true, but no suitable primary storage pool was found in cluster %s",
destinationCluster.getName()));
}
if (candidatePools.size() > 1) {
logFailureAndThrowException(String.format("The parameter forceconverttopool is set to true, but multiple suitable primary storage pools were found in cluster %s. Please provide convertinstancepoolid.",
destinationCluster.getName()));
}

StoragePoolVO implicitPool = candidatePools.get(0);
logger.debug("The parameter forceconverttopool is set to true and no conversion storage pool was provided; using the only suitable primary storage pool [{}].",
implicitPool.getName());
return implicitPool.getId();
}

private List<StoragePoolVO> getImplicitForceConvertToPoolStoragePools(Cluster destinationCluster, boolean useVddk) {
Set<StoragePoolVO> pools = new HashSet<>();
List<Storage.StoragePoolType> allowedTypes = useVddk ? vddkDirectConvertToPoolAllowedTypes : forceConvertToPoolAllowedTypes;
for (Storage.StoragePoolType poolType : allowedTypes) {
List<StoragePoolVO> clusterPools = primaryDataStoreDao.findClusterWideStoragePoolsByHypervisorAndPoolType(destinationCluster.getId(),
Hypervisor.HypervisorType.KVM, poolType);
if (clusterPools != null) {
pools.addAll(clusterPools);
}
List<StoragePoolVO> zonePools = primaryDataStoreDao.findZoneWideStoragePoolsByHypervisorAndPoolType(destinationCluster.getDataCenterId(),
Hypervisor.HypervisorType.KVM, poolType);
if (zonePools != null) {
pools.addAll(zonePools);
}
}
return new ArrayList<>(pools);
}

protected void validateSelectedConversionStoragePoolForVddk(boolean useVddk, Long convertStoragePoolId,
ServiceOfferingVO serviceOffering, Map<String, Long> dataDiskOfferingMap) {
validateSelectedConversionStoragePoolForVddk(useVddk, true, convertStoragePoolId, serviceOffering, dataDiskOfferingMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,217 @@ public void testCheckConversionStoragePoolTemporarySecondaryStorageForceConvertT
unmanagedVMsManager.checkConversionStoragePool(null, true);
}

@Test
public void testResolveImplicitConversionStoragePoolIdUsesSingleSuitablePoolWhenForceConvertToPool() {
ClusterVO cluster = getClusterForTests();
StoragePoolVO storagePool = mock(StoragePoolVO.class);
when(storagePool.getId()).thenReturn(42L);
when(primaryDataStoreDao.findClusterWideStoragePoolsByHypervisorAndPoolType(cluster.getId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.NetworkFilesystem))
.thenReturn(List.of(storagePool));

Long storagePoolId = unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, null, true, true);

Assert.assertEquals(Long.valueOf(42L), storagePoolId);
}

@Test
public void testResolveImplicitConversionStoragePoolIdUsesSingleZoneRbdPoolForVddk() {
ClusterVO cluster = getClusterForTests();
StoragePoolVO storagePool = mock(StoragePoolVO.class);
when(storagePool.getId()).thenReturn(42L);
when(primaryDataStoreDao.findZoneWideStoragePoolsByHypervisorAndPoolType(cluster.getDataCenterId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.RBD))
.thenReturn(List.of(storagePool));

Long storagePoolId = unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, null, true, true);

Assert.assertEquals(Long.valueOf(42L), storagePoolId);
}

@Test
public void testResolveImplicitConversionStoragePoolIdUsesSingleClusterLinstorPoolForVddk() {
ClusterVO cluster = getClusterForTests();
StoragePoolVO storagePool = mock(StoragePoolVO.class);
when(storagePool.getId()).thenReturn(42L);
when(primaryDataStoreDao.findClusterWideStoragePoolsByHypervisorAndPoolType(cluster.getId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.Linstor))
.thenReturn(List.of(storagePool));

Long storagePoolId = unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, null, true, true);

Assert.assertEquals(Long.valueOf(42L), storagePoolId);
}

@Test
public void testResolveImplicitConversionStoragePoolIdKeepsExplicitPoolWhenForceConvertToPool() {
ClusterVO cluster = getClusterForTests();

Assert.assertEquals(Long.valueOf(42L), unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, 42L, true, true));
Assert.assertEquals(Long.valueOf(42L), unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, 42L, true, false));
Mockito.verifyNoInteractions(primaryDataStoreDao);
}

@Test(expected = CloudRuntimeException.class)
public void testResolveImplicitConversionStoragePoolIdFailsWhenMultiplePoolsFound() {
ClusterVO cluster = getClusterForTests();
when(cluster.getName()).thenReturn("cluster-1");
StoragePoolVO firstStoragePool = mock(StoragePoolVO.class);
StoragePoolVO secondStoragePool = mock(StoragePoolVO.class);
when(primaryDataStoreDao.findClusterWideStoragePoolsByHypervisorAndPoolType(cluster.getId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.NetworkFilesystem))
.thenReturn(List.of(firstStoragePool));
when(primaryDataStoreDao.findZoneWideStoragePoolsByHypervisorAndPoolType(cluster.getDataCenterId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.NetworkFilesystem))
.thenReturn(List.of(secondStoragePool));

unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, null, true, true);
}

@Test
public void testResolveImplicitConversionStoragePoolIdFailsWhenNfsAndRbdPoolsFoundForVddk() {
ClusterVO cluster = getClusterForTests();
StoragePoolVO nfsPool = mock(StoragePoolVO.class);
StoragePoolVO rbdPool = mock(StoragePoolVO.class);
when(primaryDataStoreDao.findClusterWideStoragePoolsByHypervisorAndPoolType(cluster.getId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.NetworkFilesystem))
.thenReturn(List.of(nfsPool));
when(primaryDataStoreDao.findZoneWideStoragePoolsByHypervisorAndPoolType(cluster.getDataCenterId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.RBD))
.thenReturn(List.of(rbdPool));

CloudRuntimeException exception = Assert.assertThrows(CloudRuntimeException.class,
() -> unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, null, true, true));

Assert.assertTrue(exception.getMessage().contains("Please provide convertinstancepoolid"));
}

@Test
public void testResolveImplicitConversionStoragePoolIdFailsWhenNfsAndLinstorPoolsFoundForVddk() {
ClusterVO cluster = getClusterForTests();
StoragePoolVO nfsPool = mock(StoragePoolVO.class);
StoragePoolVO linstorPool = mock(StoragePoolVO.class);
when(primaryDataStoreDao.findClusterWideStoragePoolsByHypervisorAndPoolType(cluster.getId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.NetworkFilesystem))
.thenReturn(List.of(nfsPool));
when(primaryDataStoreDao.findZoneWideStoragePoolsByHypervisorAndPoolType(cluster.getDataCenterId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.Linstor))
.thenReturn(List.of(linstorPool));

CloudRuntimeException exception = Assert.assertThrows(CloudRuntimeException.class,
() -> unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, null, true, true));

Assert.assertTrue(exception.getMessage().contains("Please provide convertinstancepoolid"));
}

@Test
public void testResolveImplicitConversionStoragePoolIdFailsWhenNoSuitablePoolsFound() {
ClusterVO cluster = getClusterForTests();

CloudRuntimeException exception = Assert.assertThrows(CloudRuntimeException.class,
() -> unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, null, true, true));

Assert.assertTrue(exception.getMessage().contains("no suitable primary storage pool"));
}

@Test
public void testResolveImplicitConversionStoragePoolIdSkipsInferenceForStagedConversion() {
ClusterVO cluster = mock(ClusterVO.class);

Assert.assertNull(unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, null, false, true));
Assert.assertNull(unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, null, false, false));

Mockito.verifyNoInteractions(primaryDataStoreDao);
}

@Test
public void testResolveImplicitConversionStoragePoolIdKeepsExplicitPoolForStagedConversion() {
ClusterVO cluster = mock(ClusterVO.class);

Assert.assertEquals(Long.valueOf(42L), unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, 42L, false, true));
Assert.assertEquals(Long.valueOf(42L), unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, 42L, false, false));

Mockito.verifyNoInteractions(primaryDataStoreDao);
}

@Test
public void testResolveImplicitConversionStoragePoolIdUsesNfsAndIgnoresBlockPoolsForOvf() {
ClusterVO cluster = getClusterForTests();
StoragePoolVO nfsPool = mock(StoragePoolVO.class);
when(nfsPool.getId()).thenReturn(42L);
when(primaryDataStoreDao.findClusterWideStoragePoolsByHypervisorAndPoolType(cluster.getId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.NetworkFilesystem))
.thenReturn(List.of(nfsPool));
lenient().when(primaryDataStoreDao.findZoneWideStoragePoolsByHypervisorAndPoolType(cluster.getDataCenterId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.RBD))
.thenReturn(List.of(mock(StoragePoolVO.class)));
lenient().when(primaryDataStoreDao.findClusterWideStoragePoolsByHypervisorAndPoolType(cluster.getId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.Linstor))
.thenReturn(List.of(mock(StoragePoolVO.class)));

Long storagePoolId = unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, null, true, false);

Assert.assertEquals(Long.valueOf(42L), storagePoolId);
}

@Test
public void testResolveImplicitConversionStoragePoolIdFailsWhenOnlyBlockPoolsFoundForOvf() {
ClusterVO cluster = getClusterForTests();
lenient().when(primaryDataStoreDao.findZoneWideStoragePoolsByHypervisorAndPoolType(cluster.getDataCenterId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.RBD))
.thenReturn(List.of(mock(StoragePoolVO.class)));
lenient().when(primaryDataStoreDao.findClusterWideStoragePoolsByHypervisorAndPoolType(cluster.getId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.Linstor))
.thenReturn(List.of(mock(StoragePoolVO.class)));

CloudRuntimeException exception = Assert.assertThrows(CloudRuntimeException.class,
() -> unmanagedVMsManager.resolveImplicitConversionStoragePoolId(cluster, null, true, false));

Assert.assertTrue(exception.getMessage().contains("no suitable primary storage pool"));
}

@Test
public void testImportVmInfersRbdPoolBeforeDirectConversionHostSelection() throws ResourceAllocationException {
ClusterVO cluster = getClusterForTests();
StoragePoolVO rbdPool = mock(StoragePoolVO.class);
when(rbdPool.getId()).thenReturn(42L);
when(rbdPool.getPoolType()).thenReturn(Storage.StoragePoolType.RBD);
when(primaryDataStoreDao.findZoneWideStoragePoolsByHypervisorAndPoolType(cluster.getDataCenterId(),
Hypervisor.HypervisorType.KVM, Storage.StoragePoolType.RBD))
.thenReturn(List.of(rbdPool));
when(primaryDataStoreDao.findById(42L)).thenReturn(rbdPool);

ImportVmCmd cmd = mock(ImportVmCmd.class);
when(cmd.getExistingVcenterId()).thenReturn(null);
when(cmd.getVcenter()).thenReturn("vcenter.example.com");
when(cmd.getDatacenterName()).thenReturn("datacenter");
when(cmd.getUsername()).thenReturn("administrator@vsphere.local");
when(cmd.getPassword()).thenReturn("password");
when(cmd.getConvertInstanceHostId()).thenReturn(null);
when(cmd.getConvertStoragePoolId()).thenReturn(null);
when(cmd.getForceConvertToPool()).thenReturn(true);
when(cmd.getUseVddk()).thenReturn(true);
ServiceOfferingVO serviceOffering = mock(ServiceOfferingVO.class);
when(serviceOffering.getDiskOfferingId()).thenReturn(null);
Map<String, Long> dataDiskOfferingMap = Collections.emptyMap();

// Stop at host selection so the test exercises pool inference and validation without starting a migration.
IllegalStateException reachedDirectRbdHostSelection = new IllegalStateException("Direct RBD host selection reached");
Mockito.doThrow(reachedDirectRbdHostSelection).when(unmanagedVMsManager)
.selectKVMHostForConversionInCluster(cluster, null, true, true, null);

IllegalStateException exception = Assert.assertThrows(IllegalStateException.class,
() -> unmanagedVMsManager.importUnmanagedInstanceFromVmwareToKvm(
mock(DataCenter.class), cluster, templateMock, "source-vm", "imported-vm", null,
accountMock, accountMock, 1L, serviceOffering, dataDiskOfferingMap,
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), cmd, false));

Assert.assertSame(reachedDirectRbdHostSelection, exception);
verify(unmanagedVMsManager).checkConversionStoragePool(42L, true, true);
verify(unmanagedVMsManager).validateSelectedConversionStoragePoolForVddk(
true, true, 42L, serviceOffering, dataDiskOfferingMap);
}

@Test
public void testCheckConversionStoragePoolPrimaryStagingPool() {
StoragePoolVO destPool = mock(StoragePoolVO.class);
Expand Down
37 changes: 28 additions & 9 deletions ui/src/views/tools/ImportUnmanagedInstance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1161,11 +1161,7 @@ export default {
}
getAPI('listStoragePools', params).then(json => {
this.storagePoolsForConversion = json.liststoragepoolsresponse.storagepool || []
// Keep selected pool state aligned when the value is auto-populated by v-model.
if (this.form.convertstoragepoolid) {
const poolExists = this.storagePoolsForConversion.some(pool => pool.id === this.form.convertstoragepoolid)
this.selectedStoragePoolForConversion = poolExists ? this.form.convertstoragepoolid : null
}
this.syncSelectedStoragePoolForConversion()
})
} else if (this.selectedStorageOptionForConversion === 'local') {
const kvmHost = this.kvmHostsForConversion.filter(x => x.id === this.selectedKvmHostForConversion)[0]
Expand All @@ -1175,13 +1171,25 @@ export default {
status: 'Up'
}).then(json => {
this.storagePoolsForConversion = json.liststoragepoolsresponse.storagepool || []
if (this.form.convertstoragepoolid) {
const poolExists = this.storagePoolsForConversion.some(pool => pool.id === this.form.convertstoragepoolid)
this.selectedStoragePoolForConversion = poolExists ? this.form.convertstoragepoolid : null
}
this.syncSelectedStoragePoolForConversion()
})
}
},
syncSelectedStoragePoolForConversion () {
if (this.form.convertstoragepoolid) {
const poolExists = this.storagePoolsForConversion.some(pool => pool.id === this.form.convertstoragepoolid)
if (poolExists) {
this.selectedStoragePoolForConversion = this.form.convertstoragepoolid
return
}
this.form.convertstoragepoolid = null
this.selectedStoragePoolForConversion = null
}
if (this.storagePoolsForConversion.length === 1) {
this.form.convertstoragepoolid = this.storagePoolsForConversion[0].id
this.selectedStoragePoolForConversion = this.form.convertstoragepoolid
}
},
updateSelectedKvmHostForImporting (clusterid, checked, value) {
if (checked) {
this.selectedKvmHostForImporting = value
Expand Down Expand Up @@ -1238,6 +1246,9 @@ export default {
this.selectedStoragePoolForConversion = null
this.showStoragePoolsForConversion = false
this.resetStorageOptionsForConversion()
if (val) {
this.selectPrimaryStorageForForcedConversion()
}
},
onVmwareMigrationModeChange (event) {
this.setVmwareMigrationMode(event.target.value, true)
Expand Down Expand Up @@ -1269,6 +1280,14 @@ export default {
this.fetchKvmHostsForConversion()
}
this.resetStorageOptionsForConversion()
if (val) {
this.selectPrimaryStorageForForcedConversion()
}
},
selectPrimaryStorageForForcedConversion () {
this.selectedStorageOptionForConversion = 'primary'
this.showStoragePoolsForConversion = true
this.fetchStoragePoolsForConversion()
Comment on lines +1287 to +1290
},
updateSelectedRootDisk () {
var rootDisk = this.resource.disk[this.selectedRootDiskIndex]
Expand Down
Loading