bug 6678: return the VM as part of the rebootVirtualMachine implementation, serialize the VM for the API response. In 2.1.x the VM was serialized, but during refactoring for the 2.2 API framework that was changed to a SuccessResponse, but now backwards compatibility is preserved.

status 6678: resolved fixed
This commit is contained in:
Kris McQueen 2010-10-22 16:24:54 -07:00
parent 109bdb18fe
commit 57a299fb46
3 changed files with 109 additions and 14 deletions

View File

@ -21,13 +21,19 @@ import org.apache.log4j.Logger;
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.StoragePoolVO;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.VolumeVO;
import com.cloud.user.Account;
import com.cloud.uservm.UserVm;
import com.cloud.vm.InstanceGroupVO;
@Implementation(method="rebootVirtualMachine", manager=Manager.UserVmManager, description="Reboots a virtual machine.")
public class RebootVMCmd extends BaseAsyncCmd {
@ -79,10 +85,102 @@ public class RebootVMCmd extends BaseAsyncCmd {
}
@Override @SuppressWarnings("unchecked")
public SuccessResponse getResponse() {
Boolean success = (Boolean)getResponseObject();
SuccessResponse response = new SuccessResponse();
response.setSuccess(success);
public UserVmResponse getResponse() {
UserVm vm = (UserVm)getResponseObject();
UserVmResponse response = new UserVmResponse();
response.setId(vm.getId());
response.setName(vm.getName());
response.setCreated(vm.getCreated());
response.setZoneId(vm.getDataCenterId());
response.setZoneName(ApiDBUtils.findZoneById(vm.getDataCenterId()).getName());
response.setIpAddress(vm.getPrivateIpAddress());
response.setServiceOfferingId(vm.getServiceOfferingId());
response.setHaEnable(vm.isHaEnabled());
if (vm.getDisplayName() == null || vm.getDisplayName().length() == 0) {
response.setDisplayName(vm.getName());
} else {
response.setDisplayName(vm.getDisplayName());
}
InstanceGroupVO group = ApiDBUtils.findInstanceGroupForVM(vm.getId());
if (group != null) {
response.setGroup(group.getName());
response.setGroupId(group.getId());
}
if (vm.getState() != null) {
response.setState(vm.getState().toString());
}
Account acct = ApiDBUtils.findAccountById(vm.getAccountId());
if (acct != null) {
response.setAccountName(acct.getAccountName());
response.setDomainId(acct.getDomainId());
response.setDomainName(ApiDBUtils.findDomainById(acct.getDomainId()).getName());
}
if (BaseCmd.isAdmin(acct.getType()) && (vm.getHostId() != null)) {
response.setHostName(ApiDBUtils.findHostById(vm.getHostId()).getName());
response.setHostId(vm.getHostId());
}
String templateName = "ISO Boot";
boolean templatePasswordEnabled = false;
String templateDisplayText = "ISO Boot";
VMTemplateVO template = ApiDBUtils.findTemplateById(vm.getTemplateId());
if (template != null) {
templateName = template.getName();
templatePasswordEnabled = template.getEnablePassword();
templateDisplayText = template.getDisplayText();
if (templateDisplayText == null) {
templateDisplayText = templateName;
}
}
response.setTemplateId(vm.getTemplateId());
response.setTemplateName(templateName);
response.setTemplateDisplayText(templateDisplayText);
response.setPasswordEnabled(templatePasswordEnabled);
if (templatePasswordEnabled) {
response.setPassword(null); // FIXME: Where should password come from? In the old framework, password was always passed
// in to composeResultObject() as null, so that behavior is preserved...
} else {
response.setPassword("");
}
String isoName = null;
if (vm.getIsoId() != null) {
VMTemplateVO iso = ApiDBUtils.findTemplateById(vm.getIsoId().longValue());
if (iso != null) {
isoName = iso.getName();
}
}
response.setIsoId(vm.getIsoId());
response.setIsoName(isoName);
ServiceOffering offering = ApiDBUtils.findServiceOfferingById(vm.getServiceOfferingId());
response.setServiceOfferingId(vm.getServiceOfferingId());
response.setServiceOfferingName(offering.getName());
response.setCpuNumber(offering.getCpu());
response.setCpuSpeed(offering.getSpeed());
response.setMemory(offering.getRamSize());
VolumeVO rootVolume = ApiDBUtils.findRootVolume(vm.getId());
if (rootVolume != null) {
response.setRootDeviceId(rootVolume.getDeviceId());
StoragePoolVO storagePool = ApiDBUtils.findStoragePoolById(rootVolume.getPoolId());
response.setRootDeviceType(storagePool.getPoolType().toString());
}
response.setGuestOsId(vm.getGuestOSId());
//Network groups
response.setNetworkGroupList(ApiDBUtils.getNetworkGroupsNamesForVm(vm.getId()));
response.setResponseName(getName());
return response;
}

View File

@ -3512,7 +3512,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService {
}
@Override
public boolean rebootVirtualMachine(RebootVMCmd cmd) {
public UserVm rebootVirtualMachine(RebootVMCmd cmd) {
Account account = UserContext.current().getAccount();
Long userId = UserContext.current().getUserId();
Long vmId = cmd.getId();
@ -3529,15 +3529,12 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService {
boolean status = rebootVirtualMachine(userId, vmId);
if(status)
{
if (status) {
EventUtils.saveEvent(userId, vmInstance.getAccountId(), EventTypes.EVENT_VM_REBOOT, "Successfully rebooted vm with id:"+vmId);
return status;
}
else
{
return _vmDao.findById(vmId);
} else {
EventUtils.saveEvent(userId, vmInstance.getAccountId(), EventTypes.EVENT_VM_REBOOT, "Failed to reboot vm with id:"+vmId);
return status;
throw new CloudRuntimeException("Failed to reboot vm with id: " + vmId);
}
}

View File

@ -81,7 +81,7 @@ public interface UserVmService extends Manager {
UserVmVO startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, ExecutionException, ConcurrentOperationException;
UserVmVO stopVirtualMachine(StopVMCmd cmd) throws ServerApiException;
boolean rebootVirtualMachine(RebootVMCmd cmd);
UserVm rebootVirtualMachine(RebootVMCmd cmd);
@Deprecated
OperationResponse executeRebootVM(RebootVMExecutor executor, VMOperationParam param);