Refactored disassociateipaddress cmd

This commit is contained in:
abhishek 2010-08-20 16:01:37 -07:00
parent 5503013e1f
commit ec9fdd5b16
3 changed files with 92 additions and 71 deletions

View File

@ -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<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.IP_ADDRESS, 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 /////////////////////
@ -68,55 +56,58 @@ public class DisassociateIPAddrCmd 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) {
// 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<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(result).toString()));
// return returnValues;
// }
@Override
public List<Pair<String, Object>> execute(Map<String, Object> 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<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(result).toString()));
return returnValues;
}
@Override
public String getResponse() {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -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);
/**

View File

@ -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();