mirror of https://github.com/apache/cloudstack.git
bug 6914: return password in deployVirtualMachine/resetVmPassword response.
status 6914: resolved fixed
This commit is contained in:
parent
5d77e41cc8
commit
3954c7550a
|
|
@ -416,6 +416,10 @@ public class ApiResponseHelper {
|
|||
userVmResponse.setTemplateDisplayText("ISO Boot");
|
||||
userVmResponse.setPasswordEnabled(false);
|
||||
}
|
||||
|
||||
if (userVm.getPassword() != null) {
|
||||
userVmResponse.setPassword(userVm.getPassword());
|
||||
}
|
||||
|
||||
// ISO Info
|
||||
if (userVm.getIsoId() != null) {
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ import com.cloud.exception.InvalidParameterValueException;
|
|||
import com.cloud.exception.PermissionDeniedException;
|
||||
import com.cloud.exception.ResourceAllocationException;
|
||||
import com.cloud.exception.StorageUnavailableException;
|
||||
import com.cloud.storage.VMTemplateVO;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.user.UserContext;
|
||||
import com.cloud.uservm.UserVm;
|
||||
|
|
@ -200,13 +201,17 @@ public class DeployVMCmd extends BaseAsyncCmd {
|
|||
@Override
|
||||
public void execute() throws ServerApiException, InvalidParameterValueException, PermissionDeniedException, InsufficientAddressCapacityException, InsufficientCapacityException, ConcurrentOperationException, StorageUnavailableException{
|
||||
try {
|
||||
UserVm result = _mgr.deployVirtualMachine(this);
|
||||
UserVmResponse response = ApiResponseHelper.createUserVmResponse(result);
|
||||
|
||||
// FIXME: where will the password come from in this case?
|
||||
// if (templatePasswordEnabled) {
|
||||
// response.setPassword(getPassword());
|
||||
// }
|
||||
String password = null;
|
||||
if (templateId != null ) {
|
||||
VMTemplateVO template = ApiDBUtils.findTemplateById(templateId);
|
||||
if (template.getEnablePassword()) {
|
||||
password = _mgr.generateRandomPassword();
|
||||
}
|
||||
}
|
||||
UserVm result = _mgr.deployVirtualMachine(this, password);
|
||||
UserVmResponse response = ApiResponseHelper.createUserVmResponse(result);
|
||||
response.setPassword(password);
|
||||
response.setResponseName(getName());
|
||||
this.setResponseObject(response);
|
||||
} catch (ResourceAllocationException ex) {
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ import com.cloud.exception.PermissionDeniedException;
|
|||
import com.cloud.exception.StorageUnavailableException;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.uservm.UserVm;
|
||||
import com.cloud.utils.PasswordGenerator;
|
||||
|
||||
@Implementation(description="Resets the password for virtual machine. " +
|
||||
"The virtual machine must be in a \"Stopped\" state and the template must already " +
|
||||
|
|
@ -103,13 +104,12 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd {
|
|||
|
||||
@Override
|
||||
public void execute() throws ServerApiException, InvalidParameterValueException, PermissionDeniedException, InsufficientAddressCapacityException, InsufficientCapacityException, ConcurrentOperationException, StorageUnavailableException{
|
||||
UserVm result = _userVmService.resetVMPassword(this);
|
||||
|
||||
password = _mgr.generateRandomPassword();
|
||||
UserVm result = _userVmService.resetVMPassword(this, password);
|
||||
|
||||
UserVmResponse response = ApiResponseHelper.createUserVmResponse(result);
|
||||
|
||||
// FIXME: where will the password come from in this case?
|
||||
// if (templatePasswordEnabled) {
|
||||
// response.setPassword(getPassword());
|
||||
// }
|
||||
response.setResponseName(getName());
|
||||
this.setResponseObject(response);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -301,7 +301,7 @@ public interface ManagementServer {
|
|||
* @throws StorageUnavailableException
|
||||
* @throws ConcurrentOperationException
|
||||
*/
|
||||
UserVm deployVirtualMachine(DeployVMCmd cmd) throws ResourceAllocationException, InsufficientStorageCapacityException, ExecutionException, StorageUnavailableException, ConcurrentOperationException;
|
||||
UserVm deployVirtualMachine(DeployVMCmd cmd, String password) throws ResourceAllocationException, InsufficientStorageCapacityException, ExecutionException, StorageUnavailableException, ConcurrentOperationException;
|
||||
|
||||
/**
|
||||
* Finds a domain router by user and data center
|
||||
|
|
|
|||
|
|
@ -1101,7 +1101,7 @@ public class ManagementServerImpl implements ManagementServer {
|
|||
}
|
||||
|
||||
@Override
|
||||
public UserVm deployVirtualMachine(DeployVMCmd cmd) throws ResourceAllocationException,
|
||||
public UserVm deployVirtualMachine(DeployVMCmd cmd, String password) throws ResourceAllocationException,
|
||||
InsufficientStorageCapacityException, ExecutionException,
|
||||
StorageUnavailableException, ConcurrentOperationException {
|
||||
Account ctxAccount = UserContext.current().getAccount();
|
||||
|
|
@ -1114,7 +1114,6 @@ public class ManagementServerImpl implements ManagementServer {
|
|||
long templateId = cmd.getTemplateId();
|
||||
Long diskOfferingId = cmd.getDiskOfferingId();
|
||||
String domain = null; // FIXME: this was hardcoded to null in DeployVMCmd in the old framework, do we need it?
|
||||
String password = generateRandomPassword();
|
||||
String displayName = cmd.getDisplayName();
|
||||
String group = cmd.getGroup();
|
||||
String userData = cmd.getUserData();
|
||||
|
|
|
|||
|
|
@ -284,62 +284,53 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, VirtualM
|
|||
}
|
||||
|
||||
@Override
|
||||
public UserVm resetVMPassword(ResetVMPasswordCmd cmd){
|
||||
|
||||
public UserVm resetVMPassword(ResetVMPasswordCmd cmd, String password){
|
||||
Account account = UserContext.current().getAccount();
|
||||
Long userId = UserContext.current().getUserId();
|
||||
boolean result = resetVMPasswordInternal(cmd);
|
||||
|
||||
// Log event
|
||||
UserVmVO userVm = _vmDao.findById(cmd.getId());
|
||||
if (userVm != null) {
|
||||
if (result) {
|
||||
EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_VM_RESETPASSWORD, "successfully reset password for VM : " + userVm.getHostName(), null);
|
||||
} else {
|
||||
EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_RESETPASSWORD, "unable to reset password for VM : " + userVm.getHostName(), null);
|
||||
}
|
||||
Long vmId = cmd.getId();
|
||||
UserVmVO userVm = _vmDao.findById(cmd.getId());
|
||||
|
||||
//Do parameters input validation
|
||||
if (userVm == null) {
|
||||
throw new InvalidParameterValueException("unable to find a virtual machine with id " + cmd.getId());
|
||||
}
|
||||
|
||||
VMTemplateVO template = _templateDao.findById(userVm.getTemplateId());
|
||||
if (template == null || !template.getEnablePassword()) {
|
||||
throw new InvalidParameterValueException("Fail to reset password for the virtual machine, the template is not password enabled");
|
||||
}
|
||||
|
||||
userId = accountAndUserValidation(vmId, account, userId, userVm);
|
||||
|
||||
boolean result = resetVMPasswordInternal(cmd, password);
|
||||
|
||||
if (result) {
|
||||
userVm.setPassword(password);
|
||||
EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_VM_RESETPASSWORD, "successfully reset password for VM : " + userVm.getHostName(), null);
|
||||
} else {
|
||||
s_logger.warn("Unable to find vm = " + cmd.getId()+ " to reset password");
|
||||
EventUtils.saveEvent(userId, userVm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_RESETPASSWORD, "unable to reset password for VM : " + userVm.getHostName(), null);
|
||||
}
|
||||
|
||||
|
||||
return userVm;
|
||||
}
|
||||
|
||||
private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd) {
|
||||
//Input validation
|
||||
Account account = UserContext.current().getAccount();
|
||||
Long userId = UserContext.current().getUserId();
|
||||
Long id = cmd.getId();
|
||||
|
||||
String password = null;
|
||||
|
||||
//Verify input parameters
|
||||
UserVmVO vmInstance = _vmDao.findById(id.longValue());
|
||||
if (vmInstance == null) {
|
||||
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + id);
|
||||
}
|
||||
|
||||
userId = accountAndUserValidation(id, account, userId, vmInstance);
|
||||
|
||||
VMTemplateVO template = _templateDao.findById(vmInstance.getTemplateId());
|
||||
if (template.getEnablePassword()) {
|
||||
password = PasswordGenerator.generateRandomPassword(6);;
|
||||
} else {
|
||||
password = "saved_password";
|
||||
}
|
||||
private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd, String password) {
|
||||
Long vmId = cmd.getId();
|
||||
Long userId = UserContext.current().getUserId();
|
||||
UserVmVO vmInstance = _vmDao.findById(vmId);
|
||||
|
||||
if (password == null || password.equals("")) {
|
||||
return false;
|
||||
} else {
|
||||
cmd.setPassword(password);
|
||||
}
|
||||
|
||||
VMTemplateVO template = _templateDao.findById(vmInstance.getTemplateId());
|
||||
if (template.getEnablePassword()) {
|
||||
if (vmInstance.getDomainRouterId() == null)
|
||||
/*TODO: add it for external dhcp mode*/
|
||||
return true;
|
||||
if (_networkMgr.savePasswordToRouter(vmInstance.getDomainRouterId(), vmInstance.getPrivateIpAddress(), password)) {
|
||||
// Need to reboot the virtual machine so that the password gets redownloaded from the DomR, and reset on the VM
|
||||
if (!rebootVirtualMachine(userId, id)) {
|
||||
if (!rebootVirtualMachine(userId, vmId)) {
|
||||
if (vmInstance.getState() == State.Stopped) {
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ public interface UserVmService {
|
|||
* @param cmd - the command specifying vmId, password
|
||||
* @return the VM if reset worked successfully, null otherwise
|
||||
*/
|
||||
UserVm resetVMPassword(ResetVMPasswordCmd cmd);
|
||||
UserVm resetVMPassword(ResetVMPasswordCmd cmd, String password);
|
||||
|
||||
/**
|
||||
* Attaches the specified volume to the specified VM
|
||||
|
|
|
|||
Loading…
Reference in New Issue