Skip to content

Commit a0bdfcf

Browse files
committed
add zone filter
fix secgroup npe Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent cdd6f6e commit a0bdfcf

19 files changed

Lines changed: 137 additions & 58 deletions

File tree

engine/schema/src/main/java/com/cloud/dc/dao/ClusterDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ public interface ClusterDao extends GenericDao<ClusterVO, Long> {
6363

6464
List<Long> listEnabledClusterIdsByZoneHypervisorArch(Long zoneId, HypervisorType hypervisorType, CPU.CPUArch arch);
6565

66-
List<ClusterVO> listByHypervisorType(HypervisorType hypervisorType, Filter filter);
66+
List<ClusterVO> listByZonesAndHypervisorType(List<Long> zoneIds, HypervisorType hypervisorType, Filter filter);
6767
}

engine/schema/src/main/java/com/cloud/dc/dao/ClusterDaoImpl.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
2222
import java.util.ArrayList;
23+
import java.util.Collections;
2324
import java.util.HashMap;
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.stream.Collectors;
2728

2829
import javax.inject.Inject;
2930

31+
import org.apache.commons.collections.CollectionUtils;
3032
import org.springframework.stereotype.Component;
3133

3234
import com.cloud.cpu.CPU;
@@ -416,8 +418,16 @@ public List<Long> listEnabledClusterIdsByZoneHypervisorArch(Long zoneId, Hypervi
416418
}
417419

418420
@Override
419-
public List<ClusterVO> listByHypervisorType(HypervisorType hypervisorType, Filter filter) {
420-
SearchCriteria<ClusterVO> sc = ZoneHyTypeSearch.create();
421+
public List<ClusterVO> listByZonesAndHypervisorType(List<Long> zoneIds, HypervisorType hypervisorType, Filter filter) {
422+
if (CollectionUtils.isEmpty(zoneIds)) {
423+
return Collections.emptyList();
424+
}
425+
SearchBuilder<ClusterVO> sb = createSearchBuilder();
426+
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.IN);
427+
sb.and("hypervisorType", sb.entity().getHypervisorType(), SearchCriteria.Op.EQ);
428+
sb.done();
429+
SearchCriteria<ClusterVO> sc = sb.create();
430+
sc.setParameters("dataCenterId", zoneIds.toArray());
421431
sc.setParameters("hypervisorType", hypervisorType.toString());
422432
return listBy(sc, filter);
423433
}

engine/schema/src/main/java/com/cloud/network/dao/NetworkDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ public interface NetworkDao extends GenericDao<NetworkVO, Long>, StateDao<State,
101101

102102
List<NetworkVO> listByZoneAndTrafficType(long zoneId, TrafficType trafficType);
103103

104-
List<NetworkVO> listByTrafficTypeAndOwners(final TrafficType trafficType, List<Long> accountIds,
105-
List<Long> domainIds, Filter filter);
104+
List<NetworkVO> listByZonesTrafficTypeAndOwners(List<Long> zoneIds, final TrafficType trafficType,
105+
List<Long> accountIds, List<Long> domainIds, Filter filter);
106106

107107
void setCheckForGc(long networkId);
108108

engine/schema/src/main/java/com/cloud/network/dao/NetworkDaoImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.net.URI;
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
22+
import java.util.Collections;
2223
import java.util.HashSet;
2324
import java.util.List;
2425
import java.util.Map;
@@ -647,9 +648,13 @@ public List<NetworkVO> listByZoneAndTrafficType(final long zoneId, final Traffic
647648
}
648649

