From 22b0a95b2f7c5f36d72cbb0baacbb227a614fcaa Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Thu, 2 Sep 2010 16:23:29 -0700 Subject: [PATCH] Refactor listPublicIpAddresses to new API framework. --- .../commands/ListPublicIpAddressesCmd.java | 189 +++++------------- .../cloud/api/response/IPAddressResponse.java | 129 ++++++++++++ .../com/cloud/server/ManagementServer.java | 5 +- .../cloud/server/ManagementServerImpl.java | 61 ++++-- 4 files changed, 220 insertions(+), 164 deletions(-) create mode 100644 server/src/com/cloud/api/response/IPAddressResponse.java diff --git a/server/src/com/cloud/api/commands/ListPublicIpAddressesCmd.java b/server/src/com/cloud/api/commands/ListPublicIpAddressesCmd.java index b12fd25b6aa..8460ee80a27 100644 --- a/server/src/com/cloud/api/commands/ListPublicIpAddressesCmd.java +++ b/server/src/com/cloud/api/commands/ListPublicIpAddressesCmd.java @@ -20,41 +20,25 @@ 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.BaseListCmd; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; +import com.cloud.api.response.IPAddressResponse; import com.cloud.dc.Vlan.VlanType; import com.cloud.dc.VlanVO; -import com.cloud.domain.DomainVO; import com.cloud.network.IPAddressVO; -import com.cloud.server.Criteria; +import com.cloud.serializer.SerializerHelper; import com.cloud.user.Account; -import com.cloud.utils.Pair; - -public class ListPublicIpAddressesCmd extends BaseCmd { +import com.cloud.user.UserContext; + +@Implementation(method="searchForIPAddresses") +public class ListPublicIpAddressesCmd extends BaseListCmd { public static final Logger s_logger = Logger.getLogger(ListPublicIpAddressesCmd.class.getName()); private static final String s_name = "listpublicipaddressesresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ALLOCATED_ONLY, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.FOR_VIRTUAL_NETWORK, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.IP_ADDRESS, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.VLAN_DB_ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ZONE_ID, Boolean.FALSE)); - - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.KEYWORD, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGESIZE, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -122,127 +106,44 @@ public class ListPublicIpAddressesCmd extends BaseCmd { return s_name; } - @Override - public List> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map params) { - String accountName = (String) params.get(BaseCmd.Properties.ACCOUNT.getName()); - Long domainId = (Long) params.get(BaseCmd.Properties.DOMAIN_ID.getName()); - Account account = (Account) params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - Boolean allocatedOnly = (Boolean) params.get(BaseCmd.Properties.ALLOCATED_ONLY.getName()); - Long zoneId = (Long) params.get(BaseCmd.Properties.ZONE_ID.getName()); - Long vlanDbId = (Long) params.get(BaseCmd.Properties.VLAN_DB_ID.getName()); - String ip = (String) params.get(BaseCmd.Properties.IP_ADDRESS.getName()); - String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName()); - Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName()); - Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName()); - Boolean forVirtualNetwork = (Boolean) params.get(BaseCmd.Properties.FOR_VIRTUAL_NETWORK.getName()); - boolean isAdmin = false; - - if (allocatedOnly == null) - allocatedOnly = new Boolean(true); - - Long accountId = null; - if ((account == null) || isAdmin(account.getType())) { - isAdmin = true; - // validate domainId before proceeding - if (domainId != null) { - if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), domainId)) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid domain id (" + domainId + ") given, unable to list IP addresses."); - } - if (accountName != null) { - Account userAccount = getManagementServer().findAccountByName(accountName, domainId); - if (userAccount != null) { - accountId = userAccount.getId(); - } else { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find account " + accountName + " in domain " + domainId); - } - } - } else { - domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId()); - } - } else { - accountId = account.getId(); - } - - Long[] accountIds = null; - if (accountId != null) { - accountIds = new Long[1]; - accountIds[0] = accountId; - } - - Long startIndex = Long.valueOf(0); - int pageSizeNum = 50; - if (pageSize != null) { - pageSizeNum = pageSize.intValue(); - } - if (page != null) { - int pageNum = page.intValue(); - if (pageNum > 0) { - startIndex = Long.valueOf(pageSizeNum * (pageNum-1)); - } - } - - Criteria c = new Criteria("address", Boolean.FALSE, startIndex, Long.valueOf(pageSizeNum)); - - c.addCriteria(Criteria.ACCOUNTID, accountIds); - if (allocatedOnly) { - c.addCriteria(Criteria.ISALLOCATED, allocatedOnly.booleanValue()); - } - - if (keyword != null) { - c.addCriteria(Criteria.KEYWORD, keyword); - } else { - if (isAdmin) { - c.addCriteria(Criteria.DOMAINID, domainId); - c.addCriteria(Criteria.VLAN, vlanDbId); + @Override @SuppressWarnings("unchecked") + public String getResponse() { + List ipAddresses = (List)getResponseObject(); + + List response = new ArrayList(); + for (IPAddressVO ipAddress : ipAddresses) { + VlanVO vlan = getManagementServer().findVlanById(ipAddress.getVlanDbId()); + boolean forVirtualNetworks = vlan.getVlanType().equals(VlanType.VirtualNetwork); + + IPAddressResponse ipResponse = new IPAddressResponse(); + ipResponse.setIpAddress(ipAddress.getAddress()); + if (ipAddress.getAllocated() != null) { + ipResponse.setAllocated(ipAddress.getAllocated()); } - c.addCriteria(Criteria.FOR_VIRTUAL_NETWORK, forVirtualNetwork); - c.addCriteria(Criteria.DATACENTERID, zoneId); - c.addCriteria(Criteria.IPADDRESS, ip); - } - - List result = getManagementServer().searchForIPAddresses(c); - - if (result == null) { - throw new ServerApiException(BaseCmd.NET_LIST_ERROR, "unable to find IP Addresses for account: " + accountId); - } - List> ipAddrTags = new ArrayList>(); - Object[] ipTag = new Object[result.size()]; - int i = 0; - for (IPAddressVO ipAddress : result) { - VlanVO vlan = getManagementServer().findVlanById(ipAddress.getVlanDbId()); - boolean forVirtualNetworks = vlan.getVlanType().equals(VlanType.VirtualNetwork); - - List> ipAddrData = new ArrayList>(); - ipAddrData.add(new Pair(BaseCmd.Properties.IP_ADDRESS.getName(), ipAddress.getAddress())); - if (ipAddress.getAllocated() != null) { - ipAddrData.add(new Pair(BaseCmd.Properties.ALLOCATED.getName(), getDateString(ipAddress.getAllocated()))); - } - ipAddrData.add(new Pair(BaseCmd.Properties.ZONE_ID.getName(), Long.valueOf(ipAddress.getDataCenterId()).toString())); - ipAddrData.add(new Pair(BaseCmd.Properties.ZONE_NAME.getName(), getManagementServer().findDataCenterById(ipAddress.getDataCenterId()).getName())); - ipAddrData.add(new Pair(BaseCmd.Properties.IS_SOURCE_NAT.getName(), Boolean.valueOf(ipAddress.isSourceNat()).toString())); - //get account information - Account accountTemp = getManagementServer().findAccountById(ipAddress.getAccountId()); - if (accountTemp !=null){ - ipAddrData.add(new Pair(BaseCmd.Properties.ACCOUNT.getName(), accountTemp.getAccountName())); - ipAddrData.add(new Pair(BaseCmd.Properties.DOMAIN_ID.getName(), accountTemp.getDomainId())); - ipAddrData.add(new Pair(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName())); - } + ipResponse.setZoneId(ipAddress.getDataCenterId()); + ipResponse.setZoneName(getManagementServer().findDataCenterById(ipAddress.getDataCenterId()).getName()); + ipResponse.setSourceNat(ipAddress.isSourceNat()); + + //get account information + Account accountTemp = getManagementServer().findAccountById(ipAddress.getAccountId()); + if (accountTemp !=null){ + ipResponse.setAccountName(accountTemp.getAccountName()); + ipResponse.setDomainId(accountTemp.getDomainId()); + ipResponse.setDomainName(getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName()); + } - ipAddrData.add(new Pair(BaseCmd.Properties.FOR_VIRTUAL_NETWORK.getName(), forVirtualNetworks)); - //show this info to admin only - if (isAdmin == true) { - ipAddrData.add(new Pair(BaseCmd.Properties.VLAN_DB_ID.getName(), Long.valueOf(ipAddress.getVlanDbId()).toString())); - ipAddrData.add(new Pair(BaseCmd.Properties.VLAN_ID.getName(), getManagementServer().findVlanById(ipAddress.getVlanDbId()).getVlanId())); - } - ipTag[i++] = ipAddrData; - } - Pair ipAddrTag = new Pair("publicipaddress", ipTag); - ipAddrTags.add(ipAddrTag); - return ipAddrTags; + ipResponse.setForVirtualNetwork(forVirtualNetworks); + + //show this info to admin only + Account account = (Account)UserContext.current().getAccountObject(); + if ((account == null) || isAdmin(account.getType())) { + ipResponse.setVlanId(ipAddress.getVlanDbId()); + ipResponse.setVlanName(getManagementServer().findVlanById(ipAddress.getVlanDbId()).getVlanId()); + } + + response.add(ipResponse); + } + + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/api/response/IPAddressResponse.java b/server/src/com/cloud/api/response/IPAddressResponse.java new file mode 100644 index 00000000000..7d01b004eff --- /dev/null +++ b/server/src/com/cloud/api/response/IPAddressResponse.java @@ -0,0 +1,129 @@ +package com.cloud.api.response; + +import java.util.Date; + +import com.cloud.api.ResponseObject; +import com.cloud.serializer.Param; + +public class IPAddressResponse implements ResponseObject { + @Param(name="ipaddress") + private String ipAddress; + + @Param(name="allocated") + private Date allocated; + + @Param(name="zoneid") + private Long zoneId; + + @Param(name="zonename") + private String zoneName; + + @Param(name="issourcenat") + private Boolean sourceNat; + + @Param(name="account") + private String accountName; + + @Param(name="domainid") + private Long domainId; + + @Param(name="domain") + private String domainName; + + @Param(name="forvirtualnetwork") + private Boolean forVirtualNetwork; + + @Param(name="vlanid") + private Long vlanId; + + @Param(name="vlanname") + private String vlanName; + + public String getIpAddress() { + return ipAddress; + } + + public void setIpAddress(String ipAddress) { + this.ipAddress = ipAddress; + } + + public Date getAllocated() { + return allocated; + } + + public void setAllocated(Date allocated) { + this.allocated = allocated; + } + + public Long getZoneId() { + return zoneId; + } + + public void setZoneId(Long zoneId) { + this.zoneId = zoneId; + } + + public String getZoneName() { + return zoneName; + } + + public void setZoneName(String zoneName) { + this.zoneName = zoneName; + } + + public Boolean getSourceNat() { + return sourceNat; + } + + public void setSourceNat(Boolean sourceNat) { + this.sourceNat = sourceNat; + } + + public String getAccountName() { + return accountName; + } + + public void setAccountName(String accountName) { + this.accountName = accountName; + } + + public Long getDomainId() { + return domainId; + } + + public void setDomainId(Long domainId) { + this.domainId = domainId; + } + + public String getDomainName() { + return domainName; + } + + public void setDomainName(String domainName) { + this.domainName = domainName; + } + + public Boolean getForVirtualNetwork() { + return forVirtualNetwork; + } + + public void setForVirtualNetwork(Boolean forVirtualNetwork) { + this.forVirtualNetwork = forVirtualNetwork; + } + + public Long getVlanId() { + return vlanId; + } + + public void setVlanId(Long vlanId) { + this.vlanId = vlanId; + } + + public String getVlanName() { + return vlanName; + } + + public void setVlanName(String vlanName) { + this.vlanName = vlanName; + } +} diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index f3603b64f66..1e3bc641478 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -51,6 +51,7 @@ import com.cloud.api.commands.ListPortForwardingServiceRulesCmd; import com.cloud.api.commands.ListPortForwardingServicesByVmCmd; import com.cloud.api.commands.ListPortForwardingServicesCmd; import com.cloud.api.commands.ListPreallocatedLunsCmd; +import com.cloud.api.commands.ListPublicIpAddressesCmd; import com.cloud.api.commands.ListTemplatesCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; @@ -1245,10 +1246,10 @@ public interface ManagementServer { /** * Obtains a list of IP Addresses by the specified search criteria. * Can search by: "userId", "dataCenterId", "address" - * @param sc + * @param cmd the command that wraps the search criteria * @return List of IPAddresses */ - List searchForIPAddresses(Criteria c); + List searchForIPAddresses(ListPublicIpAddressesCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; /** * Obtains a list of billing records by the specified search criteria. diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 617ee15040a..512731fd45f 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -89,6 +89,7 @@ import com.cloud.api.commands.ListPortForwardingServiceRulesCmd; import com.cloud.api.commands.ListPortForwardingServicesByVmCmd; import com.cloud.api.commands.ListPortForwardingServicesCmd; import com.cloud.api.commands.ListPreallocatedLunsCmd; +import com.cloud.api.commands.ListPublicIpAddressesCmd; import com.cloud.api.commands.ListTemplatesCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; @@ -5569,26 +5570,54 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public List searchForIPAddresses(Criteria c) { - Filter searchFilter = new Filter(IPAddressVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit()); + public List searchForIPAddresses(ListPublicIpAddressesCmd cmd) throws InvalidParameterValueException, PermissionDeniedException { + Account account = (Account)UserContext.current().getAccountObject(); + Long domainId = cmd.getDomainId(); + String accountName = cmd.getAccountName(); + Long accountId = null; - Object[] accountIds = (Object[]) c.getCriteria(Criteria.ACCOUNTID); - Object zone = c.getCriteria(Criteria.DATACENTERID); - Object address = c.getCriteria(Criteria.IPADDRESS); - Object domainId = c.getCriteria(Criteria.DOMAINID); - Object vlan = c.getCriteria(Criteria.VLAN); - Object isAllocated = c.getCriteria(Criteria.ISALLOCATED); - Object keyword = c.getCriteria(Criteria.KEYWORD); - Object forVirtualNetwork = c.getCriteria(Criteria.FOR_VIRTUAL_NETWORK); + if ((account == null) || isAdmin(account.getType())) { + // validate domainId before proceeding + if (domainId != null) { + if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) { + throw new PermissionDeniedException("Unable to list IP addresses for domain " + domainId + ", permission denied."); + } + + if (accountName != null) { + Account userAccount = _accountDao.findActiveAccount(accountName, domainId); + if (userAccount != null) { + accountId = userAccount.getId(); + } else { + throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId); + } + } + } else { + domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId()); + } + } else { + accountId = account.getId(); + } + + Boolean isAllocated = cmd.isAllocatedOnly(); + if (isAllocated == null) { + isAllocated = Boolean.TRUE; + } + + Filter searchFilter = new Filter(IPAddressVO.class, "address", false, cmd.getStartIndex(), cmd.getPageSizeVal()); + + Object zone = cmd.getZoneId(); + Object address = cmd.getIpAddress(); + Object vlan = cmd.getVlanId(); + Object keyword = cmd.getKeyword(); + Object forVirtualNetwork = cmd.isForVirtualNetwork(); SearchBuilder sb = _publicIpAddressDao.createSearchBuilder(); sb.and("accountIdEQ", sb.entity().getAccountId(), SearchCriteria.Op.EQ); - sb.and("accountIdIN", sb.entity().getAccountId(), SearchCriteria.Op.IN); sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ); sb.and("address", sb.entity().getAddress(), SearchCriteria.Op.LIKE); sb.and("vlanDbId", sb.entity().getVlanDbId(), SearchCriteria.Op.EQ); - if ((accountIds == null) && (domainId != null)) { + if ((accountId == null) && (domainId != null)) { // if accountId isn't specified, we can do a domain match for the admin case SearchBuilder domainSearch = _domainDao.createSearchBuilder(); domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE); @@ -5606,12 +5635,8 @@ public class ManagementServerImpl implements ManagementServer { } SearchCriteria sc = sb.create(); - if (accountIds != null) { - if ((accountIds.length == 1) && (accountIds[0] != null)) { - sc.setParameters("accountIdEQ", accountIds[0]); - } else { - sc.setParameters("accountIdIN", accountIds); - } + if (accountId != null) { + sc.setParameters("accountIdEQ", accountId); } else if (domainId != null) { DomainVO domain = _domainDao.findById((Long)domainId); sc.setJoinParameters("domainSearch", "path", domain.getPath() + "%");