Refactoring the removePortForwardingService cmd

This commit is contained in:
abhishek 2010-08-19 10:55:58 -07:00
parent 25d5126f68
commit ef2b80f9c6
3 changed files with 149 additions and 92 deletions

View File

@ -18,35 +18,19 @@
package com.cloud.api.commands; package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger; 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.Parameter;
import com.cloud.api.ServerApiException; import com.cloud.api.BaseCmd.Manager;
import com.cloud.network.SecurityGroupVO;
import com.cloud.user.Account; @Implementation(method="removeSecurityGroup", manager=Manager.ManagementServer)
import com.cloud.utils.Pair; public class RemovePortForwardingServiceCmd extends BaseAsyncCmd {
import com.cloud.vm.UserVmVO;
public class RemovePortForwardingServiceCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(RemovePortForwardingServiceCmd.class.getName()); public static final Logger s_logger = Logger.getLogger(RemovePortForwardingServiceCmd.class.getName());
private static final String s_name = "removeportforwardingserviceresponse"; 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 ///////////////////// //////////////// API parameters /////////////////////
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
@ -83,73 +67,75 @@ public class RemovePortForwardingServiceCmd extends BaseCmd {
public String getName() { public String getName() {
return s_name; return s_name;
} }
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
@Override // @Override
public List<Pair<String, Object>> execute(Map<String, Object> params) { // public List<Pair<String, Object>> execute(Map<String, Object> params) {
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); // Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); // Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
Long securityGroupId = (Long)params.get(BaseCmd.Properties.ID.getName()); // Long securityGroupId = (Long)params.get(BaseCmd.Properties.ID.getName());
String publicIp = (String)params.get(BaseCmd.Properties.PUBLIC_IP.getName()); // String publicIp = (String)params.get(BaseCmd.Properties.PUBLIC_IP.getName());
Long vmId = (Long)params.get(BaseCmd.Properties.VIRTUAL_MACHINE_ID.getName()); // Long vmId = (Long)params.get(BaseCmd.Properties.VIRTUAL_MACHINE_ID.getName());
//
//verify input parameters // //verify input parameters
SecurityGroupVO securityG = getManagementServer().findSecurityGroupById(securityGroupId); // SecurityGroupVO securityG = getManagementServer().findSecurityGroupById(securityGroupId);
if (securityG == null) { // if (securityG == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find a port forwarding service with id " + securityGroupId); // throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find a port forwarding service with id " + securityGroupId);
} else if (account != null) { // } else if (account != null) {
if (!isAdmin(account.getType()) && (account.getId().longValue() != securityG.getAccountId())) { // 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"); // 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())) { // } 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."); // 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()); // UserVmVO vmInstance = getManagementServer().findUserVMInstanceById(vmId.longValue());
if (vmInstance == null) { // if (vmInstance == null) {
throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId); // throw new ServerApiException(BaseCmd.VM_INVALID_PARAM_ERROR, "unable to find a virtual machine with id " + vmId);
} // }
if (account != null) { // if (account != null) {
if (!isAdmin(account.getType()) && (account.getId().longValue() != vmInstance.getAccountId())) { // 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"); // 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())) { // } 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."); // throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid virtual machine id (" + vmId + ") given, unable to remove port forwarding service.");
} // }
} // }
//
Account ipAddrAccount = getManagementServer().findAccountByIpAddress(publicIp); // Account ipAddrAccount = getManagementServer().findAccountByIpAddress(publicIp);
if (ipAddrAccount == null) { // if (ipAddrAccount == null) {
if (account == null) { // if (account == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find ip address " + publicIp); // throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find ip address " + publicIp);
} else { // } else {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + publicIp); // throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + publicIp);
} // }
} // }
//
Long accountId = ipAddrAccount.getId(); // Long accountId = ipAddrAccount.getId();
if ((account != null) && !isAdmin(account.getType())) { // if ((account != null) && !isAdmin(account.getType())) {
if (account.getId().longValue() != accountId) { // if (account.getId().longValue() != accountId) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + publicIp); // throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + publicIp);
} // }
} // }
//
if (userId == null) { // if (userId == null) {
userId = Long.valueOf(1); // userId = Long.valueOf(1);
} // }
//
long jobId = getManagementServer().removeSecurityGroupAsync(userId, securityGroupId, publicIp, vmId); // long jobId = getManagementServer().removeSecurityGroupAsync(userId, securityGroupId, publicIp, vmId);
if(jobId == 0) { // if(jobId == 0) {
s_logger.warn("Unable to schedule async-job for RemovePortForwardingServiceCmd comamnd"); // s_logger.warn("Unable to schedule async-job for RemovePortForwardingServiceCmd comamnd");
} else { // } else {
if(s_logger.isDebugEnabled()) // if(s_logger.isDebugEnabled())
s_logger.debug("RemovePortForwardingServiceCmd command has been accepted, job id: " + jobId); // s_logger.debug("RemovePortForwardingServiceCmd command has been accepted, job id: " + jobId);
} // }
//
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>(); // List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); // returnValues.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId)));
return returnValues; // return returnValues;
} // }
@Override
public String getResponse() {
// TODO Look at executor
return null;
}
} }

