Skip to content

Commit 9c86c4d

Browse files
authored
Filter list VMs by IP address (#9547)
1 parent e666dca commit 9c86c4d

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ public List<T> searchIncludingRemoved(SearchCriteria<T> sc, final Filter filter,
421421
pstmt.setObject(i++, value);
422422
}
423423
}
424-
425424
if (s_logger.isDebugEnabled() && lock != null) {
426425
txn.registerLock(pstmt.toString());
427426
}
@@ -1342,22 +1341,39 @@ protected List<Attribute> addJoins(StringBuilder str, Collection<JoinBuilder<Sea
13421341
onClause.append("?");
13431342
joinAttrList.add(join.getFirstAttributes()[i]);
13441343
} else {
1345-
onClause.append(joinedTableNames.getOrDefault(join.getFirstAttributes()[i].table, join.getFirstAttributes()[i].table))
1346-
.append(".")
1347-
.append(join.getFirstAttributes()[i].columnName);
1344+
if ((join.getFirstAttributes()[i].table == null && join.getFirstAttributes()[i].value == null) ||
1345+
(join.getSecondAttribute()[i].table == null && join.getSecondAttribute()[i].value == null)) {
1346+
onClause.append(joinedTableNames.getOrDefault(join.getSecondAttribute()[i].table, join.getFirstAttributes()[i].table))
1347+
.append(".");
1348+
if (join.getFirstAttributes()[i].table == null && join.getFirstAttributes()[i].value == null) {
1349+
onClause.append(join.getSecondAttribute()[i].columnName);
1350+
} else {
1351+
onClause.append(join.getFirstAttributes()[i].columnName);
1352+
}
1353+
1354+
} else {
1355+
onClause.append(joinedTableNames.getOrDefault(join.getFirstAttributes()[i].table, join.getFirstAttributes()[i].table))
1356+
.append(".")
1357+
.append(join.getFirstAttributes()[i].columnName);
1358+
}
13481359
}
1349-
onClause.append("=");
1350-
if (join.getSecondAttribute()[i].getValue() != null) {
1351-
onClause.append("?");
1352-
joinAttrList.add(join.getSecondAttribute()[i]);
1360+
if ((join.getFirstAttributes()[i].table == null && join.getFirstAttributes()[i].value == null) ||
1361+
(join.getSecondAttribute()[i].table == null && join.getSecondAttribute()[i].value == null)) {
1362+
onClause.append(" IS NULL");
13531363
} else {
1354-
if(!joinTableAlias.equals(joinTableName)) {
1355-
onClause.append(joinTableAlias);
1364+
onClause.append("=");
1365+
if (join.getSecondAttribute()[i].getValue() != null) {
1366+
onClause.append("?");
1367+
joinAttrList.add(join.getSecondAttribute()[i]);
13561368
} else {
1357-
onClause.append(joinTableName);
1369+
if (!joinTableAlias.equals(joinTableName)) {
1370+
onClause.append(joinTableAlias);
1371+
} else {
1372+
onClause.append(joinTableName);
1373+
}
1374+
onClause.append(".")
1375+
.append(join.getSecondAttribute()[i].columnName);
13581376
}
1359-
onClause.append(".")
1360-
.append(join.getSecondAttribute()[i].columnName);
13611377
}
13621378
}
13631379
onClause.append(" ");

server/src/main/java/com/cloud/api/query/QueryManagerImpl.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
import javax.inject.Inject;
3636

37+
import com.cloud.network.dao.IPAddressDao;
38+
import com.cloud.network.dao.IPAddressVO;
3739
import com.cloud.storage.StoragePool;
3840
import com.cloud.storage.StoragePoolHostVO;
3941
import com.cloud.event.EventVO;
@@ -549,6 +551,9 @@ public class QueryManagerImpl extends MutualExclusiveIdsManagerBase implements Q
549551
@Inject
550552
private NetworkDao networkDao;
551553

554+
@Inject
555+
private IPAddressDao ipAddressDao;
556+
552557
@Inject
553558
private NicDao nicDao;
554559

@@ -1453,6 +1458,22 @@ private Pair<List<Long>, Integer> searchForUserVMIdsAndCount(ListVMsCmd cmd) {
14531458
if (isRootAdmin) {
14541459
userVmSearchBuilder.or("keywordInstanceName", userVmSearchBuilder.entity().getInstanceName(), Op.LIKE );
14551460
}
1461+
1462+
SearchBuilder<IPAddressVO> ipAddressSearch = ipAddressDao.createSearchBuilder();
1463+
userVmSearchBuilder.join("ipAddressSearch", ipAddressSearch,
1464+
ipAddressSearch.entity().getAssociatedWithVmId(), userVmSearchBuilder.entity().getId(), JoinBuilder.JoinType.LEFT);
1465+
1466+
SearchBuilder<NicVO> nicSearch = nicDao.createSearchBuilder();
1467+
userVmSearchBuilder.join("nicSearch", nicSearch, JoinBuilder.JoinType.LEFT,
1468+
JoinBuilder.JoinCondition.AND,
1469+
nicSearch.entity().getInstanceId(), userVmSearchBuilder.entity().getId(),
1470+
nicSearch.entity().getRemoved(), userVmSearchBuilder.entity().setLong(null));
1471+
1472+
userVmSearchBuilder.or("ipAddressSearch", "keywordPublicIpAddress", ipAddressSearch.entity().getAddress(), Op.LIKE);
1473+
1474+
userVmSearchBuilder.or("nicSearch", "keywordIpAddress", nicSearch.entity().getIPv4Address(), Op.LIKE);
1475+
userVmSearchBuilder.or("nicSearch", "keywordIp6Address", nicSearch.entity().getIPv6Address(), Op.LIKE);
1476+
14561477
userVmSearchBuilder.cp();
14571478
}
14581479

@@ -1546,6 +1567,9 @@ private Pair<List<Long>, Integer> searchForUserVMIdsAndCount(ListVMsCmd cmd) {
15461567
userVmSearchCriteria.setParameters("keywordDisplayName", keywordMatch);
15471568
userVmSearchCriteria.setParameters("keywordName", keywordMatch);
15481569
userVmSearchCriteria.setParameters("keywordState", keyword);
1570+
userVmSearchCriteria.setParameters("keywordIpAddress", keywordMatch);
1571+
userVmSearchCriteria.setParameters("keywordPublicIpAddress", keywordMatch);
1572+
userVmSearchCriteria.setParameters("keywordIp6Address", keywordMatch);
15491573
if (isRootAdmin) {
15501574
userVmSearchCriteria.setParameters("keywordInstanceName", keywordMatch);
15511575
}

0 commit comments

Comments
 (0)