VMware Import - Support external VMs in any folders/subfolders other than the root folder ('vm') of datacenter (#10411)

This commit is contained in:
Suresh Kumar Anaparti 2025-02-18 13:53:25 +05:30 committed by GitHub
parent b6cebe22f9
commit b9ebc7b721
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 47 additions and 2 deletions

View File

@ -27,6 +27,7 @@ public class RemoteInstanceTO implements Serializable {
private Hypervisor.HypervisorType hypervisorType;
private String instanceName;
private String instancePath;
// VMware Remote Instances parameters (required for exporting OVA through ovftool)
// TODO: cloud.agent.transport.Request#getCommands() cannot handle gsoc decode for polymorphic classes
@ -44,9 +45,10 @@ public class RemoteInstanceTO implements Serializable {
this.instanceName = instanceName;
}
public RemoteInstanceTO(String instanceName, String vcenterHost, String vcenterUsername, String vcenterPassword, String datacenterName) {
public RemoteInstanceTO(String instanceName, String instancePath, String vcenterHost, String vcenterUsername, String vcenterPassword, String datacenterName) {
this.hypervisorType = Hypervisor.HypervisorType.VMware;
this.instanceName = instanceName;
this.instancePath = instancePath;
this.vcenterHost = vcenterHost;
this.vcenterUsername = vcenterUsername;
this.vcenterPassword = vcenterPassword;
@ -61,6 +63,10 @@ public class RemoteInstanceTO implements Serializable {
return this.instanceName;
}
public String getInstancePath() {
return this.instancePath;
}
public String getVcenterUsername() {
return vcenterUsername;
}

View File

@ -33,6 +33,8 @@ public class UnmanagedInstanceTO {
private String internalCSName;
private String path;
private PowerState powerState;
private PowerState cloneSourcePowerState;
@ -75,6 +77,14 @@ public class UnmanagedInstanceTO {
this.internalCSName = internalCSName;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public PowerState getPowerState() {
return powerState;
}

View File

@ -57,6 +57,7 @@ public class LibvirtConvertInstanceCommandWrapper extends CommandWrapper<Convert
RemoteInstanceTO sourceInstance = cmd.getSourceInstance();
Hypervisor.HypervisorType sourceHypervisorType = sourceInstance.getHypervisorType();
String sourceInstanceName = sourceInstance.getInstanceName();
String sourceInstancePath = sourceInstance.getInstancePath();
Hypervisor.HypervisorType destinationHypervisorType = cmd.getDestinationHypervisorType();
DataStoreTO conversionTemporaryLocation = cmd.getConversionTemporaryLocation();
long timeout = (long) cmd.getWait() * 1000;
@ -177,9 +178,15 @@ public class LibvirtConvertInstanceCommandWrapper extends CommandWrapper<Convert
String password = vmwareInstance.getVcenterPassword();
String datacenter = vmwareInstance.getDatacenterName();
String vm = vmwareInstance.getInstanceName();
String path = vmwareInstance.getInstancePath();
String encodedUsername = encodeUsername(username);
String encodedPassword = encodeUsername(password);
if (StringUtils.isNotBlank(path)) {
s_logger.info("VM path: " + path);
return String.format("vi://%s:%s@%s/%s/%s/%s",
encodedUsername, encodedPassword, vcenter, datacenter, path, vm);
}
return String.format("vi://%s:%s@%s/%s/vm/%s",
encodedUsername, encodedPassword, vcenter, datacenter, vm);
}

View File

@ -1947,7 +1947,7 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
LOGGER.debug(String.format("Delegating the conversion of instance %s from VMware to KVM to the host %s (%s) after OVF export through ovftool",
sourceVM, convertHost.getId(), convertHost.getName()));
RemoteInstanceTO remoteInstanceTO = new RemoteInstanceTO(sourceVMwareInstance.getName(), vcenterHost, vcenterUsername, vcenterPassword, datacenterName);
RemoteInstanceTO remoteInstanceTO = new RemoteInstanceTO(sourceVMwareInstance.getName(), sourceVMwareInstance.getPath(), vcenterHost, vcenterUsername, vcenterPassword, datacenterName);
List<String> destinationStoragePools = selectInstanceConversionStoragePools(convertStoragePools, sourceVMwareInstance.getDisks(), serviceOffering, dataDiskOfferingMap);
ConvertInstanceCommand cmd = new ConvertInstanceCommand(remoteInstanceTO,
Hypervisor.HypervisorType.KVM, temporaryConvertLocation, null, false, true);

View File

@ -891,6 +891,27 @@ public class VirtualMachineMO extends BaseMO {
return fileLayout;
}
public String getPath() throws Exception {
List<String> subPaths = new ArrayList<>();
ManagedObjectReference mor = _context.getVimClient().getDynamicProperty(_mor, "parent");
while (mor != null && mor.getType().equalsIgnoreCase("Folder")) {
String subPath = _context.getVimClient().getDynamicProperty(mor, "name");
if (StringUtils.isBlank(subPath)) {
return null;
}
subPaths.add(subPath);
mor = _context.getVimClient().getDynamicProperty(mor, "parent");
}
if (!subPaths.isEmpty()) {
Collections.reverse(subPaths);
String path = StringUtils.join(subPaths, "/");
return path;
}
return null;
}
@Override
public ManagedObjectReference getParentMor() throws Exception {
return _context.getVimClient().getDynamicProperty(_mor, "parent");

View File

@ -801,6 +801,7 @@ public class VmwareHelper {
instance = new UnmanagedInstanceTO();
instance.setName(vmMo.getVmName());
instance.setInternalCSName(vmMo.getInternalCSName());
instance.setPath((vmMo.getPath()));
instance.setCpuCoresPerSocket(vmMo.getCoresPerSocket());
instance.setOperatingSystemId(vmMo.getVmGuestInfo().getGuestId());
VirtualMachineConfigSummary configSummary = vmMo.getConfigSummary();