Validate VM state before the reboot operation (#156)

* Prevent NPE on reboot stopped VM and start VM output with null display name
* Upstream PR: apache/cloudstack#6397
This commit is contained in:
Suresh Kumar Anaparti 2022-05-26 20:54:33 +05:30 committed by GitHub
parent de1414138b
commit dcf68b272e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 3 deletions

View File

@ -26,6 +26,8 @@ import javax.persistence.FetchType;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import org.apache.commons.lang3.StringUtils;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.uservm.UserVm;
@ -141,4 +143,8 @@ public class UserVmVO extends VMInstanceVO implements UserVm {
public String getName() {
return instanceName;
}
public String getDisplayNameOrHostName() {
return StringUtils.isNotBlank(displayName) ? displayName : getHostName();
}
}

View File

@ -127,7 +127,7 @@ public class UserVmJoinDaoImpl extends GenericDaoBaseWithTagInformation<UserVmJo
userVmResponse.setName(userVm.getName());
if (userVm.getDisplayName() != null) {
userVmResponse.setDisplayName(userVm.getDisplayName());
userVmResponse.setDisplayName(userVm.getDisplayName());
} else {
userVmResponse.setDisplayName(userVm.getName());
}

View File

@ -3132,6 +3132,11 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId);
}
if (vmInstance.getState() != State.Running) {
throw new InvalidParameterValueException(String.format("The VM %s (%s) is not running, unable to reboot it",
vmInstance.getUuid(), vmInstance.getDisplayNameOrHostName()));
}
_accountMgr.checkAccess(caller, null, true, vmInstance);
checkIfHostOfVMIsInPrepareForMaintenanceState(vmInstance.getHostId(), vmId, "Reboot");
@ -5121,8 +5126,9 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
}
if (vm.getState()== State.Running) {
throw new InvalidParameterValueException("The virtual machine "+ vm.getUuid()+ " ("+ vm.getDisplayName()+ ") is already running");
if (vm.getState() == State.Running) {
throw new InvalidParameterValueException(String.format("The virtual machine %s (%s) is already running",
vm.getUuid(), vm.getDisplayNameOrHostName()));
}
_accountMgr.checkAccess(callerAccount, null, true, vm);