Skip to content

Commit 100a5c5

Browse files
committed
filter storagedomains for hypervisor
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 1a37925 commit 100a5c5

5 files changed

Lines changed: 134 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,8 @@ public List<StorageDomain> listStorageDomainsByDcId(final String uuid, final Lon
791791
throw new InvalidParameterValueException("DataCenter with ID " + uuid + " not found");
792792
}
793793
Filter filter = new Filter(StoragePoolJoinVO.class, "id", true, offset, limit);
794-
List<StoragePoolJoinVO> storagePoolVOS = storagePoolJoinDao.listByZoneAndType(dataCenterVO.getId(),
795-
SUPPORTED_STORAGE_TYPES, filter);
794+
List<StoragePoolJoinVO> storagePoolVOS = storagePoolJoinDao.listByZoneHypervisorAndType(dataCenterVO.getId(),
795+
Hypervisor.HypervisorType.KVM, SUPPORTED_STORAGE_TYPES, filter);
796796
return StoreVOToStorageDomainConverter.toStorageDomainListFromPools(storagePoolVOS);
797797
}
798798

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import com.cloud.domain.dao.DomainDao;
7777
import com.cloud.exception.InvalidParameterValueException;
7878
import com.cloud.exception.PermissionDeniedException;
79+
import com.cloud.hypervisor.Hypervisor;
7980
import com.cloud.network.NetworkModel;
8081
import com.cloud.network.Networks;
8182
import com.cloud.network.dao.NetworkDao;
@@ -676,7 +677,7 @@ public void testListStorageDomainsByDcId_Found_ReturnsList() {
676677
when(dcVO.getId()).thenReturn(1L);
677678
when(dataCenterDao.findByUuid("dc-uuid")).thenReturn(dcVO);
678679
StoragePoolJoinVO poolVO = mock(StoragePoolJoinVO.class);
679-
when(storagePoolJoinDao.listByZoneAndType(eq(1L), any(), any())).thenReturn(List.of(poolVO));
680+
when(storagePoolJoinDao.listByZoneHypervisorAndType(eq(1L), eq(Hypervisor.HypervisorType.KVM), any(), any())).thenReturn(List.of(poolVO));
680681

681682
assertNotNull(serverAdapter.listStorageDomainsByDcId("dc-uuid", 0L, 10L));
682683
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020

21+
import com.cloud.hypervisor.Hypervisor;
2122
import com.cloud.storage.ScopeType;
2223
import org.apache.cloudstack.api.response.StoragePoolResponse;
2324

@@ -46,6 +47,6 @@ public interface StoragePoolJoinDao extends GenericDao<StoragePoolJoinVO, Long>
4647

4748
List<StoragePoolVO> findStoragePoolByScopeAndRuleTags(Long datacenterId, Long podId, Long clusterId, ScopeType scopeType, List<String> tags);
4849

49-
List<StoragePoolJoinVO> listByZoneAndType(long zoneId, List<Storage.StoragePoolType> types, Filter filter);
50+
List<StoragePoolJoinVO> listByZoneHypervisorAndType(long zoneId, Hypervisor.HypervisorType hypervisorType, List<Storage.StoragePoolType> types, Filter filter);
5051

5152
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.cloud.api.ApiDBUtils;
4242
import com.cloud.api.query.vo.StoragePoolJoinVO;
4343
import com.cloud.capacity.CapacityManager;
44+
import com.cloud.hypervisor.Hypervisor;
4445
import com.cloud.server.ResourceTag;
4546
import com.cloud.storage.DataStoreRole;
4647
import com.cloud.storage.ScopeType;
@@ -412,14 +413,18 @@ public List<StoragePoolVO> findStoragePoolByScopeAndRuleTags(Long datacenterId,
412413
return filteredPools;
413414
}
414415

415-
@Override
416-
public List<StoragePoolJoinVO> listByZoneAndType(long zoneId, List<Storage.StoragePoolType> types, Filter filter) {
416+
public List<StoragePoolJoinVO> listByZoneHypervisorAndType(long zoneId, Hypervisor.HypervisorType hypervisorType, List<Storage.StoragePoolType> types, Filter filter) {
417417
SearchBuilder<StoragePoolJoinVO> sb = createSearchBuilder();
418418
sb.and("zoneId", sb.entity().getZoneId(), SearchCriteria.Op.EQ);
419+
sb.and("hypervisors", sb.entity().getHypervisor(), SearchCriteria.Op.IN);
419420
sb.and("types", sb.entity().getPoolType(), SearchCriteria.Op.IN);
420421
sb.done();
421422
SearchCriteria<StoragePoolJoinVO> sc = sb.create();
422423
sc.setParameters("zoneId", zoneId);
424+
List<Hypervisor.HypervisorType> hypervisors = new ArrayList<>();
425+
hypervisors.add(Hypervisor.HypervisorType.Any);
426+
hypervisors.add(hypervisorType);
427+
sc.setParameters("hypervisors", hypervisors.toArray());
423428
if (CollectionUtils.isNotEmpty(types)) {
424429
sc.setParameters("types", types.toArray());
425430
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.api.query.dao;
18+
19+
import static org.junit.Assert.assertSame;
20+
import static org.mockito.AdditionalMatchers.aryEq;
21+
import static org.mockito.ArgumentMatchers.any;
22+
import static org.mockito.ArgumentMatchers.eq;
23+
import static org.mockito.Mockito.doReturn;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.never;
26+
import static org.mockito.Mockito.verify;
27+
import static org.mockito.Mockito.when;
28+
29+
import java.util.Arrays;
30+
import java.util.Collections;
31+
import java.util.List;
32+
33+
import org.junit.Before;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.mockito.InjectMocks;
37+
import org.mockito.Mock;
38+
import org.mockito.Spy;
39+
import org.mockito.junit.MockitoJUnitRunner;
40+
41+
import com.cloud.api.query.vo.StoragePoolJoinVO;
42+
import com.cloud.hypervisor.Hypervisor;
43+
import com.cloud.storage.Storage;
44+
import com.cloud.utils.db.Filter;
45+
import com.cloud.utils.db.SearchBuilder;
46+
import com.cloud.utils.db.SearchCriteria;
47+
48+
@RunWith(MockitoJUnitRunner.class)
49+
public class StoragePoolJoinDaoImplTest {
50+
51+
@Spy
52+
@InjectMocks
53+
private StoragePoolJoinDaoImpl storagePoolJoinDao = new StoragePoolJoinDaoImpl();
54+
55+
@Mock
56+
private SearchCriteria<StoragePoolJoinVO> searchCriteria;
57+
58+
@Before
59+
@SuppressWarnings("unchecked")
60+
public void setUp() {
61+
StoragePoolJoinVO storagePoolJoinVO = mock(StoragePoolJoinVO.class);
62+
SearchBuilder<StoragePoolJoinVO> searchBuilder = mock(SearchBuilder.class);
63+
when(searchBuilder.entity()).thenReturn(storagePoolJoinVO);
64+
when(searchBuilder.create()).thenReturn(searchCriteria);
65+
doReturn(searchBuilder).when(storagePoolJoinDao).createSearchBuilder();
66+
}
67+
68+
@Test
69+
public void listByZoneHypervisorAndTypeReturnsMatchingPoolsWhenTypesAreProvided() {
70+
long zoneId = 42L;
71+
Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.KVM;
72+
List<Storage.StoragePoolType> types = Arrays.asList(Storage.StoragePoolType.NetworkFilesystem, Storage.StoragePoolType.Filesystem);
73+
Filter filter = mock(Filter.class);
74+
List<StoragePoolJoinVO> expectedPools = Collections.singletonList(mock(StoragePoolJoinVO.class));
75+
76+
doReturn(expectedPools).when(storagePoolJoinDao).listBy(searchCriteria, filter);
77+
78+
List<StoragePoolJoinVO> result = storagePoolJoinDao.listByZoneHypervisorAndType(zoneId, hypervisorType, types, filter);
79+
80+
assertSame(expectedPools, result);
81+
verify(searchCriteria).setParameters("zoneId", zoneId);
82+
verify(searchCriteria).setParameters(eq("hypervisors"), aryEq(new Object[]{Hypervisor.HypervisorType.Any, hypervisorType}));
83+
verify(searchCriteria).setParameters(eq("types"), aryEq(types.toArray()));
84+
verify(storagePoolJoinDao).listBy(searchCriteria, filter);
85+
}
86+
87+
@Test
88+
public void listByZoneHypervisorAndTypeSkipsTypeFilterWhenTypesAreNull() {
89+
long zoneId = 7L;
90+
Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.VMware;
91+
Filter filter = mock(Filter.class);
92+
List<StoragePoolJoinVO> expectedPools = Collections.emptyList();
93+
94+
doReturn(expectedPools).when(storagePoolJoinDao).listBy(searchCriteria, filter);
95+
96+
List<StoragePoolJoinVO> result = storagePoolJoinDao.listByZoneHypervisorAndType(zoneId, hypervisorType, null, filter);
97+
98+
assertSame(expectedPools, result);
99+
verify(searchCriteria).setParameters("zoneId", zoneId);
100+
verify(searchCriteria).setParameters(eq("hypervisors"), aryEq(new Object[]{Hypervisor.HypervisorType.Any, hypervisorType}));
101+
verify(searchCriteria, never()).setParameters(eq("types"), any(Object[].class));
102+
verify(storagePoolJoinDao).listBy(searchCriteria, filter);
103+
}
104+
105+
@Test
106+
public void listByZoneHypervisorAndTypeSkipsTypeFilterForEmptyTypesAndPassesNullFilter() {
107+
long zoneId = 9L;
108+
Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.XenServer;
109+
List<StoragePoolJoinVO> expectedPools = Collections.singletonList(mock(StoragePoolJoinVO.class));
110+
111+
doReturn(expectedPools).when(storagePoolJoinDao).listBy(searchCriteria, null);
112+
113+
List<StoragePoolJoinVO> result = storagePoolJoinDao.listByZoneHypervisorAndType(zoneId, hypervisorType, Collections.emptyList(), null);
114+
115+
assertSame(expectedPools, result);
116+
verify(searchCriteria).setParameters("zoneId", zoneId);
117+
verify(searchCriteria).setParameters(eq("hypervisors"), aryEq(new Object[]{Hypervisor.HypervisorType.Any, hypervisorType}));
118+
verify(searchCriteria, never()).setParameters(eq("types"), any(Object[].class));
119+
verify(storagePoolJoinDao).listBy(searchCriteria, null);
120+
}
121+
}

0 commit comments

Comments
 (0)