shwstppr commented on a change in pull request #4294:
URL: https://github.com/apache/cloudstack/pull/4294#discussion_r488499852



##########
File path: 
plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareStorageProcessor.java
##########
@@ -1186,31 +1186,43 @@ public Answer createTemplateFromVolume(CopyCommand cmd) 
{
         String volumePath = volume.getPath();
 
         String details = null;
-
+        VirtualMachineMO vmMo = null;
+        VirtualMachineMO workerVmMo = null;
         VmwareContext context = hostService.getServiceContext(cmd);
         try {
             VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, 
cmd);
-
-            VirtualMachineMO vmMo = 
hyperHost.findVmOnHyperHost(volume.getVmName());
-            if (vmMo == null) {
-                if (s_logger.isDebugEnabled()) {
-                    s_logger.debug("Unable to find the owner VM for 
CreatePrivateTemplateFromVolumeCommand on host " + hyperHost.getHyperHostName() 
+
-                            ", try within datacenter");
-                }
-                vmMo = hyperHost.findVmOnPeerHyperHost(volume.getVmName());
-
+            if (volume.getVmName() == null) {
+                ManagedObjectReference secMorDs = 
HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, 
volume.getDataStore().getUuid());
+                DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), 
secMorDs);
+                workerVmMo = HypervisorHostHelper.createWorkerVM(hyperHost, 
dsMo, "workervm"+volume.getUuid());
+                if (workerVmMo == null) {
+                    throw new Exception("Unable to find created worker VM");
+                }
+                vmMo = workerVmMo;
+                String vmdkDataStorePath = 
VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, 
volumePath + ".vmdk");
+                vmMo.attachDisk(new String[] {vmdkDataStorePath}, secMorDs);
+            } else {
+                vmMo = hyperHost.findVmOnHyperHost(volume.getVmName());
                 if (vmMo == null) {
-                    // This means either the volume is on a zone wide storage 
pool or VM is deleted by external entity.
-                    // Look for the VM in the datacenter.
-                    ManagedObjectReference dcMor = 
hyperHost.getHyperHostDatacenter();
-                    DatacenterMO dcMo = new DatacenterMO(context, dcMor);
-                    vmMo = dcMo.findVm(volume.getVmName());
-                }
+                    if (s_logger.isDebugEnabled()) {
+                        s_logger.debug("Unable to find the owner VM for 
CreatePrivateTemplateFromVolumeCommand on host " + hyperHost.getHyperHostName() 
+
+                                ", try within datacenter");
+                    }
+                    vmMo = hyperHost.findVmOnPeerHyperHost(volume.getVmName());
 
-                if (vmMo == null) {
-                    String msg = "Unable to find the owner VM for volume 
operation. vm: " + volume.getVmName();
-                    s_logger.error(msg);
-                    throw new Exception(msg);
+                    if (vmMo == null) {
+                        // This means either the volume is on a zone wide 
storage pool or VM is deleted by external entity.
+                        // Look for the VM in the datacenter.
+                        ManagedObjectReference dcMor = 
hyperHost.getHyperHostDatacenter();
+                        DatacenterMO dcMo = new DatacenterMO(context, dcMor);
+                        vmMo = dcMo.findVm(volume.getVmName());
+                    }
+
+                    if (vmMo == null) {
+                        String msg = "Unable to find the owner VM for volume 
operation. vm: " + volume.getVmName();
+                        s_logger.error(msg);
+                        throw new Exception(msg);
+                    }

Review comment:
       Don't think it matters where we find the VM on datacenter but it is fine 
unless we want to lot of refactoring




----------------------------------------------------------------
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.

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


Reply via email to