diff --git a/engine/schema/src/main/resources/META-INF/db/schema-41510to41600.sql b/engine/schema/src/main/resources/META-INF/db/schema-41510to41600.sql index 2e1a592ced5..ad13853fc1f 100644 --- a/engine/schema/src/main/resources/META-INF/db/schema-41510to41600.sql +++ b/engine/schema/src/main/resources/META-INF/db/schema-41510to41600.sql @@ -419,3 +419,95 @@ DROP TABLE IF EXISTS `cloud`.`swift`; ALTER TABLE `cloud`.`snapshots` DROP FOREIGN KEY `fk_snapshots__s3_id` ; ALTER TABLE `cloud`.`snapshots` DROP COLUMN `s3_id` ; DROP TABLE IF EXISTS `cloud`.`s3`; + +-- Re-create host view to prevent multiple entries for hosts with multiple tags +DROP VIEW IF EXISTS `cloud`.`host_view`; +CREATE VIEW `cloud`.`host_view` AS + SELECT + host.id, + host.uuid, + host.name, + host.status, + host.disconnected, + host.type, + host.private_ip_address, + host.version, + host.hypervisor_type, + host.hypervisor_version, + host.capabilities, + host.last_ping, + host.created, + host.removed, + host.resource_state, + host.mgmt_server_id, + host.cpu_sockets, + host.cpus, + host.speed, + host.ram, + cluster.id cluster_id, + cluster.uuid cluster_uuid, + cluster.name cluster_name, + cluster.cluster_type, + data_center.id data_center_id, + data_center.uuid data_center_uuid, + data_center.name data_center_name, + data_center.networktype data_center_type, + host_pod_ref.id pod_id, + host_pod_ref.uuid pod_uuid, + host_pod_ref.name pod_name, + GROUP_CONCAT(DISTINCT(host_tags.tag)) AS tag, + guest_os_category.id guest_os_category_id, + guest_os_category.uuid guest_os_category_uuid, + guest_os_category.name guest_os_category_name, + mem_caps.used_capacity memory_used_capacity, + mem_caps.reserved_capacity memory_reserved_capacity, + cpu_caps.used_capacity cpu_used_capacity, + cpu_caps.reserved_capacity cpu_reserved_capacity, + async_job.id job_id, + async_job.uuid job_uuid, + async_job.job_status job_status, + async_job.account_id job_account_id, + oobm.enabled AS `oobm_enabled`, + oobm.power_state AS `oobm_power_state`, + ha_config.enabled AS `ha_enabled`, + ha_config.ha_state AS `ha_state`, + ha_config.provider AS `ha_provider`, + `last_annotation_view`.`annotation` AS `annotation`, + `last_annotation_view`.`created` AS `last_annotated`, + `user`.`username` AS `username` + FROM + `cloud`.`host` + LEFT JOIN + `cloud`.`cluster` ON host.cluster_id = cluster.id + LEFT JOIN + `cloud`.`data_center` ON host.data_center_id = data_center.id + LEFT JOIN + `cloud`.`host_pod_ref` ON host.pod_id = host_pod_ref.id + LEFT JOIN + `cloud`.`host_details` ON host.id = host_details.host_id + AND host_details.name = 'guest.os.category.id' + LEFT JOIN + `cloud`.`guest_os_category` ON guest_os_category.id = CONVERT ( host_details.value, UNSIGNED ) + LEFT JOIN + `cloud`.`host_tags` ON host_tags.host_id = host.id + LEFT JOIN + `cloud`.`op_host_capacity` mem_caps ON host.id = mem_caps.host_id + AND mem_caps.capacity_type = 0 + LEFT JOIN + `cloud`.`op_host_capacity` cpu_caps ON host.id = cpu_caps.host_id + AND cpu_caps.capacity_type = 1 + LEFT JOIN + `cloud`.`async_job` ON async_job.instance_id = host.id + AND async_job.instance_type = 'Host' + AND async_job.job_status = 0 + LEFT JOIN + `cloud`.`oobm` ON oobm.host_id = host.id + left join + `cloud`.`ha_config` ON ha_config.resource_id=host.id + and ha_config.resource_type='Host' + LEFT JOIN + `cloud`.`last_annotation_view` ON `last_annotation_view`.`entity_uuid` = `host`.`uuid` + LEFT JOIN + `cloud`.`user` ON `user`.`uuid` = `last_annotation_view`.`user_uuid` + GROUP BY + `host`.`id`; diff --git a/server/src/main/java/com/cloud/api/ApiDBUtils.java b/server/src/main/java/com/cloud/api/ApiDBUtils.java index 91af895870d..d688c276a33 100644 --- a/server/src/main/java/com/cloud/api/ApiDBUtils.java +++ b/server/src/main/java/com/cloud/api/ApiDBUtils.java @@ -1891,18 +1891,10 @@ public class ApiDBUtils { return s_hostJoinDao.newHostResponse(vr, details); } - public static HostResponse fillHostDetails(HostResponse vrData, HostJoinVO vr) { - return s_hostJoinDao.setHostResponse(vrData, vr); - } - public static HostForMigrationResponse newHostForMigrationResponse(HostJoinVO vr, EnumSet details) { return s_hostJoinDao.newHostForMigrationResponse(vr, details); } - public static HostForMigrationResponse fillHostForMigrationDetails(HostForMigrationResponse vrData, HostJoinVO vr) { - return s_hostJoinDao.setHostForMigrationResponse(vrData, vr); - } - public static List newHostView(Host vr) { return s_hostJoinDao.newHostView(vr); } diff --git a/server/src/main/java/com/cloud/api/query/ViewResponseHelper.java b/server/src/main/java/com/cloud/api/query/ViewResponseHelper.java index 1067ff2ed47..ec3397407f2 100644 --- a/server/src/main/java/com/cloud/api/query/ViewResponseHelper.java +++ b/server/src/main/java/com/cloud/api/query/ViewResponseHelper.java @@ -242,14 +242,7 @@ public class ViewResponseHelper { Hashtable vrDataList = new Hashtable(); // Initialise the vrdatalist with the input data for (HostJoinVO vr : hosts) { - HostResponse vrData = vrDataList.get(vr.getId()); - if (vrData == null) { - // first time encountering this vm - vrData = ApiDBUtils.newHostResponse(vr, details); - } else { - // update tags - vrData = ApiDBUtils.fillHostDetails(vrData, vr); - } + HostResponse vrData = ApiDBUtils.newHostResponse(vr, details); vrDataList.put(vr.getId(), vrData); } return new ArrayList(vrDataList.values()); @@ -259,14 +252,7 @@ public class ViewResponseHelper { Hashtable vrDataList = new Hashtable(); // Initialise the vrdatalist with the input data for (HostJoinVO vr : hosts) { - HostForMigrationResponse vrData = vrDataList.get(vr.getId()); - if (vrData == null) { - // first time encountering this vm - vrData = ApiDBUtils.newHostForMigrationResponse(vr, details); - } else { - // update tags - vrData = ApiDBUtils.fillHostForMigrationDetails(vrData, vr); - } + HostForMigrationResponse vrData = ApiDBUtils.newHostForMigrationResponse(vr, details); vrDataList.put(vr.getId(), vrData); } return new ArrayList(vrDataList.values()); diff --git a/server/src/main/java/com/cloud/api/query/dao/HostJoinDao.java b/server/src/main/java/com/cloud/api/query/dao/HostJoinDao.java index e7dc5d5ff04..e7ad5e53862 100644 --- a/server/src/main/java/com/cloud/api/query/dao/HostJoinDao.java +++ b/server/src/main/java/com/cloud/api/query/dao/HostJoinDao.java @@ -31,12 +31,8 @@ public interface HostJoinDao extends GenericDao { HostResponse newHostResponse(HostJoinVO host, EnumSet details); - HostResponse setHostResponse(HostResponse response, HostJoinVO host); - HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, EnumSet details); - HostForMigrationResponse setHostForMigrationResponse(HostForMigrationResponse response, HostJoinVO host); - List newHostView(Host group); List searchByIds(Long... ids); diff --git a/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java b/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java index 32236093f03..af392a25402 100644 --- a/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java +++ b/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java @@ -18,6 +18,7 @@ package com.cloud.api.query.dao; import java.text.DecimalFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.EnumSet; import java.util.Iterator; @@ -93,6 +94,18 @@ public class HostJoinDaoImpl extends GenericDaoBase implements this._count = "select count(distinct id) from host_view WHERE "; } + private boolean containsHostHATag(final String tags) { + boolean result = false; + String haTag = ApiDBUtils.getHaTag(); + if (StringUtils.isNotEmpty(haTag) && StringUtils.isNotEmpty(tags)) { + List tagsList = Arrays.asList(tags.split(",")); + if (tagsList.contains(haTag)) { + result = true; + } + } + return result; + } + @Override public HostResponse newHostResponse(HostJoinVO host, EnumSet details) { HostResponse hostResponse = new HostResponse(); @@ -180,13 +193,7 @@ public class HostJoinDaoImpl extends GenericDaoBase implements String hostTags = host.getTag(); hostResponse.setHostTags(hostTags); - - hostResponse.setHaHost(false); - String haTag = ApiDBUtils.getHaTag(); - if (StringUtils.isNotEmpty(haTag) && StringUtils.isNotEmpty(hostTags) && - haTag.equalsIgnoreCase(hostTags)) { - hostResponse.setHaHost(true); - } + hostResponse.setHaHost(containsHostHATag(hostTags)); hostResponse.setHypervisorVersion(host.getHypervisorVersion()); @@ -268,26 +275,6 @@ public class HostJoinDaoImpl extends GenericDaoBase implements return hostResponse; } - @Override - public HostResponse setHostResponse(HostResponse response, HostJoinVO host) { - String tag = host.getTag(); - if (StringUtils.isNotEmpty(tag)) { - if (StringUtils.isNotEmpty(response.getHostTags())) { - response.setHostTags(response.getHostTags() + "," + tag); - } else { - response.setHostTags(tag); - } - - if (Boolean.FALSE.equals(response.getHaHost())) { - String haTag = ApiDBUtils.getHaTag(); - if (StringUtils.isNotEmpty(haTag) && haTag.equalsIgnoreCase(tag)) { - response.setHaHost(true); - } - } - } - return response; - } - @Override public HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, EnumSet details) { HostForMigrationResponse hostResponse = new HostForMigrationResponse(); @@ -339,13 +326,7 @@ public class HostJoinDaoImpl extends GenericDaoBase implements String hostTags = host.getTag(); hostResponse.setHostTags(hostTags); - - hostResponse.setHaHost(false); - String haTag = ApiDBUtils.getHaTag(); - if (StringUtils.isNotEmpty(haTag) && StringUtils.isNotEmpty(hostTags) && - haTag.equalsIgnoreCase(hostTags)) { - hostResponse.setHaHost(true); - } + hostResponse.setHaHost(containsHostHATag(hostTags)); hostResponse.setHypervisorVersion(host.getHypervisorVersion()); @@ -410,26 +391,6 @@ public class HostJoinDaoImpl extends GenericDaoBase implements return hostResponse; } - @Override - public HostForMigrationResponse setHostForMigrationResponse(HostForMigrationResponse response, HostJoinVO host) { - String tag = host.getTag(); - if (tag != null) { - if (response.getHostTags() != null && response.getHostTags().length() > 0) { - response.setHostTags(response.getHostTags() + "," + tag); - } else { - response.setHostTags(tag); - } - - if (Boolean.FALSE.equals(response.getHaHost())) { - String haTag = ApiDBUtils.getHaTag(); - if (StringUtils.isNotEmpty(haTag) && haTag.equalsIgnoreCase(tag)) { - response.setHaHost(true); - } - } - } - return response; - } - @Override public List newHostView(Host host) { SearchCriteria sc = hostIdSearch.create();