DaanHoogland commented on code in PR #7554:
URL: https://github.com/apache/cloudstack/pull/7554#discussion_r1209969429


##########
api/src/main/java/org/apache/cloudstack/api/command/admin/vm/MigrateVirtualMachineWithVolumeCmd.java:
##########
@@ -146,28 +152,31 @@ public ApiCommandResourceType getApiResourceType() {
 
     @Override
     public void execute() {
-        if (hostId == null && MapUtils.isEmpty(migrateVolumeTo)) {
-            throw new InvalidParameterValueException(String.format("Either %s 
or %s must be passed for migrating the VM.", ApiConstants.HOST_ID, 
ApiConstants.MIGRATE_TO));
+        if (hostId == null && MapUtils.isEmpty(migrateVolumeTo) && 
!Boolean.TRUE.equals(autoSelect)) {
+            throw new InvalidParameterValueException(String.format("Either %s 
or %s must be passed or %s must be true for migrating the VM.", 
ApiConstants.HOST_ID, ApiConstants.MIGRATE_TO, ApiConstants.AUTO_SELECT));
         }
 
         VirtualMachine virtualMachine = 
_userVmService.getVm(getVirtualMachineId());
-        if (!VirtualMachine.State.Running.equals(virtualMachine.getState()) && 
hostId != null) {
+        if (!VirtualMachine.State.Running.equals(virtualMachine.getState()) && 
(hostId != null || Boolean.TRUE.equals(autoSelect))) {
             throw new InvalidParameterValueException(String.format("%s is not 
in the Running state to migrate it to the new host.", virtualMachine));

Review Comment:
   not in scope, but I think this meesage is misleading a bit:
   ```suggestion
               throw new InvalidParameterValueException(String.format("%s is 
not in the Running state. It must be running to migrate to a new host.", 
virtualMachine));
   ```



##########
api/src/main/java/org/apache/cloudstack/api/command/admin/vm/MigrateVirtualMachineWithVolumeCmd.java:
##########
@@ -146,28 +152,31 @@ public ApiCommandResourceType getApiResourceType() {
 
     @Override
     public void execute() {
-        if (hostId == null && MapUtils.isEmpty(migrateVolumeTo)) {
-            throw new InvalidParameterValueException(String.format("Either %s 
or %s must be passed for migrating the VM.", ApiConstants.HOST_ID, 
ApiConstants.MIGRATE_TO));
+        if (hostId == null && MapUtils.isEmpty(migrateVolumeTo) && 
!Boolean.TRUE.equals(autoSelect)) {
+            throw new InvalidParameterValueException(String.format("Either %s 
or %s must be passed or %s must be true for migrating the VM.", 
ApiConstants.HOST_ID, ApiConstants.MIGRATE_TO, ApiConstants.AUTO_SELECT));
         }
 
         VirtualMachine virtualMachine = 
_userVmService.getVm(getVirtualMachineId());
-        if (!VirtualMachine.State.Running.equals(virtualMachine.getState()) && 
hostId != null) {
+        if (!VirtualMachine.State.Running.equals(virtualMachine.getState()) && 
(hostId != null || Boolean.TRUE.equals(autoSelect))) {
             throw new InvalidParameterValueException(String.format("%s is not 
in the Running state to migrate it to the new host.", virtualMachine));
         }
 
-        if (!VirtualMachine.State.Stopped.equals(virtualMachine.getState()) && 
hostId == null) {
-            throw new InvalidParameterValueException(String.format("%s is not 
in the Stopped state to migrate, use the %s parameter to migrate it to a new 
host.",
-                    virtualMachine, ApiConstants.HOST_ID));
+        if (!VirtualMachine.State.Stopped.equals(virtualMachine.getState()) && 
hostId == null && Boolean.FALSE.equals(autoSelect)) {
+            throw new InvalidParameterValueException(String.format("%s is not 
in the Stopped state to migrate, use the %s or %s parameter to migrate it to a 
new host.",
+                    virtualMachine, 
ApiConstants.HOST_ID,ApiConstants.AUTO_SELECT));
         }
 
         try {
             VirtualMachine migratedVm = null;
-            if (hostId != null) {
-                Host destinationHost = _resourceService.getHost(getHostId());
-                // OfflineVmwareMigration: destination host would have to not 
be a required parameter for stopped VMs
-                if (destinationHost == null) {
-                    s_logger.error(String.format("Unable to find the host with 
ID [%s].", getHostId()));
-                    throw new InvalidParameterValueException("Unable to find 
the specified host to migrate the VM.");
+            if (getHostId() != null || Boolean.TRUE.equals(autoSelect)) {
+                Host destinationHost = null;
+                if (!Boolean.TRUE.equals(autoSelect)) {

Review Comment:
   this is actually an implicit null check on gethostId(), maybe we can make it 
more explicit for readability:
   ```suggestion
                   if (getHostId() != null) {
   ```
   or 
   ```suggestion
                   if (Boolean.FALSE.equals(autoSelect)) {
   ```
   in the end it seems we only care about the host id not being null in this 
block. so maybe remove both the checks on `autoSelect` completely from this 
block?



##########
server/src/main/java/com/cloud/vm/UserVmManagerImpl.java:
##########
@@ -6942,8 +6957,9 @@ public VirtualMachine 
migrateVirtualMachineWithVolume(Long vmId, Host destinatio
         Pair<Host, Host> sourceDestinationHosts = 
getHostsForMigrateVmWithStorage(vm, destinationHost);
         Host srcHost = sourceDestinationHosts.first();
 
-        if (!isVMUsingLocalStorage(vm) && MapUtils.isEmpty(volumeToPool)
-                && 
(destinationHost.getClusterId().equals(srcHost.getClusterId()) || 
isVmVolumesOnZoneWideStore(vm))){
+        final List<VolumeVO> volumes = 
_volsDao.findCreatedByInstance(vm.getId());
+        if (!isAnyVmVolumeUsingLocalStorage(volumes) && 
MapUtils.isEmpty(volumeToPool) && destinationHost != null
+                && 
(destinationHost.getClusterId().equals(srcHost.getClusterId()) || 
isAllVmVolumesOnZoneWideStore(volumes))) {

Review Comment:
   this is a complicated condition. maybe you can extract it to a method for 
readability?
   A good name would also clarify the use of the condition and making any 
comments unnecessary. Something like :
   ```
           if (isDestinationClearAndUnambiguousFor(volumes, volumeToPool, 
destinationHost)) {
   ```



##########
api/src/main/java/org/apache/cloudstack/api/command/admin/vm/MigrateVirtualMachineWithVolumeCmd.java:
##########
@@ -146,28 +152,31 @@ public ApiCommandResourceType getApiResourceType() {
 
     @Override
     public void execute() {
-        if (hostId == null && MapUtils.isEmpty(migrateVolumeTo)) {
-            throw new InvalidParameterValueException(String.format("Either %s 
or %s must be passed for migrating the VM.", ApiConstants.HOST_ID, 
ApiConstants.MIGRATE_TO));
+        if (hostId == null && MapUtils.isEmpty(migrateVolumeTo) && 
!Boolean.TRUE.equals(autoSelect)) {
+            throw new InvalidParameterValueException(String.format("Either %s 
or %s must be passed or %s must be true for migrating the VM.", 
ApiConstants.HOST_ID, ApiConstants.MIGRATE_TO, ApiConstants.AUTO_SELECT));
         }
 
         VirtualMachine virtualMachine = 
_userVmService.getVm(getVirtualMachineId());
-        if (!VirtualMachine.State.Running.equals(virtualMachine.getState()) && 
hostId != null) {
+        if (!VirtualMachine.State.Running.equals(virtualMachine.getState()) && 
(hostId != null || Boolean.TRUE.equals(autoSelect))) {
             throw new InvalidParameterValueException(String.format("%s is not 
in the Running state to migrate it to the new host.", virtualMachine));
         }
 
-        if (!VirtualMachine.State.Stopped.equals(virtualMachine.getState()) && 
hostId == null) {
-            throw new InvalidParameterValueException(String.format("%s is not 
in the Stopped state to migrate, use the %s parameter to migrate it to a new 
host.",
-                    virtualMachine, ApiConstants.HOST_ID));
+        if (!VirtualMachine.State.Stopped.equals(virtualMachine.getState()) && 
hostId == null && Boolean.FALSE.equals(autoSelect)) {
+            throw new InvalidParameterValueException(String.format("%s is not 
in the Stopped state to migrate, use the %s or %s parameter to migrate it to a 
new host.",

Review Comment:
   again, not really in scope:
   ```suggestion
               throw new InvalidParameterValueException(String.format("%s is 
not in the Stopped. To migrate it, use the %s or %s parameter to select a new 
host.",
   ```



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