mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-11-04 00:02:37 +01:00 
			
		
		
		
	Refactoring the removePortForwardingService cmd
This commit is contained in:
		
							parent
							
								
									25d5126f68
								
							
						
					
					
						commit
						ef2b80f9c6
					
				@ -18,34 +18,18 @@
 | 
			
		||||
 | 
			
		||||
package com.cloud.api.commands;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import org.apache.log4j.Logger;
 | 
			
		||||
 | 
			
		||||
import com.cloud.api.BaseCmd;
 | 
			
		||||
import com.cloud.api.BaseAsyncCmd;
 | 
			
		||||
import com.cloud.api.Implementation;
 | 
			
		||||
import com.cloud.api.Parameter;
 | 
			
		||||
import com.cloud.api.ServerApiException;
 | 
			
		||||
import com.cloud.network.SecurityGroupVO;
 | 
			
		||||
import com.cloud.user.Account;
 | 
			
		||||
import com.cloud.utils.Pair;
 | 
			
		||||
import com.cloud.vm.UserVmVO;
 | 
			
		||||
import com.cloud.api.BaseCmd.Manager;
 | 
			
		||||
 | 
			
		||||
public class RemovePortForwardingServiceCmd extends BaseCmd {
 | 
			
		||||
@Implementation(method="removeSecurityGroup", manager=Manager.ManagementServer)
 | 
			
		||||
public class RemovePortForwardingServiceCmd extends BaseAsyncCmd {
 | 
			
		||||
	public static final Logger s_logger = Logger.getLogger(RemovePortForwardingServiceCmd.class.getName());
 | 
			
		||||
 | 
			
		||||
    private static final String s_name = "removeportforwardingserviceresponse";
 | 
			
		||||
    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.PUBLIC_IP, Boolean.TRUE));
 | 
			
		||||
        s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.VIRTUAL_MACHINE_ID, Boolean.TRUE));
 | 
			
		||||
 | 
			
		||||
        s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USER_ID, Boolean.FALSE));
 | 
			
		||||
        s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
    /////////////////////////////////////////////////////
 | 
			
		||||
    //////////////// API parameters /////////////////////
 | 
			
		||||
