The stopVirtualMachine API should return the vm instance rather than a boolean indicating success. The response for async jobs is now under the jobresult property of the JSON response, handle the start/stop virtual machine responses using data from the jobresult rather than expecting some embedded object.

This commit is contained in:
Kris McQueen 2010-10-18 11:15:07 -07:00
parent c9b5b33dd8
commit 3be625ca6e
4 changed files with 115 additions and 21 deletions

View File

@ -19,15 +19,19 @@ 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.uservm.UserVm;
import com.cloud.vm.InstanceGroupVO;
@Implementation(method="stopVirtualMachine", manager=Manager.UserVmManager, description="Stops a virtual machine.")
public class StopVMCmd extends BaseAsyncCmd {
@ -80,10 +84,93 @@ public class StopVMCmd 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());
//Network groups
response.setNetworkGroupList(ApiDBUtils.getNetworkGroupsNamesForVm(vm.getId()));
response.setResponseName(getName());
return response;
}

View File

@ -173,7 +173,7 @@ public interface UserVmManager extends Manager, VirtualMachineManager<UserVmVO>
* @return true if stopped; false if problems.
*/
boolean stopVirtualMachine(long userId, long vmId, long eventId);
boolean stopVirtualMachine(StopVMCmd cmd) throws ServerApiException;
UserVmVO stopVirtualMachine(StopVMCmd cmd) throws ServerApiException;
OperationResponse executeStopVM(StopVMExecutor executor, VMOperationParam param);
void completeStopCommand(long userId, UserVmVO vm, Event e, long startEventId);

View File

@ -3457,7 +3457,7 @@ public class UserVmManagerImpl implements UserVmManager {
}
@Override
public boolean stopVirtualMachine(StopVMCmd cmd) throws ServerApiException{
public UserVmVO stopVirtualMachine(StopVMCmd cmd) throws ServerApiException{
//Input validation
Account account = (Account)UserContext.current().getAccountObject();
@ -3468,7 +3468,7 @@ public class UserVmManagerImpl implements UserVmManager {
if(account!=null && account.getRemoved() != null)
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed");
UserVmVO vmInstance = _vmDao.findById(id.longValue());
UserVmVO vmInstance = _vmDao.findById(id);
if (vmInstance == null) {
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + id);
}
@ -3477,7 +3477,12 @@ public class UserVmManagerImpl implements UserVmManager {
userId = accountAndUserValidation(id, account, userId, vmInstance);
return stopVirtualMachine(userId, id, eventId);
boolean success = stopVirtualMachine(userId, id, eventId);
if (!success) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Unable to stop virtual machine with id " + id + ", internal error.");
}
return _vmDao.findById(id);
}
@Override

View File

@ -192,6 +192,7 @@ function showInstancesTab(p_domainId, p_account) {
if (result.jobstatus == 0) {
return; //Job has not completed
} else {
var virtualmachine = result.jobresult.startvirtualmachineresponse;
if (vmInstance != null) {
$("body").stopTime(timerKey);
vmInstance.find("#vm_loading_container").hide();
@ -201,11 +202,11 @@ function showInstancesTab(p_domainId, p_account) {
vmInstance.find(".loadingmessage_container").fadeIn("slow");
vmInstance.find("#vm_state_bar").removeClass("admin_vmgrey_arrow admin_vmred_arrow").addClass("admin_vmgreen_arrow");
vmInstance.find("#vm_state").text(result.virtualmachine[0].state).removeClass("grid_celltitles grid_stoppedtitles").addClass("grid_runningtitles");
vmInstance.data("state", result.virtualmachine[0].state);
vmInstance.find("#vm_state").text(virtualmachine.state).removeClass("grid_celltitles grid_stoppedtitles").addClass("grid_runningtitles");
vmInstance.data("state", virtualmachine.state);
if (result.virtualmachine[0].hostname != undefined) {
vmInstance.find("#vm_host").html("<strong>Host:</strong> " + fromdb(result.virtualmachine[0].hostname));
if (virtualmachine.hostname != undefined) {
vmInstance.find("#vm_host").html("<strong>Host:</strong> " + fromdb(virtualmachine.hostname));
} else {
vmInstance.find("#vm_host").html("<strong>Host:</strong> ");
}
@ -215,8 +216,8 @@ function showInstancesTab(p_domainId, p_account) {
// Console Proxy UI
vmInstance.find("#vm_action_view_console").data("imgUrl", "console?cmd=thumbnail&vm=" + result.virtualmachine[0].id + "&w=144&h=110");
vmInstance.find("#vm_action_view_console").data("proxyUrl", "console?cmd=access&vm=" + result.virtualmachine[0].id).data("vmId",result.virtualmachine[0].id).click(function(event) {
vmInstance.find("#vm_action_view_console").data("imgUrl", "console?cmd=thumbnail&vm=" + virtualmachine.id + "&w=144&h=110");
vmInstance.find("#vm_action_view_console").data("proxyUrl", "console?cmd=access&vm=" + virtualmachine.id).data("vmId",virtualmachine.id).click(function(event) {
event.preventDefault();
var viewer = window.open($(this).data("proxyUrl"),$(this).data("vmId"),"width=820,height=640,resizable=yes,menubar=no,status=no,scrollbars=no,toolbar=no,location=no");
viewer.focus();
@ -282,6 +283,7 @@ function showInstancesTab(p_domainId, p_account) {
if (result.jobstatus == 0) {
return; //Job has not completed
} else {
var virtualmachine = result.jobresult.stopvirtualmachineresponse;
if (vmInstance != null) {
$("body").stopTime(timerKey);
vmInstance.find("#vm_loading_container").hide();
@ -291,11 +293,11 @@ function showInstancesTab(p_domainId, p_account) {
vmInstance.find(".loadingmessage_container").fadeIn("slow");
vmInstance.find("#vm_state_bar").removeClass("admin_vmgrey_arrow admin_vmgreen_arrow").addClass("admin_vmred_arrow");
vmInstance.find("#vm_state").text(result.virtualmachine[0].state).removeClass("grid_celltitles grid_runningtitles").addClass("grid_stoppedtitles");
vmInstance.data("state", result.virtualmachine[0].state);
vmInstance.find("#vm_state").text(virtualmachine.state).removeClass("grid_celltitles grid_runningtitles").addClass("grid_stoppedtitles");
vmInstance.data("state", virtualmachine.state);
if (result.virtualmachine[0].hostname != undefined) {
vmInstance.find("#vm_host").html("<strong>Host:</strong> " + fromdb(result.virtualmachine[0].hostname));
if (virtualmachine.hostname != undefined) {
vmInstance.find("#vm_host").html("<strong>Host:</strong> " + fromdb(virtualmachine.hostname));
} else {
vmInstance.find("#vm_host").html("<strong>Host:</strong> ");
}