Method: ForemanPuppet::Puppetclass.search_by_host

Defined in:
app/models/foreman_puppet/puppetclass.rb

.search_by_host(_key, operator, value) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/foreman_puppet/puppetclass.rb', line 129

def self.search_by_host(_key, operator, value)
  conditions = sanitize_sql_for_conditions(["hosts.name #{operator} ?", value_to_sql(operator, value)])
  direct     = Puppetclass.joins(host_puppet_facets: :host).where(conditions).pluck(:id).uniq
  hostgroup  = Hostgroup.joins(:hosts).find_by(conditions)
  indirect   = if hostgroup.blank?
                 []
               else
                 HostgroupClass.joins(hostgroup_puppet_facet: :hostgroup)
                               .where(Hostgroup.arel_table[:id].in(hostgroup.path_ids))
                               .pluck(:puppetclass_id).uniq
               end
  return { conditions: '1=0' } if direct.blank? && indirect.blank?

  puppet_classes = (direct + indirect).uniq
  { conditions: "puppetclasses.id IN(#{puppet_classes.join(',')})" }
end