Skip to content

Commit 33cd623

Browse files
committed
server: fix listing resource limits
Fixes #12179 Recently behaviour for updating resource limits for account/domain has changed with adding entries only for those types which don't have a default value. This results in listResourceLimits API returning only a single entry for a resource type. This PR fixes the behaviour. Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 4379666 commit 33cd623

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
2323
import java.util.Date;
24+
import java.util.EnumSet;
2425
import java.util.HashMap;
2526
import java.util.HashSet;
2627
import java.util.Iterator;
@@ -36,9 +37,6 @@
3637
import javax.inject.Inject;
3738
import javax.naming.ConfigurationException;
3839

39-
import com.cloud.event.ActionEventUtils;
40-
import com.cloud.event.EventTypes;
41-
import com.cloud.utils.Ternary;
4240
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
4341
import org.apache.cloudstack.api.ApiCommandResourceType;
4442
import org.apache.cloudstack.api.response.AccountResponse;
@@ -86,6 +84,8 @@
8684
import com.cloud.domain.Domain;
8785
import com.cloud.domain.DomainVO;
8886
import com.cloud.domain.dao.DomainDao;
87+
import com.cloud.event.ActionEventUtils;
88+
import com.cloud.event.EventTypes;
8989
import com.cloud.exception.InvalidParameterValueException;
9090
import com.cloud.exception.PermissionDeniedException;
9191
import com.cloud.exception.ResourceAllocationException;
@@ -118,6 +118,7 @@
118118
import com.cloud.user.ResourceLimitService;
119119
import com.cloud.user.dao.AccountDao;
120120
import com.cloud.utils.Pair;
121+
import com.cloud.utils.Ternary;
121122
import com.cloud.utils.component.ManagerBase;
122123
import com.cloud.utils.concurrency.NamedThreadFactory;
123124
import com.cloud.utils.db.DB;
@@ -804,42 +805,43 @@ public List<ResourceLimitVO> searchForLimits(Long id, Long accountId, Long domai
804805
limits.addAll(foundLimits);
805806
}
806807
} 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+
}
821813

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;
840834
}
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));
841844
}
842-
addTaggedResourceLimits(limits, resourceType, isAccount ? ResourceOwnerType.Account : ResourceOwnerType.Domain, isAccount ? accountId : domainId, hostTags, storageTags);
843845
return limits;
844846
}
845847

0 commit comments

Comments
 (0)