Module: ForemanAnsible::HostManagedExtensions::ClassMethods

Defined in:
app/models/concerns/foreman_ansible/host_managed_extensions.rb

Overview

Class methods we may need to override or add

Instance Method Summary collapse

Instance Method Details

#import_host(*args) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'app/models/concerns/foreman_ansible/host_managed_extensions.rb', line 66

def import_host(*args)
  host = super(*args)
  hostname = args[0]
  if (Resolv::IPv4::Regex.match?(hostname) || Resolv::IPv6::Regex.match?(hostname)) &&
     (host_nic = Nic::Interface.find_by(:ip => hostname))
    host = host_nic.host
  end
  host
end

#search_by_role(_key, operator, value) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'app/models/concerns/foreman_ansible/host_managed_extensions.rb', line 76

def search_by_role(_key, operator, value)
  conditions = sanitize_sql_for_conditions(["ansible_roles.name #{operator} ?", value_to_sql(operator, value)])
  host_ids = ::Host::Managed.joins(:ansible_roles).where(conditions).distinct.pluck(:id)
  hostgroup_ids = ::Hostgroup.unscoped.with_taxonomy_scope.joins(:ansible_roles).where(conditions).map(&:subtree_ids).flatten

  conds = []
  conds << "hosts.id IN(#{host_ids.join(',')})" if host_ids.present?
  conds << "hosts.hostgroup_id IN(#{hostgroup_ids.uniq.join(',')})" if hostgroup_ids.present?
  { conditions: conds.join(' OR ').presence || '1 = 0' }
end