Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vishesh92 committed Oct 16, 2024
1 parent 7137286 commit 3d00943
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public class ApiConstants {
public static final String ICMP_TYPE = "icmptype";
public static final String ID = "id";
public static final String IDS = "ids";
public static final String IMPORT_INSTANCE_HOST_ID = "importinstancehostid";
public static final String INDEX = "index";
public static final String INSTANCES_DISKS_STATS_RETENTION_ENABLED = "instancesdisksstatsretentionenabled";
public static final String INSTANCES_DISKS_STATS_RETENTION_TIME = "instancesdisksstatsretentiontime";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,13 @@ public class ImportVmCmd extends ImportUnmanagedInstanceCmd {
private String clusterName;

@Parameter(name = ApiConstants.CONVERT_INSTANCE_HOST_ID, type = CommandType.UUID, entityType = HostResponse.class,
description = "(only for importing VMs from VMware to KVM) optional - the host to perform the virt-v2v migration from VMware to KVM.")
description = "(only for importing VMs from VMware to KVM) optional - the host to perform the virt-v2v conversion from VMware to KVM.")
private Long convertInstanceHostId;

@Parameter(name = ApiConstants.IMPORT_INSTANCE_HOST_ID, type = CommandType.UUID, entityType = HostResponse.class, since = "4.19.2",
description = "(only for importing VMs from VMware to KVM) optional - the host to use to import the converted instance for migration from VMware to KVM.")
private Long importInstanceHostId;

@Parameter(name = ApiConstants.CONVERT_INSTANCE_STORAGE_POOL_ID, type = CommandType.UUID, entityType = StoragePoolResponse.class,
description = "(only for importing VMs from VMware to KVM) optional - the temporary storage pool to perform the virt-v2v migration from VMware to KVM.")
private Long convertStoragePoolId;
Expand Down Expand Up @@ -201,6 +205,10 @@ public Long getConvertInstanceHostId() {
return convertInstanceHostId;
}

public Long getImportInstanceHostId() {
return importInstanceHostId;
}

Check warning on line 210 in api/src/main/java/org/apache/cloudstack/api/command/admin/vm/ImportVmCmd.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/apache/cloudstack/api/command/admin/vm/ImportVmCmd.java#L208-L210

Added lines #L208 - L210 were not covered by tests

public Long getConvertStoragePoolId() {
return convertStoragePoolId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,7 @@ protected UserVm importUnmanagedInstanceFromVmwareToKvm(DataCenter zone, Cluster
String clusterName = cmd.getClusterName();
String sourceHostName = cmd.getHostIp();
Long convertInstanceHostId = cmd.getConvertInstanceHostId();
Long importInstanceHostId = cmd.getImportInstanceHostId();
Long convertStoragePoolId = cmd.getConvertStoragePoolId();

if ((existingVcenterId == null && vcenter == null) || (existingVcenterId != null && vcenter != null)) {
Expand Down Expand Up @@ -1599,8 +1600,8 @@ protected UserVm importUnmanagedInstanceFromVmwareToKvm(DataCenter zone, Cluster
DataStoreTO temporaryConvertLocation = null;
String ovfTemplateOnConvertLocation = null;
try {
HostVO convertHost = selectInstanceConversionKVMHostInCluster(destinationCluster, convertInstanceHostId);
HostVO importHost = convertInstanceHostId == null ? convertHost : selectInstanceConversionKVMHostInCluster(destinationCluster, null);
HostVO convertHost = selectKVMHostForConversionInCluster(destinationCluster, convertInstanceHostId);
HostVO importHost = selectKVMHostForImportingInCluster(destinationCluster, importInstanceHostId);
CheckConvertInstanceAnswer conversionSupportAnswer = checkConversionSupportOnHost(convertHost, sourceVMName, false);
LOGGER.debug(String.format("The host %s (%s) is selected to execute the conversion of the instance %s" +
" from VMware to KVM ", convertHost.getId(), convertHost.getName(), sourceVMName));
Expand Down Expand Up @@ -1787,17 +1788,54 @@ private Map<String, String> createParamsForRemoveClonedInstance(String vcenter,
return params;
}

private HostVO selectInstanceConversionKVMHostInCluster(Cluster destinationCluster, Long convertInstanceHostId) {
private HostVO selectKVMHostForImportingInCluster(Cluster destinationCluster, Long importInstanceHostId) {
if (importInstanceHostId != null) {
HostVO selectedHost = hostDao.findById(importInstanceHostId);
if (selectedHost == null) {
String msg = String.format("Cannot find host with ID %s", importInstanceHostId);
LOGGER.error(msg);
throw new CloudRuntimeException(msg);

Check warning on line 1797 in server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java#L1795-L1797

Added lines #L1795 - L1797 were not covered by tests
}
if (selectedHost.getResourceState() != ResourceState.Enabled ||
selectedHost.getStatus() != Status.Up || selectedHost.getType() != Host.Type.Routing ||
destinationCluster.getDataCenterId() != selectedHost.getDataCenterId() ||
selectedHost.getClusterId() != destinationCluster.getId()
) {
String msg = String.format(

Check warning on line 1804 in server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1804 was not covered by tests
"Cannot import the converted instance on the host %s as it is not a running and Enabled host",
selectedHost.getName());
LOGGER.error(msg);
throw new CloudRuntimeException(msg);

Check warning on line 1808 in server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java#L1806-L1808

Added lines #L1806 - L1808 were not covered by tests
}
return selectedHost;
}

List<HostVO> hosts = hostDao.listByClusterAndHypervisorType(destinationCluster.getId(), destinationCluster.getHypervisorType());

Check warning on line 1813 in server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1813 was not covered by tests
if (CollectionUtils.isNotEmpty(hosts)) {
return hosts.get(new Random().nextInt(hosts.size()));

Check warning on line 1815 in server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1815 was not covered by tests
}

String err = String.format(

Check warning on line 1818 in server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1818 was not covered by tests
"Could not find any suitable %s host in cluster %s to import the converted instance",
destinationCluster.getHypervisorType(), destinationCluster.getName());
LOGGER.error(err);
throw new CloudRuntimeException(err);

Check warning on line 1822 in server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java#L1820-L1822

Added lines #L1820 - L1822 were not covered by tests
}

private HostVO selectKVMHostForConversionInCluster(Cluster destinationCluster, Long convertInstanceHostId) {
if (convertInstanceHostId != null) {
HostVO selectedHost = hostDao.findById(convertInstanceHostId);
if (selectedHost == null) {
String msg = String.format("Cannot find host with ID %s", convertInstanceHostId);
LOGGER.error(msg);
throw new CloudRuntimeException(msg);
}
if (selectedHost.getResourceState() != ResourceState.Enabled ||
selectedHost.getStatus() != Status.Up || selectedHost.getType() != Host.Type.Routing) {
String msg = String.format("Cannot perform the conversion on the host %s as it is not a running and Enabled host", selectedHost.getName());

if (!List.of(ResourceState.Enabled, ResourceState.Disabled).contains(selectedHost.getResourceState()) ||
selectedHost.getStatus() != Status.Up || selectedHost.getType() != Host.Type.Routing ||
destinationCluster.getDataCenterId() != selectedHost.getDataCenterId()
) {
String msg = String.format("Cannot perform the conversion on the host %s as it is not running", selectedHost.getName());

Check warning on line 1838 in server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1838 was not covered by tests
LOGGER.error(msg);
throw new CloudRuntimeException(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,13 @@ private void baseTestImportVmFromVmwareToKvm(VcenterParameter vcenterParameter,
when(convertHost.getId()).thenReturn(convertHostId);
when(convertHost.getName()).thenReturn("KVM-Convert-Host");
when(convertHost.getType()).thenReturn(Host.Type.Routing);
when(convertHost.getDataCenterId()).thenReturn(zoneId);
when(convertHost.getClusterId()).thenReturn(clusterId);
if (selectConvertHost) {
when(importVmCmd.getConvertInstanceHostId()).thenReturn(convertHostId);
when(importVmCmd.getImportInstanceHostId()).thenReturn(convertHostId);
when(hostDao.findById(convertHostId)).thenReturn(convertHost);
}
when(hostDao.listByClusterHypervisorTypeAndHostCapability(clusterId, Hypervisor.HypervisorType.KVM, Host.HOST_INSTANCE_CONVERSION)).thenReturn(List.of(convertHost));

DataStoreTO dataStoreTO = mock(DataStoreTO.class);
DataStore dataStore = mock(DataStore.class);
Expand Down
34 changes: 34 additions & 0 deletions ui/src/views/tools/ImportUnmanagedInstance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@
@handle-checkselectpair-change="updateSelectedKvmHostForConversion"
/>
</a-form-item>
<a-form-item name="importhostid" ref="importhostid">
<check-box-select-pair
layout="vertical"
v-if="cluster.hypervisortype === 'KVM' && selectedVmwareVcenter"
:resourceKey="cluster.id"
:selectOptions="kvmHostsForImporting"
:checkBoxLabel="$t('message.select.kvm.host.instance.conversion')"
:defaultCheckBoxValue="false"
:reversed="false"
@handle-checkselectpair-change="updateSelectedKvmHostForImporting"
/>
</a-form-item>
<a-form-item name="convertstorageoption" ref="convertstorageoption">
<check-box-select-pair
layout="vertical"
Expand Down Expand Up @@ -494,7 +506,9 @@ export default {
switches: {},
loading: false,
kvmHostsForConversion: [],
kvmHostsForImporting: [],
selectedKvmHostForConversion: null,
selectedKvmHostForImporting: null,
storageOptionsForConversion: [
{
id: 'secondary',
Expand Down Expand Up @@ -730,6 +744,7 @@ export default {
page: 1
})
this.fetchKvmHostsForConversion()
this.fetchKvmHostsForImporting()
if (this.resource?.disk?.length > 1) {
this.updateSelectedRootDisk()
}
Expand Down Expand Up @@ -930,6 +945,17 @@ export default {
})
})
},
fetchKvmHostsForImporting () {
api('listHosts', {
clusterid: this.cluster.id,
hypervisor: this.cluster.hypervisortype,
type: 'Routing',
state: 'Up',
resourcestate: 'Enabled'
}).then(json => {
this.kvmHostsForImporting = json.listhostsresponse.host || []
})
},
fetchStoragePoolsForConversion () {
if (this.selectedStorageOptionForConversion === 'primary') {
const params = {
Expand All @@ -956,6 +982,14 @@ export default {
})
}
},
updateSelectedKvmHostForImporting (clusterid, checked, value) {
if (checked) {
this.selectedKvmHostForImporting = value
} else {
this.selectedKvmHostForImporting = null
this.resetStorageOptionsForConversion()
}
},
updateSelectedKvmHostForConversion (clusterid, checked, value) {
if (checked) {
this.selectedKvmHostForConversion = value
Expand Down

0 comments on commit 3d00943

Please sign in to comment.