Skip to content

Commit b9ebc7b

Browse files
VMware Import - Support external VMs in any folders/subfolders other than the root folder ('vm') of datacenter (#10411)
1 parent b6cebe2 commit b9ebc7b

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

api/src/main/java/com/cloud/agent/api/to/RemoteInstanceTO.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class RemoteInstanceTO implements Serializable {
2727

2828
private Hypervisor.HypervisorType hypervisorType;
2929
private String instanceName;
30+
private String instancePath;
3031

3132
// VMware Remote Instances parameters (required for exporting OVA through ovftool)
3233
// TODO: cloud.agent.transport.Request#getCommands() cannot handle gsoc decode for polymorphic classes
@@ -44,9 +45,10 @@ public RemoteInstanceTO(String instanceName) {
4445
this.instanceName = instanceName;
4546
}
4647

47-
public RemoteInstanceTO(String instanceName, String vcenterHost, String vcenterUsername, String vcenterPassword, String datacenterName) {
48+
public RemoteInstanceTO(String instanceName, String instancePath, String vcenterHost, String vcenterUsername, String vcenterPassword, String datacenterName) {
4849
this.hypervisorType = Hypervisor.HypervisorType.VMware;
4950
this.instanceName = instanceName;
51+
this.instancePath = instancePath;
5052
this.vcenterHost = vcenterHost;
5153
this.vcenterUsername = vcenterUsername;
5254
this.vcenterPassword = vcenterPassword;
@@ -61,6 +63,10 @@ public String getInstanceName() {
6163
return this.instanceName;
6264
}
6365

66+
public String getInstancePath() {
67+
return this.instancePath;
68+
}
69+
6470
public String getVcenterUsername() {
6571
return vcenterUsername;
6672
}

api/src/main/java/org/apache/cloudstack/vm/UnmanagedInstanceTO.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public enum PowerState {
3333

3434
private String internalCSName;
3535

36+
private String path;
37+
3638
private PowerState powerState;
3739

3840
private PowerState cloneSourcePowerState;
@@ -75,6 +77,14 @@ public void setInternalCSName(String internalCSName) {
7577
this.internalCSName = internalCSName;
7678
}
7779

80+
public String getPath() {
81+
return path;
82+
}
83+
84+
public void setPath(String path) {
85+
this.path = path;
86+
}
87+
7888
public PowerState getPowerState() {
7989
return powerState;
8090
}

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConvertInstanceCommandWrapper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public Answer execute(ConvertInstanceCommand cmd, LibvirtComputingResource serve
5757
RemoteInstanceTO sourceInstance = cmd.getSourceInstance();
5858
Hypervisor.HypervisorType sourceHypervisorType = sourceInstance.getHypervisorType();
5959
String sourceInstanceName = sourceInstance.getInstanceName();
60+
String sourceInstancePath = sourceInstance.getInstancePath();
6061
Hypervisor.HypervisorType destinationHypervisorType = cmd.getDestinationHypervisorType();
6162
DataStoreTO conversionTemporaryLocation = cmd.getConversionTemporaryLocation();
6263
long timeout = (long) cmd.getWait() * 1000;
@@ -177,9 +178,15 @@ private String getExportOVAUrlFromRemoteInstance(RemoteInstanceTO vmwareInstance
177178
String password = vmwareInstance.getVcenterPassword();
178179
String datacenter = vmwareInstance.getDatacenterName();
179180
String vm = vmwareInstance.getInstanceName();
181+
String path = vmwareInstance.getInstancePath();
180182

181183
String encodedUsername = encodeUsername(username);
182184
String encodedPassword = encodeUsername(password);
185+
if (StringUtils.isNotBlank(path)) {
186+
s_logger.info("VM path: " + path);
187+
return String.format("vi://%s:%s@%s/%s/%s/%s",
188+
encodedUsername, encodedPassword, vcenter, datacenter, path, vm);
189+
}
183190
return String.format("vi://%s:%s@%s/%s/vm/%s",
184191
encodedUsername, encodedPassword, vcenter, datacenter, vm);
185192
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,7 @@ private UnmanagedInstanceTO convertVmwareInstanceToKVMAfterExportingOVFToConvert
19471947
LOGGER.debug(String.format("Delegating the conversion of instance %s from VMware to KVM to the host %s (%s) after OVF export through ovftool",
19481948
sourceVM, convertHost.getId(), convertHost.getName()));
19491949

1950-
RemoteInstanceTO remoteInstanceTO = new RemoteInstanceTO(sourceVMwareInstance.getName(), vcenterHost, vcenterUsername, vcenterPassword, datacenterName);
1950+
RemoteInstanceTO remoteInstanceTO = new RemoteInstanceTO(sourceVMwareInstance.getName(), sourceVMwareInstance.getPath(), vcenterHost, vcenterUsername, vcenterPassword, datacenterName);
19511951
List<String> destinationStoragePools = selectInstanceConversionStoragePools(convertStoragePools, sourceVMwareInstance.getDisks(), serviceOffering, dataDiskOfferingMap);
19521952
ConvertInstanceCommand cmd = new ConvertInstanceCommand(remoteInstanceTO,
19531953
Hypervisor.HypervisorType.KVM, temporaryConvertLocation, null, false, true);

vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,27 @@ public VirtualMachineFileLayoutEx getFileLayout() throws Exception {
891891
return fileLayout;
892892
}
893893

894+
public String getPath() throws Exception {
895+
List<String> subPaths = new ArrayList<>();
896+
ManagedObjectReference mor = _context.getVimClient().getDynamicProperty(_mor, "parent");
897+
while (mor != null && mor.getType().equalsIgnoreCase("Folder")) {
898+
String subPath = _context.getVimClient().getDynamicProperty(mor, "name");
899+
if (StringUtils.isBlank(subPath)) {
900+
return null;
901+
}
902+
subPaths.add(subPath);
903+
mor = _context.getVimClient().getDynamicProperty(mor, "parent");
904+
}
905+
906+
if (!subPaths.isEmpty()) {
907+
Collections.reverse(subPaths);
908+
String path = StringUtils.join(subPaths, "/");
909+
return path;
910+
}
911+
912+
return null;
913+
}
914+
894915
@Override
895916
public ManagedObjectReference getParentMor() throws Exception {
896917
return _context.getVimClient().getDynamicProperty(_mor, "parent");

vmware-base/src/main/java/com/cloud/hypervisor/vmware/util/VmwareHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ public static UnmanagedInstanceTO getUnmanagedInstance(VmwareHypervisorHost hype
801801
instance = new UnmanagedInstanceTO();
802802
instance.setName(vmMo.getVmName());
803803
instance.setInternalCSName(vmMo.getInternalCSName());
804+
instance.setPath((vmMo.getPath()));
804805
instance.setCpuCoresPerSocket(vmMo.getCoresPerSocket());
805806
instance.setOperatingSystemId(vmMo.getVmGuestInfo().getGuestId());
806807
VirtualMachineConfigSummary configSummary = vmMo.getConfigSummary();

0 commit comments

Comments
 (0)