diff --git a/app/models/host.rb b/app/models/host.rb index 1e849eebbee..15aa689d525 100644 --- a/app/models/host.rb +++ b/app/models/host.rb @@ -194,6 +194,17 @@ def self.failover where(:failover => true) end + def self.all_unique_downcased_domains(includes = nil) + select_rows = select("lower(regexp_replace(split_part(hostname, ',', 1), '[^\.]*\.', '')) as domain") + .select(:id).to_sql + .sub!("SELECT", "DISTINCT ON (domain)") + .sub!(/ FROM.*$/, '') + + query = select(select_rows).where.not(:hostname => nil) + query = query.includes(includes) if includes + query + end + def authentication_check_role 'smartstate' end diff --git a/app/models/miq_provision_virt_workflow.rb b/app/models/miq_provision_virt_workflow.rb index ae9643595e1..f8c8c94d4f0 100644 --- a/app/models/miq_provision_virt_workflow.rb +++ b/app/models/miq_provision_virt_workflow.rb @@ -542,13 +542,15 @@ def allowed_domains(options = {}) @domains ||= begin domains = {} if @values[:forced_sysprep_domain_name].blank? - Host.all.each do |host| - domain = host.domain.to_s.downcase - next if domain.blank? || domains.key?(domain) - # Filter by host platform or is proxy is active + includes = [:operating_system, :hardware] if options[:platform] + Host.all_unique_downcased_domains(includes).each do |host| + # Filter by host platform + # FIXME: Does this even make sense? The domain could be applied to + # hosts with multiple different opperating systems, right? And only + # the first one pulled in will be what is filtered? next unless options[:platform].nil? || options[:platform].include?(host.platform) - next unless options[:active_proxy].nil? || host.is_proxy_active? == options[:active_proxy] - domains[domain] = domain + + domains[host.attributes["domain"]] = host.attributes["domain"] end else @values[:forced_sysprep_domain_name].to_miq_a.each { |d| domains[d] = d }