|
21 | 21 | import java.util.ArrayList; |
22 | 22 | import java.util.Arrays; |
23 | 23 | import java.util.Date; |
| 24 | +import java.util.EnumSet; |
24 | 25 | import java.util.HashMap; |
25 | 26 | import java.util.HashSet; |
26 | 27 | import java.util.Iterator; |
|
36 | 37 | import javax.inject.Inject; |
37 | 38 | import javax.naming.ConfigurationException; |
38 | 39 |
|
39 | | -import com.cloud.event.ActionEventUtils; |
40 | | -import com.cloud.event.EventTypes; |
41 | | -import com.cloud.utils.Ternary; |
42 | 40 | import org.apache.cloudstack.acl.SecurityChecker.AccessType; |
43 | 41 | import org.apache.cloudstack.api.ApiCommandResourceType; |
44 | 42 | import org.apache.cloudstack.api.response.AccountResponse; |
|
86 | 84 | import com.cloud.domain.Domain; |
87 | 85 | import com.cloud.domain.DomainVO; |
88 | 86 | import com.cloud.domain.dao.DomainDao; |
| 87 | +import com.cloud.event.ActionEventUtils; |
| 88 | +import com.cloud.event.EventTypes; |
89 | 89 | import com.cloud.exception.InvalidParameterValueException; |
90 | 90 | import com.cloud.exception.PermissionDeniedException; |
91 | 91 | import com.cloud.exception.ResourceAllocationException; |
|
118 | 118 | import com.cloud.user.ResourceLimitService; |
119 | 119 | import com.cloud.user.dao.AccountDao; |
120 | 120 | import com.cloud.utils.Pair; |
| 121 | +import com.cloud.utils.Ternary; |
121 | 122 | import com.cloud.utils.component.ManagerBase; |
122 | 123 | import com.cloud.utils.concurrency.NamedThreadFactory; |
123 | 124 | import com.cloud.utils.db.DB; |
@@ -804,42 +805,43 @@ public List<ResourceLimitVO> searchForLimits(Long id, Long accountId, Long domai |
804 | 805 | limits.addAll(foundLimits); |
805 | 806 | } |
806 | 807 | } else { |
807 | | - limits.addAll(foundLimits); |
808 | | - |
809 | | - // see if any limits are missing from the table, and if yes - get it from the config table and add |
810 | | - ResourceType[] resourceTypes = ResourceCount.ResourceType.values(); |
811 | | - if (foundLimits.size() != resourceTypes.length) { |
812 | | - List<String> accountLimitStr = new ArrayList<>(); |
813 | | - List<String> domainLimitStr = new ArrayList<>(); |
814 | | - for (ResourceLimitVO foundLimit : foundLimits) { |
815 | | - if (foundLimit.getAccountId() != null) { |
816 | | - accountLimitStr.add(foundLimit.getType().toString()); |
817 | | - } else { |
818 | | - domainLimitStr.add(foundLimit.getType().toString()); |
819 | | - } |
820 | | - } |
| 808 | + limits = getConsolidatedResourceLimitsForAllResourceTypes(accountId, domainId, foundLimits, isAccount); |
| 809 | + } |
| 810 | + addTaggedResourceLimits(limits, resourceType, isAccount ? ResourceOwnerType.Account : ResourceOwnerType.Domain, isAccount ? accountId : domainId, hostTags, storageTags); |
| 811 | + return limits; |
| 812 | + } |
821 | 813 |
|
822 | | - // get default from config values |
823 | | - if (isAccount) { |
824 | | - if (accountLimitStr.size() < resourceTypes.length) { |
825 | | - for (ResourceType rt : resourceTypes) { |
826 | | - if (!accountLimitStr.contains(rt.toString())) { |
827 | | - limits.add(new ResourceLimitVO(rt, findCorrectResourceLimitForAccount(_accountMgr.getAccount(accountId), rt, null), accountId, ResourceOwnerType.Account)); |
828 | | - } |
829 | | - } |
830 | | - } |
831 | | - } else { |
832 | | - if (domainLimitStr.size() < resourceTypes.length) { |
833 | | - for (ResourceType rt : resourceTypes) { |
834 | | - if (!domainLimitStr.contains(rt.toString())) { |
835 | | - limits.add(new ResourceLimitVO(rt, findCorrectResourceLimitForDomain(_domainDao.findById(domainId), rt, null), domainId, ResourceOwnerType.Domain)); |
836 | | - } |
837 | | - } |
838 | | - } |
839 | | - } |
| 814 | + private List<ResourceLimitVO> getConsolidatedResourceLimitsForAllResourceTypes(Long accountId, Long domainId, |
| 815 | + List<ResourceLimitVO> foundLimits, boolean isAccount) { |
| 816 | + List<ResourceLimitVO> limits = new ArrayList<>(foundLimits); |
| 817 | + |
| 818 | + Set<ResourceType> allResourceTypes = EnumSet.allOf(ResourceType.class); |
| 819 | + Set<ResourceType> foundUntaggedTypes = foundLimits.stream() |
| 820 | + .filter(l -> StringUtils.isEmpty(l.getTag())) |
| 821 | + .map(ResourceLimitVO::getType) |
| 822 | + .collect(Collectors.toSet()); |
| 823 | + |
| 824 | + if (foundUntaggedTypes.containsAll(allResourceTypes)) { |
| 825 | + return limits; |
| 826 | + } |
| 827 | + |
| 828 | + ResourceOwnerType ownerType = isAccount ? ResourceOwnerType.Account : ResourceOwnerType.Domain; |
| 829 | + long ownerId = isAccount ? accountId : domainId; |
| 830 | + |
| 831 | + for (ResourceType rt : allResourceTypes) { |
| 832 | + if (foundUntaggedTypes.contains(rt)) { |
| 833 | + continue; |
840 | 834 | } |
| 835 | + long max; |
| 836 | + if (isAccount) { |
| 837 | + Account acct = _accountMgr.getAccount(accountId); |
| 838 | + max = findCorrectResourceLimitForAccount(acct, rt, null); |
| 839 | + } else { |
| 840 | + DomainVO dom = _domainDao.findById(domainId); |
| 841 | + max = findCorrectResourceLimitForDomain(dom, rt, null); |
| 842 | + } |
| 843 | + limits.add(new ResourceLimitVO(rt, max, ownerId, ownerType)); |
841 | 844 | } |
842 | | - addTaggedResourceLimits(limits, resourceType, isAccount ? ResourceOwnerType.Account : ResourceOwnerType.Domain, isAccount ? accountId : domainId, hostTags, storageTags); |
843 | 845 | return limits; |
844 | 846 | } |
845 | 847 |
|
|
0 commit comments