diff --git a/server/src/com/cloud/api/commands/StartVMCmd.java b/server/src/com/cloud/api/commands/StartVMCmd.java index ddf27f899eb..c7848dc1087 100644 --- a/server/src/com/cloud/api/commands/StartVMCmd.java +++ b/server/src/com/cloud/api/commands/StartVMCmd.java @@ -26,24 +26,19 @@ import org.apache.log4j.Logger; import com.cloud.api.ApiConstants; import com.cloud.api.BaseCmd; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.ServerApiException; +import com.cloud.api.BaseCmd.Manager; import com.cloud.user.Account; import com.cloud.utils.Pair; import com.cloud.vm.UserVmVO; - + +@Implementation(method="startVirtualMachine", manager=Manager.UserVmManager) public class StartVMCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(StartVMCmd.class.getName()); private static final String s_name = "startvirtualmachineresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); - - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -71,50 +66,52 @@ public class StartVMCmd extends BaseCmd { public static String getResultObjectName() { return "virtualmachine"; } - - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map params) { - Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName()); - Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - - //if account is removed, return error - if(account!=null && account.getRemoved() != null) - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); - - //Verify input parameters - UserVmVO vmInstanceCheck = getManagementServer().findUserVMInstanceById(vmId.longValue()); - if (vmInstanceCheck == null) { - throw new ServerApiException (BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId); - } - - if (account != null) { - if (!isAdmin(account.getType()) && (account.getId().longValue() != vmInstanceCheck.getAccountId())) { - throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId + " for this account"); - } else if (!getManagementServer().isChildDomain(account.getDomainId(), vmInstanceCheck.getDomainId())) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to start virtual machine."); - } - } - - if (userId == null) { - userId = Long.valueOf(1); - } - - long jobId = getManagementServer().startVirtualMachineAsync(userId.longValue(), vmId.longValue(), null); - if(jobId == 0) { - s_logger.warn("Unable to schedule async-job for StartVM comamnd"); - } else { - if(s_logger.isDebugEnabled()) - s_logger.debug("StartVM command has been accepted, job id: " + jobId); - } - - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); - - return returnValues; - } +// @Override +// public List> execute(Map params) { +// Long vmId = (Long)params.get(BaseCmd.Properties.ID.getName()); +// Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); +// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); +// +// //if account is removed, return error +// if(account!=null && account.getRemoved() != null) +// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); +// +// //Verify input parameters +// UserVmVO vmInstanceCheck = getManagementServer().findUserVMInstanceById(vmId.longValue()); +// if (vmInstanceCheck == null) { +// throw new ServerApiException (BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId); +// } +// +// if (account != null) { +// if (!isAdmin(account.getType()) && (account.getId().longValue() != vmInstanceCheck.getAccountId())) { +// throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId + " for this account"); +// } else if (!getManagementServer().isChildDomain(account.getDomainId(), vmInstanceCheck.getDomainId())) { +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to start virtual machine."); +// } +// } +// +// if (userId == null) { +// userId = Long.valueOf(1); +// } +// +// long jobId = getManagementServer().startVirtualMachineAsync(userId.longValue(), vmId.longValue(), null); +// if(jobId == 0) { +// s_logger.warn("Unable to schedule async-job for StartVM comamnd"); +// } else { +// if(s_logger.isDebugEnabled()) +// s_logger.debug("StartVM command has been accepted, job id: " + jobId); +// } +// +// List> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); +// +// return returnValues; +// } + + @Override + public String getResponse() { + // TODO Add the response object as per executor + return null; + } } diff --git a/server/src/com/cloud/vm/UserVmManager.java b/server/src/com/cloud/vm/UserVmManager.java index 91cb748f5ef..7bb5c8d2cdd 100644 --- a/server/src/com/cloud/vm/UserVmManager.java +++ b/server/src/com/cloud/vm/UserVmManager.java @@ -22,6 +22,7 @@ import java.util.List; import com.cloud.agent.api.VmStatsEntry; import com.cloud.api.ServerApiException; +import com.cloud.api.commands.StartVMCmd; import com.cloud.api.commands.StopVMCmd; import com.cloud.api.commands.UpdateVMCmd; import com.cloud.api.commands.UpgradeVMCmd; @@ -155,6 +156,7 @@ public interface UserVmManager extends Manager, VirtualMachineManager * @throws ConcurrentOperationException */ UserVmVO startVirtualMachine(long userId, long vmId, String password, String isoPath, long startEventId) throws ExecutionException, StorageUnavailableException, ConcurrentOperationException; + UserVmVO startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, ExecutionException, ConcurrentOperationException; /** * Stops the virtual machine diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 9004e999422..ddccd15f0a3 100644 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -66,6 +66,7 @@ import com.cloud.agent.manager.AgentManager; import com.cloud.alert.AlertManager; import com.cloud.api.BaseCmd; import com.cloud.api.ServerApiException; +import com.cloud.api.commands.StartVMCmd; import com.cloud.api.commands.StopVMCmd; import com.cloud.api.commands.UpdateVMCmd; import com.cloud.api.commands.UpgradeVMCmd; @@ -3145,6 +3146,10 @@ public class UserVmManagerImpl implements UserVmManager { Long userId = UserContext.current().getUserId(); Long id = cmd.getId(); + //if account is removed, return error + if(account!=null && account.getRemoved() != null) + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); + UserVmVO vmInstance = _userVmDao.findById(id.longValue()); if (vmInstance == null) { throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + id); @@ -3156,4 +3161,30 @@ public class UserVmManagerImpl implements UserVmManager { return stopVirtualMachine(userId, id, eventId); } + + @Override + public UserVmVO startVirtualMachine(StartVMCmd cmd) throws StorageUnavailableException, ExecutionException, ConcurrentOperationException { + //Input validation + Account account = (Account)UserContext.current().getAccountObject(); + Long userId = UserContext.current().getUserId(); + Long id = cmd.getId(); + + //if account is removed, return error + if(account!=null && account.getRemoved() != null) + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "The account " + account.getId()+" is removed"); + + UserVmVO vmInstance = _userVmDao.findById(id.longValue()); + if (vmInstance == null) { + throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + id); + } + + long eventId = EventUtils.saveScheduledEvent(userId, vmInstance.getAccountId(), EventTypes.EVENT_VM_START, "Starting Vm with Id: "+id); + + userId = accountAndUserValidation(id, account, userId, vmInstance); + + return startVirtualMachine(userId, id, null, null, eventId); + + } + + }