Skip to content

Commit ccec275

Browse files
author
Prachi Damle
committed
CLOUDSTACK-3904 listHosts API fails in VMware setup when virtualmachineid parameter is passed
Changes: - Pod and Cluster can be null, modified the query to append these only if non-null
1 parent 2d5f24c commit ccec275

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

‎engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
9595
private static final String ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART1 =
9696
"SELECT host.cluster_id, SUM(IF(vm.state='Running' AND vm.account_id = ?, 1, 0)) FROM `cloud`.`host` host LEFT JOIN `cloud`.`vm_instance` vm ON host.id = vm.host_id WHERE ";
9797
private static final String ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2 =
98-
" AND host.type = 'Routing' GROUP BY host.cluster_id ORDER BY 2 ASC ";
98+
" AND host.type = 'Routing' AND host.removed is null GROUP BY host.cluster_id ORDER BY 2 ASC ";
9999

100-
private static final String ORDER_PODS_NUMBER_OF_VMS_FOR_ACCOUNT = "SELECT pod.id, SUM(IF(vm.state='Running' AND vm.account_id = ?, 1, 0)) FROM `cloud`.`host_pod_ref` pod LEFT JOIN `cloud`.`vm_instance` vm ON pod.id = vm.pod_id WHERE pod.data_center_id = ? " +
100+
private static final String ORDER_PODS_NUMBER_OF_VMS_FOR_ACCOUNT = "SELECT pod.id, SUM(IF(vm.state='Running' AND vm.account_id = ?, 1, 0)) FROM `cloud`.`host_pod_ref` pod LEFT JOIN `cloud`.`vm_instance` vm ON pod.id = vm.pod_id WHERE pod.data_center_id = ? AND pod.removed is null " +
101101
" GROUP BY pod.id ORDER BY 2 ASC ";
102102

103-
private static final String ORDER_HOSTS_NUMBER_OF_VMS_FOR_ACCOUNT =
104-
"SELECT host.id, SUM(IF(vm.state='Running' AND vm.account_id = ?, 1, 0)) FROM `cloud`.`host` host LEFT JOIN `cloud`.`vm_instance` vm ON host.id = vm.host_id WHERE host.data_center_id = ? " +
105-
" AND host.pod_id = ? AND host.cluster_id = ? AND host.type = 'Routing' " +
106-
" GROUP BY host.id ORDER BY 2 ASC ";
103+
private static final String ORDER_HOSTS_NUMBER_OF_VMS_FOR_ACCOUNT = "SELECT host.id, SUM(IF(vm.state='Running' AND vm.account_id = ?, 1, 0)) FROM `cloud`.`host` host LEFT JOIN `cloud`.`vm_instance` vm ON host.id = vm.host_id WHERE host.data_center_id = ? "
104+
+ " AND host.type = 'Routing' AND host.removed is null ";
105+
106+
private static final String ORDER_HOSTS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2 = " GROUP BY host.id ORDER BY 2 ASC ";
107107

108108
@Inject protected HostDao _hostDao;
109109

@@ -561,11 +561,25 @@ public List<Long> listHostIdsByVmCount(long dcId, Long podId, Long clusterId, lo
561561
List<Long> result = new ArrayList<Long>();
562562
try {
563563
String sql = ORDER_HOSTS_NUMBER_OF_VMS_FOR_ACCOUNT;
564+
if (podId != null) {
565+
sql = sql + " AND host.pod_id = ? ";
566+
}
567+
568+
if (clusterId != null) {
569+
sql = sql + " AND host.cluster_id = ? ";
570+
}
571+
572+
sql = sql + ORDER_HOSTS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2;
573+
564574
pstmt = txn.prepareAutoCloseStatement(sql);
565575
pstmt.setLong(1, accountId);
566576
pstmt.setLong(2, dcId);
567-
pstmt.setLong(3, podId);
568-
pstmt.setLong(4, clusterId);
577+
if (podId != null) {
578+
pstmt.setLong(3, podId);
579+
}
580+
if (clusterId != null) {
581+
pstmt.setLong(4, clusterId);
582+
}
569583

570584
ResultSet rs = pstmt.executeQuery();
571585
while (rs.next()) {

0 commit comments

Comments
 (0)