nvazquez commented on code in PR #7881:
URL: https://github.com/apache/cloudstack/pull/7881#discussion_r1310914643


##########
plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/guru/VMwareGuru.java:
##########
@@ -1217,4 +1222,95 @@ private String getTargetHostGuid(StoragePool 
targetLocalPoolForVM, Long destClus
     protected VirtualMachineTO toVirtualMachineTO(VirtualMachineProfile 
vmProfile) {
         return super.toVirtualMachineTO(vmProfile);
     }
+
+    private VmwareContext connectToVcenter(String vcenter, String username, 
String password) throws Exception {
+        VmwareClient vimClient = new VmwareClient(vcenter);
+        String serviceUrl = "https://"; + vcenter + "/sdk/vimService";
+        vimClient.connect(serviceUrl, username, password);
+        return new VmwareContext(vimClient, vcenter);
+    }
+
+    private void relocateClonedVMToSourceHost(VirtualMachineMO clonedVM, 
HostMO sourceHost) throws Exception {
+        if (!clonedVM.getRunningHost().getMor().equals(sourceHost.getMor())) {
+            s_logger.debug(String.format("Relocating VM to the same host as 
the source VM: %s", sourceHost.getHostName()));
+            if (!clonedVM.relocate(sourceHost.getMor())) {
+                String err = String.format("Cannot relocate cloned VM %s to 
the source host %s", clonedVM.getVmName(), sourceHost.getHostName());
+                s_logger.error(err);
+                throw new CloudRuntimeException(err);
+            }
+        }
+    }
+
+    private VirtualMachineMO createCloneFromSourceVM(String vmName, 
VirtualMachineMO vmMo,
+                                                     DatacenterMO 
dataCenterMO) throws Exception {
+        HostMO sourceHost = vmMo.getRunningHost();
+        String cloneName = String.format("%s-%s", vmName, UUID.randomUUID());
+        DatastoreMO datastoreMO = vmMo.getAllDatastores().get(0); //pick the 
first datastore
+        ManagedObjectReference morPool = 
vmMo.getRunningHost().getHyperHostOwnerResourcePool();
+        boolean result = vmMo.createFullClone(cloneName, 
dataCenterMO.getVmFolder(), morPool, datastoreMO.getMor(), 
Storage.ProvisioningType.THIN);
+        VirtualMachineMO clonedVM = dataCenterMO.findVm(cloneName);
+        if (!result || clonedVM == null) {
+            String err = String.format("Could not clone VM %s before migration 
from VMware", vmName);
+            s_logger.error(err);
+            throw new CloudRuntimeException(err);
+        }
+        relocateClonedVMToSourceHost(clonedVM, sourceHost);
+        return clonedVM;
+    }
+
+    private HypervisorOutOfBandVMClone 
generateResponseFromClone(VirtualMachineMO clonedVM) throws Exception {
+        List<VirtualDisk> virtualDisks = clonedVM.getVirtualDisks();
+        long capacity = 0;
+        for (VirtualDisk disk : virtualDisks) {
+            capacity = Long.sum(capacity, disk.getCapacityInBytes());
+        }
+        return new HypervisorOutOfBandVMClone(clonedVM.getVmName(), 
virtualDisks.size(), capacity);
+    }
+
+    @Override
+    public HypervisorOutOfBandVMClone cloneHypervisorVMOutOfBand(String 
hostIp, String vmName, Map<String, String> params) {
+        s_logger.debug(String.format("Cloning VM %s on external vCenter %s", 
vmName, hostIp));
+        String vcenter = params.get(VmDetailConstants.VMWARE_VCENTER);
+        String datacenter = params.get(VmDetailConstants.VMWARE_DATACENTER);
+        String username = 
params.get(VmDetailConstants.VMWARE_VCENTER_USERNAME);
+        String password = 
params.get(VmDetailConstants.VMWARE_VCENTER_PASSWORD);
+
+        try {
+            VmwareContext context = connectToVcenter(vcenter, username, 
password);
+            DatacenterMO dataCenterMO = new DatacenterMO(context, datacenter);
+            VirtualMachineMO vmMo = dataCenterMO.findVm(vmName);
+            if (vmMo == null) {
+                String err = String.format("Cannot find VM with name %s on 
%s/%s", vmName, vcenter, datacenter);
+                s_logger.error(err);
+                throw new CloudRuntimeException(err);
+            }
+            VirtualMachineMO clonedVM = createCloneFromSourceVM(vmName, vmMo, 
dataCenterMO);
+            HypervisorOutOfBandVMClone clone = 
generateResponseFromClone(clonedVM);
+            s_logger.debug(String.format("VM cloned successfully"));
+            return clone;
+        } catch (Exception e) {
+            String err = String.format("Error cloning VM: %s from external 
vCenter %s: %s", vmName, vcenter, e.getMessage());
+            s_logger.error(err, e);
+            throw new CloudRuntimeException(err, e);
+        }
+    }
+
+    @Override
+    public boolean removeHypervisorVMOutOfBand(String hostIp, String vmName, 
Map<String, String> params) {
+        s_logger.debug(String.format("Removing VM %s on external vCenter %s", 
vmName, hostIp));
+        String vcenter = params.get(VmDetailConstants.VMWARE_VCENTER);
+        String datacenter = params.get(VmDetailConstants.VMWARE_DATACENTER);
+        String username = 
params.get(VmDetailConstants.VMWARE_VCENTER_USERNAME);
+        String password = 
params.get(VmDetailConstants.VMWARE_VCENTER_PASSWORD);
+        try {
+            VmwareContext context = connectToVcenter(vcenter, username, 
password);
+            DatacenterMO dataCenterMO = new DatacenterMO(context, datacenter);
+            VirtualMachineMO vmMo = dataCenterMO.findVm(vmName);
+            return vmMo.destroy();

Review Comment:
   Thanks, fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to