Skip to content

Commit 8b4f50e

Browse files
committed
prevent restricting svc offering to domains if any vms are there outside of mentioned domains
1 parent 7fc59b9 commit 8b4f50e

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,6 @@ List<VMInstanceVO> searchRemovedByRemoveDate(final Date startDate, final Date en
188188
Map<String, Long> getNameIdMapForVmIds(Collection<Long> ids);
189189

190190
int getVmCountByOfferingId(Long serviceOfferingId);
191+
192+
int getVmCountByOfferingNotInDomain(Long serviceOfferingId, List<Long> domainIds);
191193
}

engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
105105
protected SearchBuilder<VMInstanceVO> VmsNotInClusterUsingPool;
106106
protected SearchBuilder<VMInstanceVO> IdsPowerStateSelectSearch;
107107
GenericSearchBuilder<VMInstanceVO, Integer> CountByOfferingId;
108+
GenericSearchBuilder<VMInstanceVO, Integer> CountUserVmNotInDomain;
108109

109110
@Inject
110111
ResourceTagDao tagsDao;
@@ -350,6 +351,13 @@ protected void init() {
350351
CountByOfferingId.select(null, Func.COUNT, CountByOfferingId.entity().getId());
351352
CountByOfferingId.and("serviceOfferingId", CountByOfferingId.entity().getServiceOfferingId(), Op.EQ);
352353
CountByOfferingId.done();
354+
355+
CountUserVmNotInDomain = createSearchBuilder(Integer.class);
356+
CountUserVmNotInDomain.select(null, Func.COUNT, CountUserVmNotInDomain.entity().getId());
357+
CountUserVmNotInDomain.and("serviceOfferingId", CountUserVmNotInDomain.entity().getServiceOfferingId(), Op.EQ);
358+
CountUserVmNotInDomain.and("domainIdsNotIn", CountUserVmNotInDomain.entity().getDomainId(), Op.NIN);
359+
CountUserVmNotInDomain.done();
360+
353361
}
354362

355363
@Override
@@ -1241,4 +1249,16 @@ public int getVmCountByOfferingId(Long serviceOfferingId) {
12411249
List<Integer> count = customSearch(sc, null);
12421250
return count.get(0);
12431251
}
1252+
1253+
@Override
1254+
public int getVmCountByOfferingNotInDomain(Long serviceOfferingId, List<Long> domainIds) {
1255+
if (serviceOfferingId == null || CollectionUtils.isEmpty(domainIds)) {
1256+
return 0;
1257+
}
1258+
SearchCriteria<Integer> sc = CountUserVmNotInDomain.create();
1259+
sc.setParameters("serviceOfferingId", serviceOfferingId);
1260+
sc.setParameters("domainIdsNotIn", domainIds.toArray());
1261+
List<Integer> count = customSearch(sc, null);
1262+
return count.get(0);
1263+
}
12441264
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import javax.naming.ConfigurationException;
5252

5353

54+
import com.cloud.exception.UnsupportedServiceException;
5455
import com.cloud.network.as.AutoScaleManager;
5556
import com.cloud.user.AccountManagerImpl;
5657
import org.apache.cloudstack.acl.RoleType;
@@ -3722,6 +3723,12 @@ public ServiceOffering updateServiceOffering(final UpdateServiceOfferingCmd cmd)
37223723
List<Long> filteredDomainIds = filterChildSubDomains(domainIds);
37233724
Collections.sort(filteredDomainIds);
37243725

3726+
// avoid domain update of service offering if any instance is associated to it
3727+
int instanceCount = _vmInstanceDao.getVmCountByOfferingNotInDomain(offeringHandle.getId(), filteredDomainIds);
3728+
if (instanceCount > 0) {
3729+
throw new UnsupportedServiceException("There are Instances associated to this service offering outside of the specified domains.");
3730+
}
3731+
37253732
List<Long> filteredZoneIds = new ArrayList<>();
37263733
if (CollectionUtils.isNotEmpty(zoneIds)) {
37273734
filteredZoneIds.addAll(zoneIds);

0 commit comments

Comments
 (0)