Skip to content

Commit 7137286

Browse files
committed
Add checks for storage pool when convert host is selected
1 parent 124b865 commit 7137286

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,8 @@ protected UserVm importUnmanagedInstanceFromVmwareToKvm(DataCenter zone, Cluster
16051605
LOGGER.debug(String.format("The host %s (%s) is selected to execute the conversion of the instance %s" +
16061606
" from VMware to KVM ", convertHost.getId(), convertHost.getName(), sourceVMName));
16071607

1608-
temporaryConvertLocation = selectInstanceConversionTemporaryLocation(destinationCluster, convertStoragePoolId);
1608+
temporaryConvertLocation = selectInstanceConversionTemporaryLocation(
1609+
destinationCluster, convertHost, convertStoragePoolId);
16091610
List<StoragePoolVO> convertStoragePools = findInstanceConversionStoragePoolsInCluster(destinationCluster);
16101611
long importStartTime = System.currentTimeMillis();
16111612
Pair<UnmanagedInstanceTO, Boolean> sourceInstanceDetails = getSourceVmwareUnmanagedInstance(vcenter, datacenterName, username, password, clusterName, sourceHostName, sourceVMName);
@@ -1969,7 +1970,9 @@ private void logFailureAndThrowException(String msg) {
19691970
throw new CloudRuntimeException(msg);
19701971
}
19711972

1972-
protected DataStoreTO selectInstanceConversionTemporaryLocation(Cluster destinationCluster, Long convertStoragePoolId) {
1973+
protected DataStoreTO selectInstanceConversionTemporaryLocation(Cluster destinationCluster,
1974+
HostVO convertHost,
1975+
Long convertStoragePoolId) {
19731976
if (convertStoragePoolId != null) {
19741977
StoragePoolVO selectedStoragePool = primaryDataStoreDao.findById(convertStoragePoolId);
19751978
if (selectedStoragePool == null) {
@@ -1980,6 +1983,10 @@ protected DataStoreTO selectInstanceConversionTemporaryLocation(Cluster destinat
19801983
logFailureAndThrowException(String.format("Cannot use the storage pool %s for the instance conversion as " +
19811984
"it is not in the scope of the cluster %s", selectedStoragePool.getName(), destinationCluster.getName()));
19821985
}
1986+
if (convertHost != null && selectedStoragePool.getScope() == ScopeType.CLUSTER && !selectedStoragePool.getClusterId().equals(convertHost.getClusterId())) {
1987+
logFailureAndThrowException(String.format("Cannot use the storage pool %s for the instance conversion as " +
1988+
"the host %s for conversion is in a different cluster", selectedStoragePool.getName(), convertHost.getName()));
1989+
}
19831990
if (selectedStoragePool.getScope() == ScopeType.HOST) {
19841991
logFailureAndThrowException(String.format("The storage pool %s is a local storage pool and not supported for temporary conversion location, cluster and zone wide NFS storage pools are supported", selectedStoragePool.getName()));
19851992
} else if (selectedStoragePool.getPoolType() != Storage.StoragePoolType.NetworkFilesystem) {

server/src/test/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImplTest.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ public void testSelectInstanceConversionTemporaryLocationInvalidStorage() {
818818

819819
long poolId = 1L;
820820
when(primaryDataStoreDao.findById(poolId)).thenReturn(null);
821-
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, poolId);
821+
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, null, poolId);
822822
}
823823

824824
@Test(expected = CloudRuntimeException.class)
@@ -829,17 +829,32 @@ public void testSelectInstanceConversionTemporaryLocationPoolInvalidScope() {
829829
Mockito.when(pool.getScope()).thenReturn(ScopeType.CLUSTER);
830830
Mockito.when(pool.getClusterId()).thenReturn(100L);
831831
when(primaryDataStoreDao.findById(poolId)).thenReturn(pool);
832-
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, poolId);
832+
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, null, poolId);
833833
}
834834

835+
836+
@Test(expected = CloudRuntimeException.class)
837+
public void testSelectInstanceConversionTemporaryLocationPoolConvertHostDifferentCluster() {
838+
ClusterVO cluster = getClusterForTests();
839+
long poolId = 1L;
840+
StoragePoolVO pool = mock(StoragePoolVO.class);
841+
Mockito.when(pool.getScope()).thenReturn(ScopeType.CLUSTER);
842+
Mockito.when(pool.getClusterId()).thenReturn(1L);
843+
HostVO host = mock(HostVO.class);
844+
when(primaryDataStoreDao.findById(poolId)).thenReturn(pool);
845+
when(host.getClusterId()).thenReturn(2L);
846+
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, host, poolId);
847+
}
848+
849+
835850
@Test(expected = CloudRuntimeException.class)
836851
public void testSelectInstanceConversionTemporaryLocationLocalStoragePoolInvalid() {
837852
ClusterVO cluster = getClusterForTests();
838853
long poolId = 1L;
839854
StoragePoolVO pool = mock(StoragePoolVO.class);
840855
Mockito.when(pool.getScope()).thenReturn(ScopeType.HOST);
841856
when(primaryDataStoreDao.findById(poolId)).thenReturn(pool);
842-
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, poolId);
857+
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, null, poolId);
843858
}
844859

845860
@Test(expected = CloudRuntimeException.class)
@@ -851,13 +866,13 @@ public void testSelectInstanceConversionTemporaryLocationStoragePoolInvalidType(
851866
Mockito.when(pool.getClusterId()).thenReturn(1L);
852867
when(primaryDataStoreDao.findById(poolId)).thenReturn(pool);
853868
Mockito.when(pool.getPoolType()).thenReturn(Storage.StoragePoolType.RBD);
854-
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, poolId);
869+
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, null, poolId);
855870
}
856871

857872
@Test(expected = CloudRuntimeException.class)
858873
public void testSelectInstanceConversionTemporaryLocationNoPoolAvailable() {
859874
ClusterVO cluster = getClusterForTests();
860875
Mockito.when(imageStoreDao.findOneByZoneAndProtocol(anyLong(), anyString())).thenReturn(null);
861-
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, null);
876+
unmanagedVMsManager.selectInstanceConversionTemporaryLocation(cluster, null, null);
862877
}
863878
}

ui/src/views/tools/ImportUnmanagedInstance.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,10 +932,17 @@ export default {
932932
},
933933
fetchStoragePoolsForConversion () {
934934
if (this.selectedStorageOptionForConversion === 'primary') {
935-
api('listStoragePools', {
935+
const params = {
936936
zoneid: this.cluster.zoneid,
937937
status: 'Up'
938-
}).then(json => {
938+
}
939+
if (this.selectedKvmHostForConversion) {
940+
const kvmHost = this.kvmHostsForConversion.filter(x => x.id === this.selectedKvmHostForConversion)[0]
941+
if (kvmHost.clusterid !== this.cluster.id) {
942+
params.scope = 'ZONE'
943+
}
944+
}
945+
api('listStoragePools', params).then(json => {
939946
this.storagePoolsForConversion = json.liststoragepoolsresponse.storagepool || []
940947
})
941948
} else if (this.selectedStorageOptionForConversion === 'local') {

0 commit comments

Comments
 (0)