From c8949abb7afddd5daef5039ec939d02cfdc4449d Mon Sep 17 00:00:00 2001 From: alena Date: Thu, 10 Feb 2011 13:57:11 -0800 Subject: [PATCH] bug 7434: list direct service offerings for account only when 1) account has Account specific Direct vlan range 2) there are Zone wide Direct vlan ranges status 7434: resolved fixed --- .../api/commands/ListServiceOfferingsCmd.java | 36 ++++++++++++++++++- .../com/cloud/api/commands/ListVMsCmd.java | 4 +-- .../cloud/server/ManagementServerImpl.java | 34 +++++++++++++++++- 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/server/src/com/cloud/api/commands/ListServiceOfferingsCmd.java b/server/src/com/cloud/api/commands/ListServiceOfferingsCmd.java index d301f9a8955..9e95a5d4594 100644 --- a/server/src/com/cloud/api/commands/ListServiceOfferingsCmd.java +++ b/server/src/com/cloud/api/commands/ListServiceOfferingsCmd.java @@ -27,8 +27,8 @@ import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; import com.cloud.api.ServerApiException; import com.cloud.server.Criteria; -import com.cloud.service.ServiceOfferingVO; import com.cloud.service.ServiceOffering.GuestIpType; +import com.cloud.service.ServiceOfferingVO; import com.cloud.user.Account; import com.cloud.utils.Pair; import com.cloud.vm.UserVmVO; @@ -47,6 +47,9 @@ public class ListServiceOfferingsCmd extends BaseCmd { s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.PAGE, Boolean.FALSE)); s_properties.add(new Pair(BaseCmd.Properties.PAGESIZE, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); + s_properties.add(new Pair(BaseCmd.Properties.ZONE_ID, Boolean.FALSE)); } public String getName() { @@ -65,6 +68,9 @@ public class ListServiceOfferingsCmd extends BaseCmd { Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName()); Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName()); + Long zoneId = (Long)params.get(BaseCmd.Properties.ZONE_ID.getName()); + String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); + Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); Long startIndex = Long.valueOf(0); int pageSizeNum = 50; @@ -81,6 +87,7 @@ public class ListServiceOfferingsCmd extends BaseCmd { c.addCriteria(Criteria.KEYWORD, keyword); c.addCriteria(Criteria.ID, id); c.addCriteria(Criteria.NAME, name); + c.addCriteria(Criteria.DATACENTERID, zoneId); //If vmId is present in the list of parameters, verify it if (vmId != null) { @@ -96,6 +103,33 @@ public class ListServiceOfferingsCmd extends BaseCmd { if (keyword == null) c.addCriteria(Criteria.INSTANCEID, vmId); } + + //add account information to the criteria + Long accountId = null; + if ((account == null) || isAdmin(account.getType())) { + 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 service offerings."); + } + + if (accountName != null) { + account = getManagementServer().findActiveAccount(accountName, domainId); + if (account == null) { + throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find account " + accountName + " in domain " + domainId); + } + accountId = account.getId(); + } + } else if (account != null && isAdmin(account.getType())) { + accountId = account.getId(); + } + } else { + accountId = account.getId(); + } + + + if (accountId != null) { + c.addCriteria(Criteria.ACCOUNTID, accountId); + } List offerings = getManagementServer().searchForServiceOfferings(c); if (offerings == null) { diff --git a/server/src/com/cloud/api/commands/ListVMsCmd.java b/server/src/com/cloud/api/commands/ListVMsCmd.java index 3cb8299e5e7..a8449317581 100644 --- a/server/src/com/cloud/api/commands/ListVMsCmd.java +++ b/server/src/com/cloud/api/commands/ListVMsCmd.java @@ -31,15 +31,13 @@ import com.cloud.api.ServerApiException; import com.cloud.async.AsyncJobVO; import com.cloud.domain.DomainVO; import com.cloud.host.HostVO; -import com.cloud.network.security.NetworkGroupVMMapVO; -import com.cloud.network.security.NetworkGroupVO; import com.cloud.server.Criteria; import com.cloud.service.ServiceOfferingVO; import com.cloud.storage.VMTemplateVO; import com.cloud.user.Account; import com.cloud.utils.Pair; -import com.cloud.vm.VmStats; import com.cloud.vm.UserVm; +import com.cloud.vm.VmStats; public class ListVMsCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(ListVMsCmd.class.getName()); diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 6473dc606ed..3123f6e60f3 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -3786,6 +3786,8 @@ public class ManagementServerImpl implements ManagementServer { Object vmIdObj = c.getCriteria(Criteria.INSTANCEID); Object id = c.getCriteria(Criteria.ID); Object keyword = c.getCriteria(Criteria.KEYWORD); + Object accountId = c.getCriteria(Criteria.ACCOUNTID); + Object zoneId = c.getCriteria(Criteria.DATACENTERID); if (keyword != null) { SearchCriteria ssc = _offeringsDao.createSearchCriteria(); @@ -3814,10 +3816,40 @@ public class ManagementServerImpl implements ManagementServer { sc.addAnd("useLocalStorage", SearchCriteria.Op.EQ, offering.getUseLocalStorage()); } } - + + //if account doesn't have direct ip addresses and there are no direct Zone wide vlans, return virtual service offerings only + List accountDirectVlans = new ArrayList(); + List zoneDirectVlans = new ArrayList(); + if (accountId != null || zoneId != null) { + if (accountId != null && zoneId != null) { + accountDirectVlans = _vlanDao.listVlansForAccountByType(null, ((Long)accountId).longValue(), VlanType.DirectAttached); + zoneDirectVlans = listZoneWideVlansByType(VlanType.DirectAttached, (Long)zoneId); + } else if (zoneId != null) { + zoneDirectVlans = listZoneWideVlansByType(VlanType.DirectAttached, (Long)zoneId); + } + + if (accountDirectVlans.isEmpty() && zoneDirectVlans.isEmpty()) { + sc.addAnd("guestIpType", SearchCriteria.Op.EQ, GuestIpType.Virtualized); + } + } + + return _offeringsDao.search(sc, searchFilter); } + //lists zone wide vlans by type + private List listZoneWideVlansByType(VlanType type, long zoneId) { + List zoneVlans = _vlanDao.listByZoneAndType(zoneId, type); + List zoneWideVlans = new ArrayList(); + + for (VlanVO vlan : zoneVlans) { + if (_accountVlanMapDao.listAccountVlanMapsByVlan(vlan.getId()).isEmpty()) { + zoneWideVlans.add(vlan); + } + } + return zoneWideVlans; + } + @Override public List searchForClusters(Criteria c) { Filter searchFilter = new Filter(ClusterVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());