View File

@ -29,6 +29,7 @@ import com.cloud.api.commands.CreatePortForwardingServiceRuleCmd;
import com.cloud.api.commands.EnableAccountCmd; import com.cloud.api.commands.EnableAccountCmd;
import com.cloud.api.commands.EnableUserCmd; import com.cloud.api.commands.EnableUserCmd;
import com.cloud.api.commands.GetCloudIdentifierCmd; import com.cloud.api.commands.GetCloudIdentifierCmd;
import com.cloud.api.commands.RemovePortForwardingServiceCmd;
import com.cloud.api.commands.StartSystemVMCmd; import com.cloud.api.commands.StartSystemVMCmd;
import com.cloud.api.commands.StopSystemVmCmd; import com.cloud.api.commands.StopSystemVmCmd;
import com.cloud.api.commands.UpdateAccountCmd; import com.cloud.api.commands.UpdateAccountCmd;
@ -1753,7 +1754,8 @@ public interface ManagementServer {
* @param vmId * @param vmId
*/ */
void removeSecurityGroup(long userId, long securityGroupId, String publicIp, long vmId, long startEventId) throws InvalidParameterValueException, PermissionDeniedException; 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); long assignSecurityGroupAsync(Long userId, Long securityGroupId, List<Long> securityGroupIdList, String publicIp, Long vmId);
long removeSecurityGroupAsync(Long userId, long securityGroupId, String publicIp, long vmId); long removeSecurityGroupAsync(Long userId, long securityGroupId, String publicIp, long vmId);

View File

@ -79,6 +79,7 @@ import com.cloud.api.commands.GetCloudIdentifierCmd;
import com.cloud.api.commands.PrepareForMaintenanceCmd; import com.cloud.api.commands.PrepareForMaintenanceCmd;
import com.cloud.api.commands.PreparePrimaryStorageForMaintenanceCmd; import com.cloud.api.commands.PreparePrimaryStorageForMaintenanceCmd;
import com.cloud.api.commands.ReconnectHostCmd; import com.cloud.api.commands.ReconnectHostCmd;
import com.cloud.api.commands.RemovePortForwardingServiceCmd;
import com.cloud.api.commands.StartRouterCmd; import com.cloud.api.commands.StartRouterCmd;
import com.cloud.api.commands.StartSystemVMCmd; import com.cloud.api.commands.StartSystemVMCmd;
import com.cloud.api.commands.StartVMCmd; import com.cloud.api.commands.StartVMCmd;
@ -3198,6 +3199,74 @@ public class ManagementServerImpl implements ManagementServer {
return _asyncMgr.submitAsyncJob(job); 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 @Override
@DB @DB
public void removeSecurityGroup(long userId, long securityGroupId, String publicIp, long vmId, long startEventId) throws InvalidParameterValueException, PermissionDeniedException { public void removeSecurityGroup(long userId, long securityGroupId, String publicIp, long vmId, long startEventId) throws InvalidParameterValueException, PermissionDeniedException {