@ -84,72 +68,74 @@ public class RemovePortForwardingServiceCmd extends BaseCmd {
 | 
			
		||||
        return s_name;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public List<Pair<Enum, Boolean>> getProperties() {
 | 
			
		||||
        return s_properties;
 | 
			
		||||
    }
 | 
			
		||||
//    @Override
 | 
			
		||||
//    public List<Pair<String, Object>> execute(Map<String, Object> params) {
 | 
			
		||||
//    	Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
 | 
			
		||||
//        Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
 | 
			
		||||
//        Long securityGroupId = (Long)params.get(BaseCmd.Properties.ID.getName());
 | 
			
		||||
//        String publicIp = (String)params.get(BaseCmd.Properties.PUBLIC_IP.getName());
 | 
			
		||||
//        Long vmId = (Long)params.get(BaseCmd.Properties.VIRTUAL_MACHINE_ID.getName());
 | 
			
		||||
//        
 | 
			
		||||
//        //verify input parameters
 | 
			
		||||
//        SecurityGroupVO securityG = getManagementServer().findSecurityGroupById(securityGroupId);
 | 
			
		||||
//        if (securityG == null) {
 | 
			
		||||
//        	throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find a port forwarding service with id " + securityGroupId);
 | 
			
		||||
//        } else if (account != null) {
 | 
			
		||||
//            if (!isAdmin(account.getType()) && (account.getId().longValue() != securityG.getAccountId())) {
 | 
			
		||||
//                throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find a port forwarding service with id " + securityGroupId + " for this account");
 | 
			
		||||
//            } else if (!getManagementServer().isChildDomain(account.getDomainId(), securityG.getDomainId())) {
 | 
			
		||||
//                throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid port forwarding service id (" + securityGroupId + ") given, unable to remove port forwarding service.");
 | 
			
		||||
//            }
 | 
			
		||||
//        }
 | 
			
		||||
//        
 | 
			
		||||
//        UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue());
 | 
			
		||||
//        if (vmInstance == 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() != vmInstance.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(), vmInstance.getDomainId())) {
 | 
			
		||||
//                throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to remove port forwarding service.");
 | 
			
		||||
//            }
 | 
			
		||||
//        }
 | 
			
		||||
//
 | 
			
		||||
//        Account ipAddrAccount = getManagementServer().findAccountByIpAddress(publicIp);
 | 
			
		||||
//        if (ipAddrAccount == null) {
 | 
			
		||||
//            if (account == null) {
 | 
			
		||||
//                throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find ip address " + publicIp);
 | 
			
		||||
//            } else {
 | 
			
		||||
//                throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + publicIp);
 | 
			
		||||
//            }
 | 
			
		||||
//        }
 | 
			
		||||
//
 | 
			
		||||
//        Long accountId = ipAddrAccount.getId();
 | 
			
		||||
//        if ((account != null) && !isAdmin(account.getType())) {
 | 
			
		||||
//            if (account.getId().longValue() != accountId) {
 | 
			
		||||
//                throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + publicIp);
 | 
			
		||||
//            }
 | 
			
		||||
//        }
 | 
			
		||||
//
 | 
			
		||||
//        if (userId == null) {
 | 
			
		||||
//            userId = Long.valueOf(1);
 | 
			
		||||
//        }
 | 
			
		||||
//        
 | 
			
		||||
//        long jobId = getManagementServer().removeSecurityGroupAsync(userId, securityGroupId, publicIp, vmId);
 | 
			
		||||
//        if(jobId == 0) {
 | 
			
		||||
//        	s_logger.warn("Unable to schedule async-job for RemovePortForwardingServiceCmd comamnd");
 | 
			
		||||
//        } else {
 | 
			
		||||
//	        if(s_logger.isDebugEnabled())
 | 
			
		||||
//	        	s_logger.debug("RemovePortForwardingServiceCmd 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) {
 | 
			
		||||
    	Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
 | 
			
		||||
        Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
 | 
			
		||||
        Long securityGroupId = (Long)params.get(BaseCmd.Properties.ID.getName());
 | 
			
		||||
        String publicIp = (String)params.get(BaseCmd.Properties.PUBLIC_IP.getName());
 | 
			
		||||
        Long vmId = (Long)params.get(BaseCmd.Properties.VIRTUAL_MACHINE_ID.getName());
 | 
			
		||||
        
 | 
			
		||||
        //verify input parameters
 | 
			
		||||
        SecurityGroupVO securityG = getManagementServer().findSecurityGroupById(securityGroupId);
 | 
			
		||||
        if (securityG == null) {
 | 
			
		||||
        	throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find a port forwarding service with id " + securityGroupId);
 | 
			
		||||
        } else if (account != null) {
 | 
			
		||||
            if (!isAdmin(account.getType()) && (account.getId().longValue() != securityG.getAccountId())) {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find a port forwarding service with id " + securityGroupId + " for this account");
 | 
			
		||||
            } else if (!getManagementServer().isChildDomain(account.getDomainId(), securityG.getDomainId())) {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid port forwarding service id (" + securityGroupId + ") given, unable to remove port forwarding service.");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue());
 | 
			
		||||
        if (vmInstance == 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() != vmInstance.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(), vmInstance.getDomainId())) {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to remove port forwarding service.");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Account ipAddrAccount = getManagementServer().findAccountByIpAddress(publicIp);
 | 
			
		||||
        if (ipAddrAccount == null) {
 | 
			
		||||
            if (account == null) {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find ip address " + publicIp);
 | 
			
		||||
            } else {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + publicIp);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Long accountId = ipAddrAccount.getId();
 | 
			
		||||
        if ((account != null) && !isAdmin(account.getType())) {
 | 
			
		||||
            if (account.getId().longValue() != accountId) {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + publicIp);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (userId == null) {
 | 
			
		||||
            userId = Long.valueOf(1);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        long jobId = getManagementServer().removeSecurityGroupAsync(userId, securityGroupId, publicIp, vmId);
 | 
			
		||||
        if(jobId == 0) {
 | 
			
		||||
        	s_logger.warn("Unable to schedule async-job for RemovePortForwardingServiceCmd comamnd");
 | 
			
		||||
        } else {
 | 
			
		||||
	        if(s_logger.isDebugEnabled())
 | 
			
		||||
	        	s_logger.debug("RemovePortForwardingServiceCmd 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 String getResponse() {
 | 
			
		||||
		// TODO Look at executor
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -29,6 +29,7 @@ import com.cloud.api.commands.CreatePortForwardingServiceRuleCmd;
 | 
			
		||||
import com.cloud.api.commands.EnableAccountCmd;
 | 
			
		||||
import com.cloud.api.commands.EnableUserCmd;
 | 
			
		||||
import com.cloud.api.commands.GetCloudIdentifierCmd;
 | 
			
		||||
import com.cloud.api.commands.RemovePortForwardingServiceCmd;
 | 
			
		||||
import com.cloud.api.commands.StartSystemVMCmd;
 | 
			
		||||
import com.cloud.api.commands.StopSystemVmCmd;
 | 
			
		||||
import com.cloud.api.commands.UpdateAccountCmd;
 | 
			
		||||
@ -1753,6 +1754,7 @@ public interface ManagementServer {
 | 
			
		||||
     * @param vmId
 | 
			
		||||
     */
 | 
			
		||||
    void removeSecurityGroup(long userId, long securityGroupId, String publicIp, long vmId, long startEventId) throws InvalidParameterValueException, PermissionDeniedException;
 | 
			
		||||
    void removeSecurityGroup(RemovePortForwardingServiceCmd cmd) throws InvalidParameterValueException, PermissionDeniedException;
 | 
			
		||||
    
 | 
			
		||||
    long assignSecurityGroupAsync(Long userId, Long securityGroupId, List<Long> securityGroupIdList, String publicIp, Long vmId);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -79,6 +79,7 @@ import com.cloud.api.commands.GetCloudIdentifierCmd;
 | 
			
		||||
import com.cloud.api.commands.PrepareForMaintenanceCmd;
 | 
			
		||||
import com.cloud.api.commands.PreparePrimaryStorageForMaintenanceCmd;
 | 
			
		||||
import com.cloud.api.commands.ReconnectHostCmd;
 | 
			
		||||
import com.cloud.api.commands.RemovePortForwardingServiceCmd;
 | 
			
		||||
import com.cloud.api.commands.StartRouterCmd;
 | 
			
		||||
import com.cloud.api.commands.StartSystemVMCmd;
 | 
			
		||||
import com.cloud.api.commands.StartVMCmd;
 | 
			
		||||
@ -3198,6 +3199,74 @@ public class ManagementServerImpl implements ManagementServer {
 | 
			
		||||
        return _asyncMgr.submitAsyncJob(job);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void removeSecurityGroup(RemovePortForwardingServiceCmd cmd) throws InvalidParameterValueException, PermissionDeniedException{
 | 
			
		||||
    	
 | 
			
		||||
    	Account account = (Account)UserContext.current().getAccountObject();
 | 
			
		||||
        Long userId = UserContext.current().getUserId();
 | 
			
		||||
        Long securityGroupId = cmd.getId();
 | 
			
		||||
        String publicIp = cmd.getPublicIp();
 | 
			
		||||
        Long vmId = cmd.getVirtualMachineId();
 | 
			
		||||
        
 | 
			
		||||
        //verify input parameters
 | 
			
		||||
        SecurityGroupVO securityG = _securityGroupDao.findById(securityGroupId);
 | 
			
		||||
        if (securityG == null) {
 | 
			
		||||
        	throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find a port forwarding service with id " + securityGroupId);
 | 
			
		||||
        } else if (account != null) {
 | 
			
		||||
            if (!isAdmin(account.getType()) && (account.getId().longValue() != securityG.getAccountId())) {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find a port forwarding service with id " + securityGroupId + " for this account");
 | 
			
		||||
            } else if (!isChildDomain(account.getDomainId(), securityG.getDomainId())) {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid port forwarding service id (" + securityGroupId + ") given, unable to remove port forwarding service.");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        UserVmVO vmInstance = findUserVMInstanceById(vmId.longValue());
 | 
			
		||||
        if (vmInstance == 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() != vmInstance.getAccountId())) {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId + " for this account");
 | 
			
		||||
            } else if (!isChildDomain(account.getDomainId(), vmInstance.getDomainId())) {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to remove port forwarding service.");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Account ipAddrAccount = findAccountByIpAddress(publicIp);
 | 
			
		||||
        if (ipAddrAccount == null) {
 | 
			
		||||
            if (account == null) {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find ip address " + publicIp);
 | 
			
		||||
            } else {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + publicIp);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Long accountId = ipAddrAccount.getId();
 | 
			
		||||
        if ((account != null) && !isAdmin(account.getType())) {
 | 
			
		||||
            if (account.getId().longValue() != accountId) {
 | 
			
		||||
                throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + publicIp);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (userId == null) {
 | 
			
		||||
            userId = Long.valueOf(1);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        long eventId = EventUtils.saveScheduledEvent(userId, vmInstance.getAccountId(), EventTypes.EVENT_PORT_FORWARDING_SERVICE_REMOVE, "removing security groups for Vm with Id: "+vmId);
 | 
			
		||||
 | 
			
		||||
        /*TODO : ASK KRIS AS TO WHAT DO WE DO WITH THIS PART IN THE EXECUTOR CODE
 | 
			
		||||
        UserVmVO userVm = userVmDao.findById(param.getInstanceId());
 | 
			
		||||
        if(userVm == null)
 | 
			
		||||
        	return null;
 | 
			
		||||
        
 | 
			
		||||
        if (userVm.getDomainRouterId() == null) {
 | 
			
		||||
        	return null;
 | 
			
		||||
        } else
 | 
			
		||||
        	return routerDao.findById(userVm.getDomainRouterId());
 | 
			
		||||
	    */
 | 
			
		||||
        removeSecurityGroup(userId, securityGroupId, publicIp, vmId, eventId);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    @Override
 | 
			
		||||
    @DB
 | 
			
		||||
    public void removeSecurityGroup(long userId, long securityGroupId, String publicIp, long vmId, long startEventId) throws InvalidParameterValueException, PermissionDeniedException {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user