From 7a73fcd83ca80ddbb783ea978cf8acbbc6d04e82 Mon Sep 17 00:00:00 2001 From: prachi Date: Mon, 5 Mar 2012 17:46:22 -0800 Subject: [PATCH] Bug 14028 - Add hostid parameter to StartVmCmd to enable admins to start VM on a particular host Changes: - Added hostid to StartVMCmd. Only root admin can use it. --- .../com/cloud/api/commands/StartVMCmd.java | 8 +++++++ api/src/com/cloud/vm/UserVmService.java | 2 +- .../src/com/cloud/vm/UserVmManagerImpl.java | 24 ++++++++++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/api/src/com/cloud/api/commands/StartVMCmd.java b/api/src/com/cloud/api/commands/StartVMCmd.java index 141ed3d30ab..2e0d1fbb98e 100644 --- a/api/src/com/cloud/api/commands/StartVMCmd.java +++ b/api/src/com/cloud/api/commands/StartVMCmd.java @@ -53,6 +53,10 @@ public class StartVMCmd extends BaseAsyncCmd { @IdentityMapper(entityTableName = "vm_instance") @Parameter(name = ApiConstants.ID, type = CommandType.LONG, required = true, description = "The ID of the virtual machine") private Long id; + + @IdentityMapper(entityTableName="host") + @Parameter(name=ApiConstants.HOST_ID, type=CommandType.LONG, description="destination Host ID to deploy the VM to - parameter available for root admin only") + private Long hostId; // /////////////////////////////////////////////////// // ///////////////// Accessors /////////////////////// @@ -61,6 +65,10 @@ public class StartVMCmd extends BaseAsyncCmd { public Long getId() { return id; } + + public Long getHostId() { + return hostId; + } // /////////////////////////////////////////////////// // ///////////// API Implementation/////////////////// diff --git a/api/src/com/cloud/vm/UserVmService.java b/api/src/com/cloud/vm/UserVmService.java index d4cc05eb479..e27c5538720 100755 --- a/api/src/com/cloud/vm/UserVmService.java +++ b/api/src/com/cloud/vm/UserVmService.java @@ -369,7 +369,7 @@ public interface UserVmService { UserVm stopVirtualMachine(long vmId, boolean forced) throws ConcurrentOperationException; - UserVm startVirtualMachine(long vmId) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException; + UserVm startVirtualMachine(long vmId, Long hostId) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException; void deletePrivateTemplateRecord(Long templateId); diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 41129272a37..439792e1772 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -1818,7 +1818,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager @Override @ActionEvent(eventType = EventTypes.EVENT_VM_START, eventDescription = "starting Vm", async = true) public UserVm startVirtualMachine(StartVMCmd cmd) throws ExecutionException, ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { - return startVirtualMachine(cmd.getId()); + return startVirtualMachine(cmd.getId(), cmd.getHostId()); } @Override @@ -2807,7 +2807,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } @Override - public UserVm startVirtualMachine(long vmId) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { + public UserVm startVirtualMachine(long vmId, Long hostId) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { // Input validation Account caller = UserContext.current().getCaller(); Long userId = UserContext.current().getCallerUserId(); @@ -2834,6 +2834,18 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager throw new PermissionDeniedException("The owner of " + vm + " is disabled: " + vm.getAccountId()); } + Host destinationHost = null; + if(hostId != null){ + Account account = UserContext.current().getCaller(); + if(!_accountService.isRootAdmin(account.getType())){ + throw new PermissionDeniedException("Parameter hostid can only be specified by a Root Admin, permission denied"); + } + destinationHost = _hostDao.findById(hostId); + if (destinationHost == null) { + throw new InvalidParameterValueException("Unable to find the host to deploy the VM, host id=" + hostId); + } + } + UserVO user = _userDao.findById(userId); //check if vm is security group enabled @@ -2850,8 +2862,14 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager _securityGroupMgr.addInstanceToGroups(vmId, groupList); } } + + DataCenterDeployment plan = null; + if (destinationHost != null) { + s_logger.debug("Destination Host to deploy the VM is specified, specifying a deployment plan to deploy the VM"); + plan = new DataCenterDeployment(vm.getDataCenterIdToDeployIn(), destinationHost.getPodId(), destinationHost.getClusterId(), destinationHost.getId(), null, null); + } - return _itMgr.start(vm, null, user, caller); + return _itMgr.start(vm, null, user, caller, plan); } @Override