From 3be625ca6eb9ec3369a0f4a328b4325a182ac7e8 Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Mon, 18 Oct 2010 11:15:07 -0700 Subject: [PATCH] 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. --- .../src/com/cloud/api/commands/StopVMCmd.java | 101 ++++++++++++++++-- server/src/com/cloud/vm/UserVmManager.java | 2 +- .../src/com/cloud/vm/UserVmManagerImpl.java | 11 +- ui/scripts/cloud.core.instances.js | 22 ++-- 4 files changed, 115 insertions(+), 21 deletions(-) diff --git a/server/src/com/cloud/api/commands/StopVMCmd.java b/server/src/com/cloud/api/commands/StopVMCmd.java index b2d9617899f..9a35dbd4db3 100644 --- a/server/src/com/cloud/api/commands/StopVMCmd.java +++ b/server/src/com/cloud/api/commands/StopVMCmd.java @@ -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; } diff --git a/server/src/com/cloud/vm/UserVmManager.java b/server/src/com/cloud/vm/UserVmManager.java index 98f3142c972..f123f552c48 100644 --- a/server/src/com/cloud/vm/UserVmManager.java +++ b/server/src/com/cloud/vm/UserVmManager.java @@ -173,7 +173,7 @@ public interface UserVmManager extends Manager, VirtualMachineManager * @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); diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 6c4487e64fd..6ae0c4e18e8 100644 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -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 diff --git a/ui/scripts/cloud.core.instances.js b/ui/scripts/cloud.core.instances.js index 00aab6701b3..05ca191efa4 100644 --- a/ui/scripts/cloud.core.instances.js +++ b/ui/scripts/cloud.core.instances.js @@ -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("Host: " + fromdb(result.virtualmachine[0].hostname)); + if (virtualmachine.hostname != undefined) { + vmInstance.find("#vm_host").html("Host: " + fromdb(virtualmachine.hostname)); } else { vmInstance.find("#vm_host").html("Host: "); } @@ -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("Host: " + fromdb(result.virtualmachine[0].hostname)); + if (virtualmachine.hostname != undefined) { + vmInstance.find("#vm_host").html("Host: " + fromdb(virtualmachine.hostname)); } else { vmInstance.find("#vm_host").html("Host: "); }