Skip to content

Commit

Permalink
Filter list VMs by IP address (#9547)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pearl1594 authored Oct 8, 2024
1 parent e666dca commit 9c86c4d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
42 changes: 29 additions & 13 deletions framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ public List<T> searchIncludingRemoved(SearchCriteria<T> sc, final Filter filter,
pstmt.setObject(i++, value);
}
}

if (s_logger.isDebugEnabled() && lock != null) {
txn.registerLock(pstmt.toString());
}
Expand Down Expand Up @@ -1342,22 +1341,39 @@ protected List<Attribute> addJoins(StringBuilder str, Collection<JoinBuilder<Sea
onClause.append("?");
joinAttrList.add(join.getFirstAttributes()[i]);
} else {
onClause.append(joinedTableNames.getOrDefault(join.getFirstAttributes()[i].table, join.getFirstAttributes()[i].table))
.append(".")
.append(join.getFirstAttributes()[i].columnName);
if ((join.getFirstAttributes()[i].table == null && join.getFirstAttributes()[i].value == null) ||
(join.getSecondAttribute()[i].table == null && join.getSecondAttribute()[i].value == null)) {
onClause.append(joinedTableNames.getOrDefault(join.getSecondAttribute()[i].table, join.getFirstAttributes()[i].table))
.append(".");
if (join.getFirstAttributes()[i].table == null && join.getFirstAttributes()[i].value == null) {
onClause.append(join.getSecondAttribute()[i].columnName);
} else {
onClause.append(join.getFirstAttributes()[i].columnName);
}

} else {
onClause.append(joinedTableNames.getOrDefault(join.getFirstAttributes()[i].table, join.getFirstAttributes()[i].table))
.append(".")
.append(join.getFirstAttributes()[i].columnName);
}
}
onClause.append("=");
if (join.getSecondAttribute()[i].getValue() != null) {
onClause.append("?");
joinAttrList.add(join.getSecondAttribute()[i]);
if ((join.getFirstAttributes()[i].table == null && join.getFirstAttributes()[i].value == null) ||
(join.getSecondAttribute()[i].table == null && join.getSecondAttribute()[i].value == null)) {
onClause.append(" IS NULL");
} else {
if(!joinTableAlias.equals(joinTableName)) {
onClause.append(joinTableAlias);
onClause.append("=");
if (join.getSecondAttribute()[i].getValue() != null) {
onClause.append("?");
joinAttrList.add(join.getSecondAttribute()[i]);
} else {
onClause.append(joinTableName);
if (!joinTableAlias.equals(joinTableName)) {
onClause.append(joinTableAlias);
} else {
onClause.append(joinTableName);
}
onClause.append(".")
.append(join.getSecondAttribute()[i].columnName);
}
onClause.append(".")
.append(join.getSecondAttribute()[i].columnName);
}
}
onClause.append(" ");
Expand Down
24 changes: 24 additions & 0 deletions server/src/main/java/com/cloud/api/query/QueryManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import javax.inject.Inject;

import com.cloud.network.dao.IPAddressDao;
import com.cloud.network.dao.IPAddressVO;
import com.cloud.storage.StoragePool;
import com.cloud.storage.StoragePoolHostVO;
import com.cloud.event.EventVO;
Expand Down Expand Up @@ -549,6 +551,9 @@ public class QueryManagerImpl extends MutualExclusiveIdsManagerBase implements Q
@Inject
private NetworkDao networkDao;

@Inject
private IPAddressDao ipAddressDao;

@Inject
private NicDao nicDao;

Expand Down Expand Up @@ -1453,6 +1458,22 @@ private Pair<List<Long>, Integer> searchForUserVMIdsAndCount(ListVMsCmd cmd) {
if (isRootAdmin) {
userVmSearchBuilder.or("keywordInstanceName", userVmSearchBuilder.entity().getInstanceName(), Op.LIKE );
}

SearchBuilder<IPAddressVO> ipAddressSearch = ipAddressDao.createSearchBuilder();
userVmSearchBuilder.join("ipAddressSearch", ipAddressSearch,
ipAddressSearch.entity().getAssociatedWithVmId(), userVmSearchBuilder.entity().getId(), JoinBuilder.JoinType.LEFT);

SearchBuilder<NicVO> nicSearch = nicDao.createSearchBuilder();
userVmSearchBuilder.join("nicSearch", nicSearch, JoinBuilder.JoinType.LEFT,
JoinBuilder.JoinCondition.AND,
nicSearch.entity().getInstanceId(), userVmSearchBuilder.entity().getId(),
nicSearch.entity().getRemoved(), userVmSearchBuilder.entity().setLong(null));

userVmSearchBuilder.or("ipAddressSearch", "keywordPublicIpAddress", ipAddressSearch.entity().getAddress(), Op.LIKE);

userVmSearchBuilder.or("nicSearch", "keywordIpAddress", nicSearch.entity().getIPv4Address(), Op.LIKE);
userVmSearchBuilder.or("nicSearch", "keywordIp6Address", nicSearch.entity().getIPv6Address(), Op.LIKE);

userVmSearchBuilder.cp();
}

Expand Down Expand Up @@ -1546,6 +1567,9 @@ private Pair<List<Long>, Integer> searchForUserVMIdsAndCount(ListVMsCmd cmd) {
userVmSearchCriteria.setParameters("keywordDisplayName", keywordMatch);
userVmSearchCriteria.setParameters("keywordName", keywordMatch);
userVmSearchCriteria.setParameters("keywordState", keyword);
userVmSearchCriteria.setParameters("keywordIpAddress", keywordMatch);
userVmSearchCriteria.setParameters("keywordPublicIpAddress", keywordMatch);
userVmSearchCriteria.setParameters("keywordIp6Address", keywordMatch);
if (isRootAdmin) {
userVmSearchCriteria.setParameters("keywordInstanceName", keywordMatch);
}
Expand Down

0 comments on commit 9c86c4d

Please sign in to comment.