mirror of https://github.com/apache/cloudstack.git
filter storagedomains for hypervisor
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
This commit is contained in:
parent
1a379251bc
commit
100a5c5cda
|
|
@ -791,8 +791,8 @@ public class ServerAdapter extends ManagerBase {
|
|||
throw new InvalidParameterValueException("DataCenter with ID " + uuid + " not found");
|
||||
}
|
||||
Filter filter = new Filter(StoragePoolJoinVO.class, "id", true, offset, limit);
|
||||
List<StoragePoolJoinVO> storagePoolVOS = storagePoolJoinDao.listByZoneAndType(dataCenterVO.getId(),
|
||||
SUPPORTED_STORAGE_TYPES, filter);
|
||||
List<StoragePoolJoinVO> storagePoolVOS = storagePoolJoinDao.listByZoneHypervisorAndType(dataCenterVO.getId(),
|
||||
Hypervisor.HypervisorType.KVM, SUPPORTED_STORAGE_TYPES, filter);
|
||||
return StoreVOToStorageDomainConverter.toStorageDomainListFromPools(storagePoolVOS);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -76,6 +76,7 @@ import com.cloud.dc.dao.DataCenterDao;
|
|||
import com.cloud.domain.dao.DomainDao;
|
||||
import com.cloud.exception.InvalidParameterValueException;
|
||||
import com.cloud.exception.PermissionDeniedException;
|
||||
import com.cloud.hypervisor.Hypervisor;
|
||||
import com.cloud.network.NetworkModel;
|
||||
import com.cloud.network.Networks;
|
||||
import com.cloud.network.dao.NetworkDao;
|
||||
|
|
@ -676,7 +677,7 @@ public class ServerAdapterTest {
|
|||
when(dcVO.getId()).thenReturn(1L);
|
||||
when(dataCenterDao.findByUuid("dc-uuid")).thenReturn(dcVO);
|
||||
StoragePoolJoinVO poolVO = mock(StoragePoolJoinVO.class);
|
||||
when(storagePoolJoinDao.listByZoneAndType(eq(1L), any(), any())).thenReturn(List.of(poolVO));
|
||||
when(storagePoolJoinDao.listByZoneHypervisorAndType(eq(1L), eq(Hypervisor.HypervisorType.KVM), any(), any())).thenReturn(List.of(poolVO));
|
||||
|
||||
assertNotNull(serverAdapter.listStorageDomainsByDcId("dc-uuid", 0L, 10L));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package com.cloud.api.query.dao;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import com.cloud.hypervisor.Hypervisor;
|
||||
import com.cloud.storage.ScopeType;
|
||||
import org.apache.cloudstack.api.response.StoragePoolResponse;
|
||||
|
||||
|
|
@ -46,6 +47,6 @@ public interface StoragePoolJoinDao extends GenericDao<StoragePoolJoinVO, Long>
|
|||
|
||||
List<StoragePoolVO> findStoragePoolByScopeAndRuleTags(Long datacenterId, Long podId, Long clusterId, ScopeType scopeType, List<String> tags);
|
||||
|
||||
List<StoragePoolJoinVO> listByZoneAndType(long zoneId, List<Storage.StoragePoolType> types, Filter filter);
|
||||
List<StoragePoolJoinVO> listByZoneHypervisorAndType(long zoneId, Hypervisor.HypervisorType hypervisorType, List<Storage.StoragePoolType> types, Filter filter);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ import org.springframework.stereotype.Component;
|
|||
import com.cloud.api.ApiDBUtils;
|
||||
import com.cloud.api.query.vo.StoragePoolJoinVO;
|
||||
import com.cloud.capacity.CapacityManager;
|
||||
import com.cloud.hypervisor.Hypervisor;
|
||||
import com.cloud.server.ResourceTag;
|
||||
import com.cloud.storage.DataStoreRole;
|
||||
import com.cloud.storage.ScopeType;
|
||||
|
|
@ -412,14 +413,18 @@ public class StoragePoolJoinDaoImpl extends GenericDaoBase<StoragePoolJoinVO, Lo
|
|||
return filteredPools;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StoragePoolJoinVO> listByZoneAndType(long zoneId, List<Storage.StoragePoolType> types, Filter filter) {
|
||||
public List<StoragePoolJoinVO> listByZoneHypervisorAndType(long zoneId, Hypervisor.HypervisorType hypervisorType, List<Storage.StoragePoolType> types, Filter filter) {
|
||||
SearchBuilder<StoragePoolJoinVO> sb = createSearchBuilder();
|
||||
sb.and("zoneId", sb.entity().getZoneId(), SearchCriteria.Op.EQ);
|
||||
sb.and("hypervisors", sb.entity().getHypervisor(), SearchCriteria.Op.IN);
|
||||
sb.and("types", sb.entity().getPoolType(), SearchCriteria.Op.IN);
|
||||
sb.done();
|
||||
SearchCriteria<StoragePoolJoinVO> sc = sb.create();
|
||||
sc.setParameters("zoneId", zoneId);
|
||||
List<Hypervisor.HypervisorType> hypervisors = new ArrayList<>();
|
||||
hypervisors.add(Hypervisor.HypervisorType.Any);
|
||||
hypervisors.add(hypervisorType);
|
||||
sc.setParameters("hypervisors", hypervisors.toArray());
|
||||
if (CollectionUtils.isNotEmpty(types)) {
|
||||
sc.setParameters("types", types.toArray());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,121 @@
|
|||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
package com.cloud.api.query.dao;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.mockito.AdditionalMatchers.aryEq;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.cloud.api.query.vo.StoragePoolJoinVO;
|
||||
import com.cloud.hypervisor.Hypervisor;
|
||||
import com.cloud.storage.Storage;
|
||||
import com.cloud.utils.db.Filter;
|
||||
import com.cloud.utils.db.SearchBuilder;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class StoragePoolJoinDaoImplTest {
|
||||
|
||||
@Spy
|
||||
@InjectMocks
|
||||
private StoragePoolJoinDaoImpl storagePoolJoinDao = new StoragePoolJoinDaoImpl();
|
||||
|
||||
@Mock
|
||||
private SearchCriteria<StoragePoolJoinVO> searchCriteria;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setUp() {
|
||||
StoragePoolJoinVO storagePoolJoinVO = mock(StoragePoolJoinVO.class);
|
||||
SearchBuilder<StoragePoolJoinVO> searchBuilder = mock(SearchBuilder.class);
|
||||
when(searchBuilder.entity()).thenReturn(storagePoolJoinVO);
|
||||
when(searchBuilder.create()).thenReturn(searchCriteria);
|
||||
doReturn(searchBuilder).when(storagePoolJoinDao).createSearchBuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listByZoneHypervisorAndTypeReturnsMatchingPoolsWhenTypesAreProvided() {
|
||||
long zoneId = 42L;
|
||||
Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.KVM;
|
||||
List<Storage.StoragePoolType> types = Arrays.asList(Storage.StoragePoolType.NetworkFilesystem, Storage.StoragePoolType.Filesystem);
|
||||
Filter filter = mock(Filter.class);
|
||||
List<StoragePoolJoinVO> expectedPools = Collections.singletonList(mock(StoragePoolJoinVO.class));
|
||||
|
||||
doReturn(expectedPools).when(storagePoolJoinDao).listBy(searchCriteria, filter);
|
||||
|
||||
List<StoragePoolJoinVO> result = storagePoolJoinDao.listByZoneHypervisorAndType(zoneId, hypervisorType, types, filter);
|
||||
|
||||
assertSame(expectedPools, result);
|
||||
verify(searchCriteria).setParameters("zoneId", zoneId);
|
||||
verify(searchCriteria).setParameters(eq("hypervisors"), aryEq(new Object[]{Hypervisor.HypervisorType.Any, hypervisorType}));
|
||||
verify(searchCriteria).setParameters(eq("types"), aryEq(types.toArray()));
|
||||
verify(storagePoolJoinDao).listBy(searchCriteria, filter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listByZoneHypervisorAndTypeSkipsTypeFilterWhenTypesAreNull() {
|
||||
long zoneId = 7L;
|
||||
Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.VMware;
|
||||
Filter filter = mock(Filter.class);
|
||||
List<StoragePoolJoinVO> expectedPools = Collections.emptyList();
|
||||
|
||||
doReturn(expectedPools).when(storagePoolJoinDao).listBy(searchCriteria, filter);
|
||||
|
||||
List<StoragePoolJoinVO> result = storagePoolJoinDao.listByZoneHypervisorAndType(zoneId, hypervisorType, null, filter);
|
||||
|
||||
assertSame(expectedPools, result);
|
||||
verify(searchCriteria).setParameters("zoneId", zoneId);
|
||||
verify(searchCriteria).setParameters(eq("hypervisors"), aryEq(new Object[]{Hypervisor.HypervisorType.Any, hypervisorType}));
|
||||
verify(searchCriteria, never()).setParameters(eq("types"), any(Object[].class));
|
||||
verify(storagePoolJoinDao).listBy(searchCriteria, filter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listByZoneHypervisorAndTypeSkipsTypeFilterForEmptyTypesAndPassesNullFilter() {
|
||||
long zoneId = 9L;
|
||||
Hypervisor.HypervisorType hypervisorType = Hypervisor.HypervisorType.XenServer;
|
||||
List<StoragePoolJoinVO> expectedPools = Collections.singletonList(mock(StoragePoolJoinVO.class));
|
||||
|
||||
doReturn(expectedPools).when(storagePoolJoinDao).listBy(searchCriteria, null);
|
||||
|
||||
List<StoragePoolJoinVO> result = storagePoolJoinDao.listByZoneHypervisorAndType(zoneId, hypervisorType, Collections.emptyList(), null);
|
||||
|
||||
assertSame(expectedPools, result);
|
||||
verify(searchCriteria).setParameters("zoneId", zoneId);
|
||||
verify(searchCriteria).setParameters(eq("hypervisors"), aryEq(new Object[]{Hypervisor.HypervisorType.Any, hypervisorType}));
|
||||
verify(searchCriteria, never()).setParameters(eq("types"), any(Object[].class));
|
||||
verify(storagePoolJoinDao).listBy(searchCriteria, null);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue