Skip to content

Commit 360b8ac

Browse files
sureshanapartidhslove
authored andcommitted
VMware Import - Support external VMs in any folders/subfolders other than the root folder ('vm') of datacenter (apache#10411)
1 parent a91155c commit 360b8ac

6 files changed

Lines changed: 52 additions & 2 deletions

File tree

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
@@ -54,6 +54,7 @@ public Answer execute(ConvertInstanceCommand cmd, LibvirtComputingResource serve
5454
RemoteInstanceTO sourceInstance = cmd.getSourceInstance();
5555
Hypervisor.HypervisorType sourceHypervisorType = sourceInstance.getHypervisorType();
5656
String sourceInstanceName = sourceInstance.getInstanceName();
57+
String sourceInstancePath = sourceInstance.getInstancePath();
5758
Hypervisor.HypervisorType destinationHypervisorType = cmd.getDestinationHypervisorType();
5859
DataStoreTO conversionTemporaryLocation = cmd.getConversionTemporaryLocation();
5960
long timeout = (long) cmd.getWait() * 1000;
@@ -171,9 +172,15 @@ private String getExportOVAUrlFromRemoteInstance(RemoteInstanceTO vmwareInstance
171172
String password = vmwareInstance.getVcenterPassword();
172173
String datacenter = vmwareInstance.getDatacenterName();
173174
String vm = vmwareInstance.getInstanceName();
175+
String path = vmwareInstance.getInstancePath();
174176

175177
String encodedUsername = encodeUsername(username);
176178
String encodedPassword = encodeUsername(password);
179+
if (StringUtils.isNotBlank(path)) {
180+
s_logger.info("VM path: " + path);
181+
return String.format("vi://%s:%s@%s/%s/%s/%s",
182+
encodedUsername, encodedPassword, vcenter, datacenter, path, vm);
183+
}
177184
return String.format("vi://%s:%s@%s/%s/vm/%s",
178185
encodedUsername, encodedPassword, vcenter, datacenter, vm);
179186
}

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

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

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

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,32 @@ public VirtualMachineFileLayoutEx getFileLayout() throws Exception {
889889
return fileLayout;
890890
}
891891

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

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
@@ -802,6 +802,7 @@ public static UnmanagedInstanceTO getUnmanagedInstance(VmwareHypervisorHost hype
802802
instance = new UnmanagedInstanceTO();
803803
instance.setName(vmMo.getVmName());
804804
instance.setInternalCSName(vmMo.getInternalCSName());
805+
instance.setPath((vmMo.getPath()));
805806
instance.setCpuCoresPerSocket(vmMo.getCoresPerSocket());
806807
instance.setOperatingSystemId(vmMo.getVmGuestInfo().getGuestId());
807808
VirtualMachineConfigSummary configSummary = vmMo.getConfigSummary();

0 commit comments

Comments
 (0)