mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-10-26 08:42:29 +01:00 
			
		
		
		
	Refactoring the startvm cmd
This commit is contained in:
		
							parent
							
								
									c58749b917
								
							
						
					
					
						commit
						6ca1ceb769
					
				| @ -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<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>(); | ||||
| 
 | ||||
|     static { | ||||
|     	s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.TRUE)); | ||||
|         s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); | ||||
|         s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USER_ID, Boolean.FALSE)); | ||||
|          | ||||
|     } | ||||
| 
 | ||||
|     ///////////////////////////////////////////////////// | ||||
|     //////////////// API parameters ///////////////////// | ||||
| @ -72,49 +67,51 @@ public class StartVMCmd extends BaseCmd { | ||||
|     	return "virtualmachine"; | ||||
|     } | ||||
| 
 | ||||
|     public List<Pair<Enum, Boolean>> getProperties() { | ||||
|         return s_properties; | ||||
|     } | ||||
| //    @Override | ||||
| //    public List<Pair<String, Object>> execute(Map<String, Object> 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<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>(); | ||||
| //        returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));  | ||||
| //         | ||||
| //        return returnValues; | ||||
| //    } | ||||
| 
 | ||||
| 	@Override | ||||
|     public List<Pair<String, Object>> execute(Map<String, Object> 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<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>(); | ||||
|         returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));  | ||||
|          | ||||
|         return returnValues; | ||||
| 	public String getResponse() { | ||||
| 		// TODO Add the response object as per executor | ||||
| 		return null; | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -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<UserVmVO> | ||||
|      * @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 | ||||
|  | ||||
| @ -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); | ||||
| 		 | ||||
| 	} | ||||
| 	 | ||||
| 	 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user