Fix return types for resetVmPassword and upgradeVirtualMachine commands. Fix response for resetVmPassword to be a vm rather than success/failure. Make sure the password is a transient variable for the resetVmPassword command so that the new password is returned to the user.

This commit is contained in:
Kris McQueen 2010-10-06 18:29:27 -07:00
parent 6db9f377a3
commit fbb5a109cc
3 changed files with 145 additions and 28 deletions

View File

@ -19,15 +19,21 @@ package com.cloud.api.commands;
import org.apache.log4j.Logger;
import com.cloud.api.BaseAsyncCmd;
import com.cloud.api.BaseCmd.Manager;
import com.cloud.api.ApiDBUtils;
import com.cloud.api.BaseAsyncCmd;
import com.cloud.api.BaseCmd;
import com.cloud.api.BaseCmd.Manager;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.response.SuccessResponse;
import com.cloud.api.response.UserVmResponse;
import com.cloud.event.EventTypes;
import com.cloud.offering.ServiceOffering;
import com.cloud.storage.VMTemplateVO;
import com.cloud.user.Account;
import com.cloud.user.User;
import com.cloud.user.UserContext;
import com.cloud.uservm.UserVm;
import com.cloud.vm.InstanceGroupVO;
@Implementation(method="resetVMPassword", manager=Manager.UserVmManager)
public class ResetVMPasswordCmd extends BaseAsyncCmd {
@ -42,6 +48,10 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd {
@Parameter(name="id", type=CommandType.LONG, required=true)
private Long id;
// unexposed parameter needed for serializing/deserializing the command
@Parameter(name="password", type=CommandType.STRING, expose=false)
private String password;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
@ -50,6 +60,14 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd {
return id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@ -80,10 +98,107 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd {
}
@Override @SuppressWarnings("unchecked")
public SuccessResponse getResponse() {
Boolean success = (Boolean)getResponseObject();
SuccessResponse response = new SuccessResponse();
response.setSuccess(success);
public UserVmResponse getResponse() {
UserVm userVm = (UserVm)getResponseObject();
UserVmResponse response = new UserVmResponse();
response.setId(userVm.getId());
response.setName(userVm.getName());
response.setCreated(userVm.getCreated());
response.setZoneId(userVm.getDataCenterId());
response.setZoneName(ApiDBUtils.findZoneById(userVm.getDataCenterId()).getName());
response.setIpAddress(userVm.getPrivateIpAddress());
response.setServiceOfferingId(userVm.getServiceOfferingId());
response.setHaEnable(userVm.isHaEnabled());
InstanceGroupVO group = ApiDBUtils.findInstanceGroupForVM(userVm.getId());
if (group != null) {
response.setGroup(group.getName());
response.setGroupId(group.getId());
}
if (userVm.getDisplayName() == null || userVm.getDisplayName().length() == 0) {
response.setDisplayName(userVm.getName());
} else {
response.setDisplayName(userVm.getDisplayName());
}
if (userVm.getState() != null) {
response.setState(userVm.getState().toString());
}
VMTemplateVO template = ApiDBUtils.findTemplateById(userVm.getTemplateId());
Account acct = ApiDBUtils.findAccountById(Long.valueOf(userVm.getAccountId()));
if (acct != null) {
response.setAccountName(acct.getAccountName());
response.setDomainId(acct.getDomainId());
response.setDomainName(ApiDBUtils.findDomainById(acct.getDomainId()).getName());
}
Long userId = UserContext.current().getUserId();
if (userId == null) {
userId = User.UID_SYSTEM;
}
//this is for the case where the admin deploys a vm for a normal user
User userExecutingCmd = ApiDBUtils.findUserById(userId);
Account acctForUserExecutingCmd = ApiDBUtils.findAccountById(Long.valueOf(userExecutingCmd.getAccountId()));
if ((BaseCmd.isAdmin(acctForUserExecutingCmd.getType()) && (userVm.getHostId() != null)) || (BaseCmd.isAdmin(acct.getType()) && (userVm.getHostId() != null))) {
response.setHostName(ApiDBUtils.findHostById(userVm.getHostId()).getName());
response.setHostId(userVm.getHostId());
}
String templateName = "none";
boolean templatePasswordEnabled = false;
String templateDisplayText = null;
if (template != null) {
templateName = template.getName();
templatePasswordEnabled = template.getEnablePassword();
templateDisplayText = template.getDisplayText();
if (templateDisplayText == null) {
templateDisplayText = templateName;
}
}
if (templatePasswordEnabled) { // FIXME: where will the password come from in this case?
response.setPassword(getPassword());
}
// ISO Info
Long isoId = userVm.getIsoId();
if (isoId != null) {
VMTemplateVO iso = ApiDBUtils.findTemplateById(isoId.longValue());
if (iso != null) {
response.setIsoId(isoId.longValue());
response.setIsoName(iso.getName());
response.setTemplateId(isoId.longValue());
response.setTemplateName(iso.getName());
templateDisplayText = iso.getDisplayText();
if(templateDisplayText == null)
templateDisplayText = iso.getName();
response.setIsoDisplayText(templateDisplayText);
response.setTemplateDisplayText(templateDisplayText);
}
} else {
response.setTemplateId(userVm.getTemplateId());
response.setTemplateName(templateName);
response.setTemplateDisplayText(templateDisplayText);
response.setPasswordEnabled(templatePasswordEnabled);
}
ServiceOffering offering = ApiDBUtils.findServiceOfferingById(userVm.getServiceOfferingId());
response.setServiceOfferingId(userVm.getServiceOfferingId());
response.setServiceOfferingName(offering.getName());
response.setCpuNumber(offering.getCpu());
response.setCpuSpeed(offering.getSpeed());
response.setMemory(offering.getRamSize());
response.setNetworkGroupList(ApiDBUtils.getNetworkGroupsNamesForVm(userVm.getId()));
response.setResponseName(getName());
return response;
}

View File

@ -113,9 +113,9 @@ public interface UserVmManager extends Manager, VirtualMachineManager<UserVmVO>
/**
* Resets the password of a virtual machine.
* @param cmd - the command specifying vmId, password
* @return true if reset worked successfully, false otherwise
* @return the VM if reset worked successfully, null otherwise
*/
boolean resetVMPassword(ResetVMPasswordCmd cmd);
UserVm resetVMPassword(ResetVMPasswordCmd cmd);
/**
* Attaches the specified volume to the specified VM
@ -182,10 +182,10 @@ public interface UserVmManager extends Manager, VirtualMachineManager<UserVmVO>
/**
* upgrade the service offering of the virtual machine
* @param cmd - the command specifying vmId and new serviceOfferingId
* @return success/failure
* @return the vm
* @throws InvalidParameterValueException
*/
boolean upgradeVirtualMachine(UpgradeVMCmd cmd) throws ServerApiException, InvalidParameterValueException;
UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ServerApiException, InvalidParameterValueException;
/**
* Obtains statistics for a list of host or VMs; CPU and network utilization

View File

@ -282,7 +282,7 @@ public class UserVmManagerImpl implements UserVmManager {
}
@Override
public boolean resetVMPassword(ResetVMPasswordCmd cmd){
public UserVm resetVMPassword(ResetVMPasswordCmd cmd){
Long userId = UserContext.current().getUserId();
boolean result = resetVMPasswordInternal(cmd);
@ -298,17 +298,16 @@ public class UserVmManagerImpl implements UserVmManager {
} else {
s_logger.warn("Unable to find vm = " + cmd.getId()+ " to reset password");
}
return result;
return userVm;
}
private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd) {
private boolean resetVMPasswordInternal(ResetVMPasswordCmd cmd) {
//Input validation
Account account = (Account)UserContext.current().getAccountObject();
Long userId = UserContext.current().getUserId();
Long id = cmd.getId();
String password = null;
//Verify input parameters
@ -316,9 +315,9 @@ public class UserVmManagerImpl implements UserVmManager {
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();;
@ -328,8 +327,10 @@ public class UserVmManagerImpl implements UserVmManager {
if (password == null || password.equals("")) {
return false;
} else {
cmd.setPassword(password);
}
if (template.getEnablePassword()) {
if (vmInstance.getDomainRouterId() == null)
/*TODO: add it for external dhcp mode*/
@ -1301,15 +1302,14 @@ public class UserVmManagerImpl implements UserVmManager {
/*
* TODO: cleanup eventually - Refactored API call
*/
public boolean upgradeVirtualMachine(UpgradeVMCmd cmd) throws ServerApiException, InvalidParameterValueException {
public UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ServerApiException, InvalidParameterValueException {
Long virtualMachineId = cmd.getId();
Long serviceOfferingId = cmd.getServiceOfferingId();
Account account = (Account)UserContext.current().getAccountObject();
Long userId = UserContext.current().getUserId();
// Verify input parameters
UserVmVO vmInstance = _vmDao.createForUpdate(virtualMachineId.longValue());
UserVmVO vmInstance = _vmDao.findById(virtualMachineId);
if (vmInstance == null) {
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + virtualMachineId);
}
@ -1368,11 +1368,13 @@ public class UserVmManagerImpl implements UserVmManager {
// FIXME: save this eventId somewhere as part of the async process?
/*long eventId = */EventUtils.saveScheduledEvent(userId, vmInstance.getAccountId(), EventTypes.EVENT_VM_UPGRADE, "upgrading Vm with Id: "+vmInstance.getId());
vmInstance.setServiceOfferingId(serviceOfferingId);
vmInstance.setHaEnabled(_serviceOfferingDao.findById(serviceOfferingId).getOfferHA());
return _vmDao.update(vmInstance.getId(), vmInstance);
UserVmVO vmForUpdate = _vmDao.createForUpdate();
vmForUpdate.setServiceOfferingId(serviceOfferingId);
vmForUpdate.setHaEnabled(_serviceOfferingDao.findById(serviceOfferingId).getOfferHA());
_vmDao.update(vmInstance.getId(), vmForUpdate);
return _vmDao.findById(vmInstance.getId());
}
private Long accountAndUserValidation(Long virtualMachineId,Account account, Long userId, UserVmVO vmInstance) throws ServerApiException {