|
23 | 23 | import java.util.HashMap; |
24 | 24 | import java.util.HashSet; |
25 | 25 | import java.util.List; |
26 | | -import java.util.ListIterator; |
27 | 26 | import java.util.Map; |
28 | 27 | import java.util.Set; |
29 | 28 | import java.util.stream.Collectors; |
|
121 | 120 | import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailsDao; |
122 | 121 | import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; |
123 | 122 | import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao; |
124 | | -import org.apache.commons.collections.CollectionUtils; |
125 | 123 | import org.apache.log4j.Logger; |
126 | 124 | import org.springframework.stereotype.Component; |
127 | 125 |
|
|
207 | 205 | import com.cloud.server.TaggedResourceService; |
208 | 206 | import com.cloud.service.ServiceOfferingVO; |
209 | 207 | import com.cloud.service.dao.ServiceOfferingDao; |
| 208 | +import com.cloud.service.dao.ServiceOfferingDetailsDao; |
210 | 209 | import com.cloud.storage.DataStoreRole; |
211 | 210 | import com.cloud.storage.DiskOfferingVO; |
212 | 211 | import com.cloud.storage.ScopeType; |
@@ -350,14 +349,17 @@ public class QueryManagerImpl extends MutualExclusiveIdsManagerBase implements Q |
350 | 349 | private DiskOfferingJoinDao _diskOfferingJoinDao; |
351 | 350 |
|
352 | 351 | @Inject |
353 | | - private DiskOfferingDetailsDao diskOfferingDetailsDao; |
| 352 | + private DiskOfferingDetailsDao _diskOfferingDetailsDao; |
354 | 353 |
|
355 | 354 | @Inject |
356 | 355 | private ServiceOfferingJoinDao _srvOfferingJoinDao; |
357 | 356 |
|
358 | 357 | @Inject |
359 | 358 | private ServiceOfferingDao _srvOfferingDao; |
360 | 359 |
|
| 360 | + @Inject |
| 361 | + private ServiceOfferingDetailsDao _srvOfferingDetailsDao; |
| 362 | + |
361 | 363 | @Inject |
362 | 364 | private DataCenterJoinDao _dcJoinDao; |
363 | 365 |
|
@@ -2868,57 +2870,41 @@ private Pair<List<DiskOfferingJoinVO>, Integer> searchForDiskOfferingsInternal(L |
2868 | 2870 | sc.addAnd("zoneId", SearchCriteria.Op.SC, zoneSC); |
2869 | 2871 | } |
2870 | 2872 |
|
2871 | | - // FIXME: disk offerings should search back up the hierarchy for |
2872 | | - // available disk offerings... |
2873 | | - /* |
2874 | | - * sb.addAnd("domainId", sb.entity().getDomainId(), |
2875 | | - * SearchCriteria.Op.EQ); if (domainId != null) { |
2876 | | - * SearchBuilder<DomainVO> domainSearch = |
2877 | | - * _domainDao.createSearchBuilder(); domainSearch.addAnd("path", |
2878 | | - * domainSearch.entity().getPath(), SearchCriteria.Op.LIKE); |
2879 | | - * sb.join("domainSearch", domainSearch, sb.entity().getDomainId(), |
2880 | | - * domainSearch.entity().getId()); } |
2881 | | - */ |
2882 | | - |
2883 | | - // FIXME: disk offerings should search back up the hierarchy for |
2884 | | - // available disk offerings... |
2885 | | - /* |
2886 | | - * if (domainId != null) { sc.setParameters("domainId", domainId); // |
2887 | | - * //DomainVO domain = _domainDao.findById((Long)domainId); // // I want |
2888 | | - * to join on user_vm.domain_id = domain.id where domain.path like |
2889 | | - * 'foo%' //sc.setJoinParameters("domainSearch", "path", |
2890 | | - * domain.getPath() + "%"); // } |
2891 | | - */ |
| 2873 | + // Filter offerings that are not associated with caller's domain |
| 2874 | + // Fetch the offering ids from the details table since theres no smart way to filter them in the join ... yet! |
| 2875 | + Account caller = CallContext.current().getCallingAccount(); |
| 2876 | + if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) { |
| 2877 | + Domain callerDomain = _domainDao.findById(caller.getDomainId()); |
| 2878 | + List<Long> domainIds = _domainDao.getDomainParentIds(callerDomain.getId()) |
| 2879 | + .stream().collect(Collectors.toList()); |
| 2880 | + if (isRecursive) { |
| 2881 | + List<Long> childrenIds = _domainDao.getDomainChildrenIds(callerDomain.getPath()); |
| 2882 | + if (childrenIds != null && !childrenIds.isEmpty()) |
| 2883 | + domainIds.addAll(childrenIds); |
| 2884 | + } |
2892 | 2885 |
|
2893 | | - Pair<List<DiskOfferingJoinVO>, Integer> result = _diskOfferingJoinDao.searchAndCount(sc, searchFilter); |
2894 | | - // Remove offerings that are not associated with caller's domain |
2895 | | - if (account.getType() != Account.ACCOUNT_TYPE_ADMIN && CollectionUtils.isNotEmpty(result.first())) { |
2896 | | - ListIterator<DiskOfferingJoinVO> it = result.first().listIterator(); |
2897 | | - while (it.hasNext()) { |
2898 | | - DiskOfferingJoinVO offering = it.next(); |
2899 | | - if(!Strings.isNullOrEmpty(offering.getDomainId())) { |
2900 | | - boolean toRemove = true; |
2901 | | - String[] domainIdsArray = offering.getDomainId().split(","); |
2902 | | - for (String domainIdString : domainIdsArray) { |
2903 | | - Long dId = Long.valueOf(domainIdString.trim()); |
2904 | | - if (isRecursive) { |
2905 | | - if (_domainDao.isChildDomain(account.getDomainId(), dId)) { |
2906 | | - toRemove = false; |
2907 | | - break; |
2908 | | - } |
2909 | | - } else { |
2910 | | - if (_domainDao.isChildDomain(dId, account.getDomainId())) { |
2911 | | - toRemove = false; |
2912 | | - break; |
2913 | | - } |
2914 | | - } |
2915 | | - } |
2916 | | - if (toRemove) { |
2917 | | - it.remove(); |
2918 | | - } |
2919 | | - } |
| 2886 | + List<Long> ids = _diskOfferingDetailsDao.findOfferingIdsByDomainIds(domainIds); |
| 2887 | + |
| 2888 | + if (ids == null || ids.isEmpty()) { |
| 2889 | + SearchBuilder<DiskOfferingJoinVO> sb = _diskOfferingJoinDao.createSearchBuilder(); |
| 2890 | + sb.or("domainId", sb.entity().getDomainId(), Op.NULL); |
| 2891 | + sb.done(); |
| 2892 | + |
| 2893 | + SearchCriteria<DiskOfferingJoinVO> scc = sb.create(); |
| 2894 | + sc.addAnd("domainId", SearchCriteria.Op.SC, scc); |
| 2895 | + } else { |
| 2896 | + SearchBuilder<DiskOfferingJoinVO> sb = _diskOfferingJoinDao.createSearchBuilder(); |
| 2897 | + sb.and("id", sb.entity().getId(), Op.IN); |
| 2898 | + sb.or("domainId", sb.entity().getDomainId(), Op.NULL); |
| 2899 | + sb.done(); |
| 2900 | + |
| 2901 | + SearchCriteria<DiskOfferingJoinVO> scc = sb.create(); |
| 2902 | + scc.setParameters("id", ids.toArray()); |
| 2903 | + sc.addAnd("domainId", SearchCriteria.Op.SC, scc); |
2920 | 2904 | } |
2921 | 2905 | } |
| 2906 | + |
| 2907 | + Pair<List<DiskOfferingJoinVO>, Integer> result = _diskOfferingJoinDao.searchAndCount(sc, searchFilter); |
2922 | 2908 | return new Pair<>(result.first(), result.second()); |
2923 | 2909 | } |
2924 | 2910 |
|
@@ -3109,38 +3095,44 @@ private Pair<List<ServiceOfferingJoinVO>, Integer> searchForServiceOfferingsInte |
3109 | 3095 | sc.addAnd("cpuspeedconstraints", SearchCriteria.Op.SC, cpuSpeedSearchCriteria); |
3110 | 3096 | } |
3111 | 3097 |
|
| 3098 | + // Filter offerings that are not associated with caller's domain |
| 3099 | + // Fetch the offering ids from the details table since theres no smart way to filter them in the join ... yet! |
| 3100 | + if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) { |
| 3101 | + Domain callerDomain = _domainDao.findById(caller.getDomainId()); |
| 3102 | + List<Long> domainIds = _domainDao.getDomainParentIds(callerDomain.getId()) |
| 3103 | + .stream().collect(Collectors.toList()); |
| 3104 | + if (isRecursive) { |
| 3105 | + List<Long> childrenIds = _domainDao.getDomainChildrenIds(callerDomain.getPath()); |
| 3106 | + if (childrenIds != null && !childrenIds.isEmpty()) |
| 3107 | + domainIds.addAll(childrenIds); |
| 3108 | + } |
| 3109 | + |
| 3110 | + List<Long> ids = _srvOfferingDetailsDao.findOfferingIdsByDomainIds(domainIds); |
| 3111 | + |
| 3112 | + if (ids == null || ids.isEmpty()) { |
| 3113 | + SearchBuilder<ServiceOfferingJoinVO> sb = _srvOfferingJoinDao.createSearchBuilder(); |
| 3114 | + sb.or("domainId", sb.entity().getDomainId(), Op.NULL); |
| 3115 | + sb.done(); |
| 3116 | + |
| 3117 | + SearchCriteria<ServiceOfferingJoinVO> scc = sb.create(); |
| 3118 | + sc.addAnd("domainId", SearchCriteria.Op.SC, scc); |
| 3119 | + } else { |
| 3120 | + SearchBuilder<ServiceOfferingJoinVO> sb = _srvOfferingJoinDao.createSearchBuilder(); |
| 3121 | + sb.and("id", sb.entity().getId(), Op.IN); |
| 3122 | + sb.or("domainId", sb.entity().getDomainId(), Op.NULL); |
| 3123 | + sb.done(); |
| 3124 | + |
| 3125 | + SearchCriteria<ServiceOfferingJoinVO> scc = sb.create(); |
| 3126 | + scc.setParameters("id", ids.toArray()); |
| 3127 | + sc.addAnd("domainId", SearchCriteria.Op.SC, scc); |
| 3128 | + } |
| 3129 | + } |
| 3130 | + |
3112 | 3131 | Pair<List<ServiceOfferingJoinVO>, Integer> result = _srvOfferingJoinDao.searchAndCount(sc, searchFilter); |
3113 | 3132 |
|
3114 | 3133 | //Couldn't figure out a smart way to filter offerings based on tags in sql so doing it in Java. |
3115 | 3134 | List<ServiceOfferingJoinVO> filteredOfferings = filterOfferingsOnCurrentTags(result.first(), currentVmOffering); |
3116 | | - // Remove offerings that are not associated with caller's domain |
3117 | | - if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN && CollectionUtils.isNotEmpty(filteredOfferings)) { |
3118 | | - ListIterator<ServiceOfferingJoinVO> it = filteredOfferings.listIterator(); |
3119 | | - while (it.hasNext()) { |
3120 | | - ServiceOfferingJoinVO offering = it.next(); |
3121 | | - if(!Strings.isNullOrEmpty(offering.getDomainId())) { |
3122 | | - boolean toRemove = true; |
3123 | | - String[] domainIdsArray = offering.getDomainId().split(","); |
3124 | | - for (String domainIdString : domainIdsArray) { |
3125 | | - Long dId = Long.valueOf(domainIdString.trim()); |
3126 | | - if (isRecursive) { |
3127 | | - if (_domainDao.isChildDomain(caller.getDomainId(), dId)) { |
3128 | | - toRemove = false; |
3129 | | - break; |
3130 | | - } |
3131 | | - } else { |
3132 | | - if (_domainDao.isChildDomain(dId, caller.getDomainId())) { |
3133 | | - toRemove = false; |
3134 | | - break; |
3135 | | - } |
3136 | | - } |
3137 | | - } |
3138 | | - if (toRemove) { |
3139 | | - it.remove(); |
3140 | | - } |
3141 | | - } |
3142 | | - } |
3143 | | - } |
| 3135 | + |
3144 | 3136 | return new Pair<>(filteredOfferings, result.second()); |
3145 | 3137 | } |
3146 | 3138 |
|
|
0 commit comments