From ec9fdd5b168de7c6cf8563c9b35dc32d373c625c Mon Sep 17 00:00:00 2001 From: abhishek Date: Fri, 20 Aug 2010 16:01:37 -0700 Subject: [PATCH] Refactored disassociateipaddress cmd --- .../api/commands/DisassociateIPAddrCmd.java | 121 ++++++++---------- .../com/cloud/server/ManagementServer.java | 3 +- .../cloud/server/ManagementServerImpl.java | 39 +++++- 3 files changed, 92 insertions(+), 71 deletions(-) diff --git a/server/src/com/cloud/api/commands/DisassociateIPAddrCmd.java b/server/src/com/cloud/api/commands/DisassociateIPAddrCmd.java index 50fb9e55b1b..4fdcd5a03f7 100644 --- a/server/src/com/cloud/api/commands/DisassociateIPAddrCmd.java +++ b/server/src/com/cloud/api/commands/DisassociateIPAddrCmd.java @@ -18,30 +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.Implementation; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; -import com.cloud.exception.PermissionDeniedException; -import com.cloud.user.Account; -import com.cloud.utils.Pair; - +import com.cloud.api.BaseCmd.Manager; + +@Implementation(method="disassociateIpAddress", manager=Manager.ManagementServer) public class DisassociateIPAddrCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(DisassociateIPAddrCmd.class.getName()); private static final String s_name = "disassociateipaddressresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.IP_ADDRESS, 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 ///////////////////// @@ -67,56 +55,59 @@ public class DisassociateIPAddrCmd extends BaseCmd { public String getName() { return s_name; } - - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map params) { - Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - String ipAddress = (String)params.get(BaseCmd.Properties.IP_ADDRESS.getName()); - boolean result = false; +// @Override +// public List> execute(Map params) { +// Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); +// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); +// String ipAddress = (String)params.get(BaseCmd.Properties.IP_ADDRESS.getName()); +// boolean result = false; +// +// // Verify input parameters +// Account accountByIp = getManagementServer().findAccountByIpAddress(ipAddress); +// if(accountByIp == null) { +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find account owner for ip " + ipAddress); +// } +// +// Long accountId = accountByIp.getId(); +// if (account != null) { +// if (!isAdmin(account.getType())) { +// if (account.getId().longValue() != accountId.longValue()) { +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + ipAddress); +// } +// } else if (!getManagementServer().isChildDomain(account.getDomainId(), accountByIp.getDomainId())) { +// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to disassociate IP address " + ipAddress + ", permission denied."); +// } +// } +// +// // If command is executed via 8096 port, set userId to the id of System account (1) +// if (userId == null) { +// userId = Long.valueOf(1); +// } +// +// try { +// result = getManagementServer().disassociateIpAddress(userId.longValue(), accountId.longValue(), ipAddress); +// } catch (PermissionDeniedException ex) { +// throw new ServerApiException(BaseCmd.NET_INVALID_PARAM_ERROR, ex.getMessage()); +// } catch (IllegalArgumentException ex1) { +// throw new ServerApiException(BaseCmd.NET_INVALID_PARAM_ERROR, ex1.getMessage()); +// } catch (Exception ex2) { +// throw new ServerApiException(BaseCmd.NET_IP_DIASSOC_ERROR, "unable to disassociate ip address"); +// } +// +// if (result == false) { +// throw new ServerApiException(BaseCmd.NET_IP_DIASSOC_ERROR, "unable to disassociate ip address"); +// } +// +// List> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(result).toString())); +// return returnValues; +// } - // Verify input parameters - Account accountByIp = getManagementServer().findAccountByIpAddress(ipAddress); - if(accountByIp == null) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find account owner for ip " + ipAddress); - } - Long accountId = accountByIp.getId(); - if (account != null) { - if (!isAdmin(account.getType())) { - if (account.getId().longValue() != accountId.longValue()) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + ipAddress); - } - } else if (!getManagementServer().isChildDomain(account.getDomainId(), accountByIp.getDomainId())) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to disassociate IP address " + ipAddress + ", permission denied."); - } - } - - // If command is executed via 8096 port, set userId to the id of System account (1) - if (userId == null) { - userId = Long.valueOf(1); - } - - try { - result = getManagementServer().disassociateIpAddress(userId.longValue(), accountId.longValue(), ipAddress); - } catch (PermissionDeniedException ex) { - throw new ServerApiException(BaseCmd.NET_INVALID_PARAM_ERROR, ex.getMessage()); - } catch (IllegalArgumentException ex1) { - throw new ServerApiException(BaseCmd.NET_INVALID_PARAM_ERROR, ex1.getMessage()); - } catch (Exception ex2) { - throw new ServerApiException(BaseCmd.NET_IP_DIASSOC_ERROR, "unable to disassociate ip address"); - } - - if (result == false) { - throw new ServerApiException(BaseCmd.NET_IP_DIASSOC_ERROR, "unable to disassociate ip address"); - } - - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(result).toString())); - return returnValues; - } + @Override + public String getResponse() { + // TODO Auto-generated method stub + return null; + } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index e87feb807d7..849401567d1 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -26,6 +26,7 @@ import com.cloud.alert.AlertVO; import com.cloud.api.commands.CreateDomainCmd; import com.cloud.api.commands.CreatePortForwardingServiceCmd; import com.cloud.api.commands.CreatePortForwardingServiceRuleCmd; +import com.cloud.api.commands.DisassociateIPAddrCmd; import com.cloud.api.commands.EnableAccountCmd; import com.cloud.api.commands.EnableUserCmd; import com.cloud.api.commands.GetCloudIdentifierCmd; @@ -441,7 +442,7 @@ public interface ManagementServer { * @param ipAddress * @return success */ - boolean disassociateIpAddress(long userId, long accountId, String ipAddress) throws PermissionDeniedException; + boolean disassociateIpAddress(DisassociateIPAddrCmd cmd) throws PermissionDeniedException; long disassociateIpAddressAsync(long userId, long accountId, String ipAddress); /** diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 58373c74e55..a91cf3c2c3d 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -68,6 +68,7 @@ import com.cloud.api.commands.DeleteIsoCmd; import com.cloud.api.commands.DeleteTemplateCmd; import com.cloud.api.commands.DeleteUserCmd; import com.cloud.api.commands.DeployVMCmd; +import com.cloud.api.commands.DisassociateIPAddrCmd; import com.cloud.api.commands.EnableAccountCmd; import com.cloud.api.commands.EnableUserCmd; import com.cloud.api.commands.GetCloudIdentifierCmd; @@ -1689,10 +1690,38 @@ public class ManagementServerImpl implements ManagementServer { @Override @DB - public boolean disassociateIpAddress(long userId, long accountId, String publicIPAddress) throws PermissionDeniedException, IllegalArgumentException { + public boolean disassociateIpAddress(DisassociateIPAddrCmd cmd) throws PermissionDeniedException, IllegalArgumentException { Transaction txn = Transaction.currentTxn(); + + Long userId = UserContext.current().getUserId(); + Account account = (Account)UserContext.current().getAccountObject(); + String ipAddress = cmd.getIpAddress(); + boolean result = false; + + // Verify input parameters + Account accountByIp = findAccountByIpAddress(ipAddress); + if(accountByIp == null) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find account owner for ip " + ipAddress); + } + + Long accountId = accountByIp.getId(); + if (account != null) { + if (!isAdmin(account.getType())) { + if (account.getId().longValue() != accountId.longValue()) { + throw new ServerApiException(BaseCmd.PARAM_ERROR, "account " + account.getAccountName() + " doesn't own ip address " + ipAddress); + } + } else if (!isChildDomain(account.getDomainId(), accountByIp.getDomainId())) { + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to disassociate IP address " + ipAddress + ", permission denied."); + } + } + + // If command is executed via 8096 port, set userId to the id of System account (1) + if (userId == null) { + userId = Long.valueOf(1); + } + try { - IPAddressVO ipVO = _publicIpAddressDao.findById(publicIPAddress); + IPAddressVO ipVO = _publicIpAddressDao.findById(ipAddress); if (ipVO == null) { return false; } @@ -1710,7 +1739,7 @@ public class ManagementServerImpl implements ManagementServer { // FIXME: is the user visible in the admin account's domain???? if (!BaseCmd.isAdmin(accountVO.getType())) { if (s_logger.isDebugEnabled()) { - s_logger.debug("permission denied disassociating IP address " + publicIPAddress + "; acct: " + accountId + "; ip (acct / dc / dom / alloc): " + s_logger.debug("permission denied disassociating IP address " + ipAddress + "; acct: " + accountId + "; ip (acct / dc / dom / alloc): " + ipVO.getAccountId() + " / " + ipVO.getDataCenterId() + " / " + ipVO.getDomainId() + " / " + ipVO.getAllocated()); } throw new PermissionDeniedException("User/account does not own supplied address"); @@ -1732,11 +1761,11 @@ public class ManagementServerImpl implements ManagementServer { //Check for account wide pool. It will have an entry for account_vlan_map. if (_accountVlanMapDao.findAccountVlanMap(accountId,ipVO.getVlanDbId()) != null){ - throw new PermissionDeniedException(publicIPAddress + " belongs to Account wide IP pool and cannot be disassociated"); + throw new PermissionDeniedException(ipAddress + " belongs to Account wide IP pool and cannot be disassociated"); } txn.start(); - boolean success = _networkMgr.releasePublicIpAddress(userId, publicIPAddress); + boolean success = _networkMgr.releasePublicIpAddress(userId, ipAddress); if (success) _accountMgr.decrementResourceCount(accountId, ResourceType.public_ip); txn.commit();