diff --git a/server/src/com/cloud/api/commands/RemovePortForwardingServiceCmd.java b/server/src/com/cloud/api/commands/RemovePortForwardingServiceCmd.java index d5444d13337..a96ee31b160 100644 --- a/server/src/com/cloud/api/commands/RemovePortForwardingServiceCmd.java +++ b/server/src/com/cloud/api/commands/RemovePortForwardingServiceCmd.java @@ -18,35 +18,19 @@ 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; - -public class RemovePortForwardingServiceCmd extends BaseCmd { +import com.cloud.api.BaseCmd.Manager; + +@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> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.PUBLIC_IP, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.VIRTUAL_MACHINE_ID, Boolean.TRUE)); - - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - } - + ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// @@ -83,73 +67,75 @@ public class RemovePortForwardingServiceCmd extends BaseCmd { public String getName() { return s_name; } - - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map 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> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); - return returnValues; - } +// @Override +// public List> execute(Map 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> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), Long.valueOf(jobId))); +// return returnValues; +// } + + @Override + public String getResponse() { + // TODO Look at executor + return null; + } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 774dba2bbcb..4d5993a9ef5 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -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,7 +1754,8 @@ 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 securityGroupIdList, String publicIp, Long vmId); long removeSecurityGroupAsync(Long userId, long securityGroupId, String publicIp, long vmId); diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index c5cc233ede9..5d310c5d20e 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -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 {