Skip to content

Commit ac9084b

Browse files
shwstppryadvr
authored andcommitted
server: filter child domains for disk offering create, update
This change enables filtering child domains while creating, updating disk offerings when both parent and child domains are passed. Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 2bed4ac commit ac9084b

1 file changed

Lines changed: 53 additions & 9 deletions

File tree

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,27 @@ protected DiskOfferingVO createDiskOffering(final Long userId, final List<Long>
26222622
}
26232623
}
26242624

2625+
// Filter child domains when both parent and child domains are present
2626+
List<Long> filteredDomainIds = new ArrayList<>();
2627+
if (domainIds != null) {
2628+
filteredDomainIds.addAll(domainIds);
2629+
}
2630+
if (filteredDomainIds.size() > 1) {
2631+
for (int i = filteredDomainIds.size() - 1; i >= 1; i--) {
2632+
long first = filteredDomainIds.get(i);
2633+
for (int j = i - 1; j >= 0; j--) {
2634+
long second = filteredDomainIds.get(j);
2635+
if (_domainDao.isChildDomain(filteredDomainIds.get(i), filteredDomainIds.get(j))) {
2636+
filteredDomainIds.remove(j);
2637+
i--;
2638+
}
2639+
if (_domainDao.isChildDomain(filteredDomainIds.get(j), filteredDomainIds.get(i))) {
2640+
filteredDomainIds.remove(i);
2641+
break;
2642+
}
2643+
}
2644+
}
2645+
}
26252646

26262647
// Check if user exists in the system
26272648
final User user = _userDao.findById(userId);
@@ -2630,13 +2651,13 @@ protected DiskOfferingVO createDiskOffering(final Long userId, final List<Long>
26302651
}
26312652
final Account account = _accountDao.findById(user.getAccountId());
26322653
if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
2633-
if (domainIds == null || domainIds.isEmpty()) {
2654+
if (filteredDomainIds.isEmpty()) {
26342655
throw new InvalidParameterValueException("Unable to create public disk offering by id " + userId + " because it is domain-admin");
26352656
}
26362657
if (tags != null) {
26372658
throw new InvalidParameterValueException("Unable to create disk offering with storage tags by id " + userId + " because it is domain-admin");
26382659
}
2639-
for (Long domainId : domainIds) {
2660+
for (Long domainId : filteredDomainIds) {
26402661
if (domainId == null || !_domainDao.isChildDomain(account.getDomainId(), domainId)) {
26412662
throw new InvalidParameterValueException("Unable to create disk offering by another domain admin with id " + userId);
26422663
}
@@ -2704,9 +2725,9 @@ protected DiskOfferingVO createDiskOffering(final Long userId, final List<Long>
27042725
CallContext.current().setEventDetails("Disk offering id=" + newDiskOffering.getId());
27052726
final DiskOfferingVO offering = _diskOfferingDao.persist(newDiskOffering);
27062727
if (offering != null) {
2707-
if(domainIds!=null && !domainIds.isEmpty()) {
2728+
if(!filteredDomainIds.isEmpty()) {
27082729
List<String> domainIdsStringList = new ArrayList<>();
2709-
for(Long domainId : domainIds)
2730+
for(Long domainId : filteredDomainIds)
27102731
domainIdsStringList.add(String.valueOf(domainId));
27112732
diskOfferingDetailsDao.addDetail(offering.getId(), ApiConstants.DOMAIN_ID_LIST, String.join(",", domainIdsStringList), true);
27122733
}
@@ -2817,8 +2838,31 @@ public DiskOffering updateDiskOffering(final UpdateDiskOfferingCmd cmd) {
28172838
}
28182839
final Account account = _accountDao.findById(user.getAccountId());
28192840
final List<Long> domainIds = cmd.getDomainIds();
2841+
2842+
// Filter child domains when both parent and child domains are present
2843+
List<Long> filteredDomainIds = new ArrayList<>();
2844+
if (domainIds != null) {
2845+
filteredDomainIds.addAll(domainIds);
2846+
}
2847+
if (filteredDomainIds.size() > 1) {
2848+
for (int i = filteredDomainIds.size() - 1; i >= 1; i--) {
2849+
long first = filteredDomainIds.get(i);
2850+
for (int j = i - 1; j >= 0; j--) {
2851+
long second = filteredDomainIds.get(j);
2852+
if (_domainDao.isChildDomain(filteredDomainIds.get(i), filteredDomainIds.get(j))) {
2853+
filteredDomainIds.remove(j);
2854+
i--;
2855+
}
2856+
if (_domainDao.isChildDomain(filteredDomainIds.get(j), filteredDomainIds.get(i))) {
2857+
filteredDomainIds.remove(i);
2858+
break;
2859+
}
2860+
}
2861+
}
2862+
}
2863+
28202864
if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
2821-
if (domainIds == null) {
2865+
if (filteredDomainIds.isEmpty()) {
28222866
if (existingDomainIds.isEmpty()) {
28232867
throw new InvalidParameterValueException("Unable to update public disk offering by id " + userId + " because it is domain-admin");
28242868
}
@@ -2828,7 +2872,7 @@ public DiskOffering updateDiskOffering(final UpdateDiskOfferingCmd cmd) {
28282872
}
28292873
}
28302874
} else {
2831-
for (Long domainId : domainIds) {
2875+
for (Long domainId : filteredDomainIds) {
28322876
if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
28332877
throw new InvalidParameterValueException("Unable to update disk offering by another domain admin with id " + userId);
28342878
}
@@ -2847,7 +2891,7 @@ public DiskOffering updateDiskOffering(final UpdateDiskOfferingCmd cmd) {
28472891
}
28482892

28492893
final boolean updateNeeded = name != null || displayText != null || sortKey != null || displayDiskOffering != null;
2850-
final boolean detailsUpdateNeeded = (domainIds != null && !domainIds.isEmpty()) || (zoneIds != null && !zoneIds.isEmpty());
2894+
final boolean detailsUpdateNeeded = (!filteredDomainIds.isEmpty()) || (zoneIds != null && !zoneIds.isEmpty());
28512895
if (!updateNeeded && !detailsUpdateNeeded) {
28522896
return _diskOfferingDao.findById(diskOfferingId);
28532897
}
@@ -2896,9 +2940,9 @@ public DiskOffering updateDiskOffering(final UpdateDiskOfferingCmd cmd) {
28962940
// }
28972941

28982942
if (!updateNeeded || _diskOfferingDao.update(diskOfferingId, diskOffering)) {
2899-
if(domainIds!=null && !domainIds.isEmpty()) {
2943+
if(!filteredDomainIds.isEmpty()) {
29002944
List<String> domainIdsStringList = new ArrayList<>();
2901-
for(Long domainId : domainIds)
2945+
for(Long domainId : filteredDomainIds)
29022946
domainIdsStringList.add(String.valueOf(domainId));
29032947
diskOfferingDetailsDao.addDetail(diskOfferingId, ApiConstants.DOMAIN_ID_LIST, String.join(",", domainIdsStringList), true);
29042948
}

0 commit comments

Comments
 (0)