649650
@Override
650-
public List<NetworkVO> listByTrafficTypeAndOwners(final TrafficType trafficType, List<Long> accountIds,
651-
List<Long> domainIds, Filter filter) {
651+
public List<NetworkVO> listByZonesTrafficTypeAndOwners(List<Long> zoneIds, final TrafficType trafficType,
652+
List<Long> accountIds, List<Long> domainIds, Filter filter) {
653+
if (CollectionUtils.isEmpty(zoneIds)) {
654+
return Collections.emptyList();
655+
}
652656
SearchBuilder<NetworkVO> sb = createSearchBuilder();
657+
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.IN);
653658
sb.and("trafficType", sb.entity().getTrafficType(), Op.EQ);
654659
boolean accountIdsNotEmpty = CollectionUtils.isNotEmpty(accountIds);
655660
boolean domainIdsNotEmpty = CollectionUtils.isNotEmpty(domainIds);
@@ -660,6 +665,7 @@ public List<NetworkVO> listByTrafficTypeAndOwners(final TrafficType trafficType,
660665
}
661666
sb.done();
662667
final SearchCriteria<NetworkVO> sc = sb.create();
668+
sc.setParameters("dataCenterId", zoneIds.toArray());
663669
sc.setParameters("trafficType", trafficType);
664670
if (accountIdsNotEmpty) {
665671
sc.setParameters("account", accountIds.toArray());

engine/schema/src/main/java/org/apache/cloudstack/backup/dao/ImageTransferDao.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ public interface ImageTransferDao extends GenericDao<ImageTransferVO, Long> {
3131
ImageTransferVO findByVolume(Long volumeId);
3232
ImageTransferVO findUnfinishedByVolume(Long volumeId);
3333
List<ImageTransferVO> listByPhaseAndDirection(ImageTransfer.Phase phase, ImageTransfer.Direction direction);
34-
List<ImageTransferVO> listByOwners(List<Long> accountIds, List<Long> domainIds, Filter filter);
34+
List<ImageTransferVO> listByZonesAndOwners(List<Long> zoneIds, List<Long> accountIds, List<Long> domainIds,
35+
Filter filter);
3536
}

engine/schema/src/main/java/org/apache/cloudstack/backup/dao/ImageTransferDaoImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.cloudstack.backup.dao;
1919

20+
import java.util.Collections;
2021
import java.util.List;
2122

2223
import javax.annotation.PostConstruct;
@@ -106,8 +107,13 @@ public List<ImageTransferVO> listByPhaseAndDirection(ImageTransfer.Phase phase,
106107
}
107108

108109
@Override
109-
public List<ImageTransferVO> listByOwners(List<Long> accountIds, List<Long> domainIds, Filter filter) {
110+
public List<ImageTransferVO> listByZonesAndOwners(List<Long> zoneIds, List<Long> accountIds, List<Long> domainIds,
111+
Filter filter) {
112+
if (CollectionUtils.isEmpty(zoneIds)) {
113+
return Collections.emptyList();
114+
}
110115
SearchBuilder<ImageTransferVO> sb = createSearchBuilder();
116+
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.IN);
111117
boolean accountIdsNotEmpty = CollectionUtils.isNotEmpty(accountIds);
112118
boolean domainIdsNotEmpty = CollectionUtils.isNotEmpty(domainIds);
113119
if (accountIdsNotEmpty || domainIdsNotEmpty) {
@@ -117,6 +123,7 @@ public List<ImageTransferVO> listByOwners(List<Long> accountIds, List<Long> doma
117123
}
118124
sb.done();
119125
final SearchCriteria<ImageTransferVO> sc = sb.create();
126+
sc.setParameters("dataCenterId", zoneIds.toArray());
120127
if (accountIdsNotEmpty) {
121128
sc.setParameters("account", accountIds.toArray());
122129
}

plugins/integrations/veeam-control-service/src/main/java/org/apache/cloudstack/veeam/adapter/ServerAdapter.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,8 @@ public Pair<User, Account> getServiceAccount() {
10871087
@ApiAccess(command = ListZonesCmd.class)
10881088
public List<DataCenter> listAllDataCenters(Long offset, Long limit) {
10891089
Filter filter = new Filter(DataCenterJoinVO.class, "id", true, offset, limit);
1090-
final List<DataCenterJoinVO> clusters = dataCenterJoinDao.listAll(filter);
1090+
final List<DataCenterJoinVO> clusters = dataCenterJoinDao.listByIds(
1091+
kvmBackupExportService.listCompatibleDataCenterIds(), filter);
10911092
return DataCenterJoinVOToDataCenterConverter.toDCList(clusters);
10921093
}
10931094

@@ -1126,7 +1127,8 @@ public List<Network> listNetworksByDcId(final String uuid, final Long offset, fi
11261127
@ApiAccess(command = ListClustersCmd.class)
11271128
public List<Cluster> listAllClusters(Long offset, Long limit) {
11281129
Filter filter = new Filter(ClusterVO.class, "id", true, offset, limit);
1129-
final List<ClusterVO> clusters = clusterDao.listByHypervisorType(Hypervisor.HypervisorType.KVM, filter);
1130+
final List<ClusterVO> clusters = clusterDao.listByZonesAndHypervisorType(
1131+
kvmBackupExportService.listCompatibleDataCenterIds(), Hypervisor.HypervisorType.KVM, filter);
11301132
return ClusterVOToClusterConverter.toClusterList(clusters, this::getZoneById);
11311133
}
11321134

@@ -1142,7 +1144,8 @@ public Cluster getCluster(String uuid) {
11421144
@ApiAccess(command = ListHostsCmd.class)
11431145
public List<Host> listAllHosts(Long offset, Long limit) {
11441146
Filter filter = new Filter(HostJoinVO.class, "id", true, offset, limit);
1145-
final List<HostJoinVO> hosts = hostJoinDao.listAvailableRoutingHostsByHypervisor(Hypervisor.HypervisorType.KVM, filter);
1147+
final List<HostJoinVO> hosts = hostJoinDao.listAvailableRoutingByZonesAndHypervisor(
1148+
kvmBackupExportService.listCompatibleDataCenterIds(), Hypervisor.HypervisorType.KVM, filter);
11461149
return HostJoinVOToHostConverter.toHostList(hosts);
11471150
}
11481151

@@ -1159,7 +1162,8 @@ public Host getHost(String uuid) {
11591162
public List<Network> listAllNetworks(Long offset, Long limit) {
11601163
Filter filter = new Filter(NetworkVO.class, "id", true, offset, limit);
11611164
Pair<List<Long>, List<Long>> ownerDetails = getResourceOwnerFiltersWithDomainIds();
1162-
final List<NetworkVO> networks = networkDao.listByTrafficTypeAndOwners(Networks.TrafficType.Guest,
1165+
final List<NetworkVO> networks = networkDao.listByZonesTrafficTypeAndOwners(
1166+
kvmBackupExportService.listCompatibleDataCenterIds(), Networks.TrafficType.Guest,
11631167
ownerDetails.first(), ownerDetails.second(), filter);
11641168
return NetworkVOToNetworkConverter.toNetworkList(networks, this::getZoneById);
11651169
}
@@ -1178,7 +1182,8 @@ public Network getNetwork(String uuid) {
11781182
public List<VnicProfile> listAllVnicProfiles(Long offset, Long limit) {
11791183
Filter filter = new Filter(NetworkVO.class, "id", true, offset, limit);
11801184
Pair<List<Long>, List<Long>> ownerDetails = getResourceOwnerFiltersWithDomainIds();
1181-
final List<NetworkVO> networks = networkDao.listByTrafficTypeAndOwners(Networks.TrafficType.Guest,
1185+
final List<NetworkVO> networks = networkDao.listByZonesTrafficTypeAndOwners(
1186+
kvmBackupExportService.listCompatibleDataCenterIds(), Networks.TrafficType.Guest,
11821187
ownerDetails.first(), ownerDetails.second(), filter);
11831188
return NetworkVOToVnicProfileConverter.toVnicProfileList(networks, this::getZoneById);
11841189
}
@@ -1197,7 +1202,8 @@ public List<Vm> listAllInstances(boolean includeTags, boolean includeDisks, bool
11971202
boolean allContent, Long offset, Long limit) {
11981203
Filter filter = new Filter(UserVmJoinVO.class, "id", true, offset, limit);
11991204
Pair<List<Long>, String> ownerDetails = getResourceOwnerFilters();
1200-
List<UserVmJoinVO> vms = userVmJoinDao.listByHypervisorNotTypesAndOwners(Hypervisor.HypervisorType.KVM,
1205+
List<UserVmJoinVO> vms = userVmJoinDao.listByZonesHypervisorNotTypesAndOwners(
1206+
kvmBackupExportService.listCompatibleDataCenterIds(), Hypervisor.HypervisorType.KVM,
12011207
Arrays.asList(UserVmManager.CKS_NODE), ownerDetails.first(), ownerDetails.second(), filter);
12021208
return UserVmJoinVOToVmConverter.toVmList(vms,
12031209
this::getHostById,
@@ -1442,7 +1448,8 @@ public List<DiskAttachment> listDiskAttachmentsByInstanceUuid(final String uuid)
14421448
public List<Disk> listAllDisks(Long offset, Long limit) {
14431449
Filter filter = new Filter(VolumeJoinVO.class, "id", true, offset, limit);
14441450
Pair<List<Long>, String> ownerDetails = getResourceOwnerFilters();
1445-
List<VolumeJoinVO> kvmVolumes = volumeJoinDao.listByHypervisorTypeAndOwners(Hypervisor.HypervisorType.KVM,
1451+
List<VolumeJoinVO> kvmVolumes = volumeJoinDao.listByZonesHypervisorTypeAndOwners(
1452+
kvmBackupExportService.listCompatibleDataCenterIds(), Hypervisor.HypervisorType.KVM,
14461453
ownerDetails.first(), ownerDetails.second(), filter);
14471454
return VolumeJoinVOToDiskConverter.toDiskList(kvmVolumes, this::getVolumePhysicalSize);
14481455
}
@@ -1672,7 +1679,8 @@ public Nic attachInstanceNic(final String vmUuid, final Nic request) {
16721679
public List<ImageTransfer> listAllImageTransfers(Long offset, Long limit) {
16731680
Filter filter = new Filter(ImageTransferVO.class, "id", true, offset, limit);
16741681
Pair<List<Long>, List<Long>> ownerDetails = getResourceOwnerFiltersWithDomainIds();
1675-
List<ImageTransferVO> imageTransfers = imageTransferDao.listByOwners(ownerDetails.first(),
1682+
List<ImageTransferVO> imageTransfers = imageTransferDao.listByZonesAndOwners(
1683+
kvmBackupExportService.listCompatibleDataCenterIds(), ownerDetails.first(),
16761684
ownerDetails.second(), filter);
16771685
return ImageTransferVOToImageTransferConverter.toImageTransferList(imageTransfers, this::getHostById, this::getVolumeById);
16781686
}

plugins/integrations/veeam-control-service/src/main/java/org/apache/cloudstack/veeam/api/converter/UserVmJoinVOToVmConverter.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.apache.cloudstack.veeam.api.dto.Tag;
4242
import org.apache.cloudstack.veeam.api.dto.Topology;
4343
import org.apache.cloudstack.veeam.api.dto.Vm;
44+
import org.apache.commons.collections.CollectionUtils;
4445
import org.apache.commons.collections.MapUtils;
4546
import org.apache.commons.lang3.StringUtils;
4647

@@ -181,11 +182,7 @@ public static Vm toVm(final UserVmJoinVO src,
181182
dst.setGuestOsName(src.getGuestOsDisplayName());
182183
dst.setInstanceType(src.getUserVmType());
183184
updateSharedFSDetailsIfNeeded(src, sharedFsResolver, dst);
184-
if (securityGroupsResolver != null) {
185-
dst.setSecurityGroupIds(securityGroupsResolver.apply(src.getId()).stream()
186-
.map(SecurityGroupVO::getUuid)
187-
.collect(Collectors.toList()));
188-
}
185+
updateSecurityGroupsIfNeeded(src, securityGroupsResolver, dst);
189186

190187
// Keep at end
191188
if (allContent) {
@@ -195,6 +192,19 @@ public static Vm toVm(final UserVmJoinVO src,
195192
return dst;
196193
}
197194

195+
private static void updateSecurityGroupsIfNeeded(UserVmJoinVO src, Function<Long, List<SecurityGroupVO>> securityGroupsResolver, Vm dst) {
196+
if (securityGroupsResolver == null) {
197+
return;
198+
}
199+
List<SecurityGroupVO> securityGroups = securityGroupsResolver.apply(src.getId());
200+
if (CollectionUtils.isEmpty(securityGroups)) {
201+
return;
202+
}
203+
dst.setSecurityGroupIds(securityGroups.stream()
204+
.map(SecurityGroupVO::getUuid)
205+
.collect(Collectors.toList()));
206+
}
207+
198208
private static void updateSharedFSDetailsIfNeeded(UserVmJoinVO src, Function<UserVmJoinVO, SharedFS> sharedFsResolver, Vm dst) {
199209
if (sharedFsResolver == null || dst.getDiskAttachments() == null) {
200210
return;

plugins/integrations/veeam-control-service/src/test/java/org/apache/cloudstack/veeam/adapter/ServerAdapterTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.Map;
4040

4141
import org.apache.cloudstack.backup.BackupVO;
42+
import org.apache.cloudstack.backup.KVMBackupExportService;
4243
import org.apache.cloudstack.backup.dao.BackupDao;
4344
import org.apache.cloudstack.backup.dao.ImageTransferDao;
4445
import org.apache.cloudstack.context.CallContext;
@@ -142,6 +143,8 @@ public class ServerAdapterTest {
142143
@Mock NetworkModel networkModel;
143144
@Mock ProjectManager projectManager;
144145
@Mock DomainDao domainDao;
146+
@Mock
147+
KVMBackupExportService kvmBackupExportService;
145148

146149
@Before
147150
public void setupCallContext() {
@@ -816,7 +819,7 @@ public void testGetDataCenter_Found_ReturnsDataCenter() {
816819
@Test
817820
public void testListAllDataCenters_ReturnsConvertedList() {
818821
DataCenterJoinVO vo = mock(DataCenterJoinVO.class);
819-
when(dataCenterJoinDao.listAll(any())).thenReturn(List.of(vo));
822+
when(dataCenterJoinDao.listByIds(anyList(), any())).thenReturn(List.of(vo));
820823

821824
List<DataCenter> result = serverAdapter.listAllDataCenters(0L, 10L);
822825

@@ -826,7 +829,7 @@ public void testListAllDataCenters_ReturnsConvertedList() {
826829

827830
@Test
828831
public void testListAllDataCenters_EmptyList_ReturnsEmpty() {
829-
when(dataCenterJoinDao.listAll(any())).thenReturn(Collections.emptyList());
832+
when(dataCenterJoinDao.listByIds(anyList(), any())).thenReturn(Collections.emptyList());
830833

831834
assertTrue(serverAdapter.listAllDataCenters(0L, 10L).isEmpty());
832835
}
@@ -869,7 +872,7 @@ public void testListNetworksByDcId_Found_ReturnsEmptyListWhenNoNetworks() {
869872

870873
@Test
871874
public void testListAllClusters_ReturnsEmptyListWhenNoClusters() {
872-
when(clusterDao.listByHypervisorType(any(), any())).thenReturn(Collections.emptyList());
875+
when(clusterDao.listByZonesAndHypervisorType(anyList(), any(), any())).thenReturn(Collections.emptyList());
873876
assertTrue(serverAdapter.listAllClusters(0L, 10L).isEmpty());
874877
}
875878

@@ -884,7 +887,7 @@ public void testGetCluster_NotFound_Throws() {
884887
@Test
885888
public void testListAllHosts_ReturnsList() {
886889
HostJoinVO hostVO = mock(HostJoinVO.class);
887-
when(hostJoinDao.listAvailableRoutingHostsByHypervisor(any(), any())).thenReturn(List.of(hostVO));
890+
when(hostJoinDao.listAvailableRoutingByZonesAndHypervisor(anyList(), any(), any())).thenReturn(List.of(hostVO));
888891

889892
assertEquals(1, serverAdapter.listAllHosts(0L, 10L).size());
890893
}

server/src/main/java/com/cloud/api/query/dao/DataCenterJoinDao.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
// under the License.
1717
package com.cloud.api.query.dao;
1818

19+
import java.util.List;
20+
1921
import org.apache.cloudstack.api.ResponseObject.ResponseView;
2022
import org.apache.cloudstack.api.response.ZoneResponse;
2123

2224
import com.cloud.api.query.vo.DataCenterJoinVO;
2325
import com.cloud.dc.DataCenter;
26+
import com.cloud.utils.db.Filter;
2427
import com.cloud.utils.db.GenericDao;
2528

2629
public interface DataCenterJoinDao extends GenericDao<DataCenterJoinVO, Long> {
@@ -30,4 +33,6 @@ public interface DataCenterJoinDao extends GenericDao<DataCenterJoinVO, Long> {
3033
ZoneResponse newDataCenterResponse(ResponseView view, DataCenterJoinVO dof, Boolean showCapacities, Boolean showResourceImage);
3134

3235
DataCenterJoinVO newDataCenterView(DataCenter dof);
36+
37+
List<DataCenterJoinVO> listByIds(List<Long> ids, Filter filter);
3338
}

0 commit comments

Comments
 (0)