Module: Katello::Concerns::ContentFacetHostExtensions::ClassMethods

Defined in:
app/models/katello/concerns/content_facet_host_extensions.rb

Instance Method Summary collapse

Instance Method Details

#build_condition(operator, subquery) ⇒ Object



92
93
94
95
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 92

def build_condition(operator, subquery)
  sql_in = operator == 'IS NULL' ? 'NOT IN' : 'IN'
  { :conditions => "#{::Host::Managed.table_name}.id #{sql_in} (#{subquery.to_sql})" }
end

#find_by_applicable_debs(_key, operator, value) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 123

def find_by_applicable_debs(_key, operator, value)
  if value.nil?
    hosts = ::Host::Managed.joins(:applicable_debs).select(:id)
    return build_condition(operator, hosts)
  end
  hosts = find_by_debs(::Host::Managed.joins(:applicable_debs), operator, value)
  if hosts.empty?
    { :conditions => "1=0" }
  else
    { :conditions => "#{::Host::Managed.table_name}.id IN (#{hosts.pluck(:id).join(',')})" }
  end
end

#find_by_applicable_errata(_key, operator, value) ⇒ Object



109
110
111
112
113
114
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 109

def find_by_applicable_errata(_key, operator, value)
  hosts = ::Host::Managed.joins(:applicable_errata).select(:id)
  return build_condition(operator, hosts) if value.nil?
  conditions = sanitize_sql_for_conditions(["#{Katello::Erratum.table_name}.errata_id #{operator} ?", value_to_sql(operator, value)])
  { :conditions => "#{::Host::Managed.table_name}.id IN (#{hosts.where(conditions).to_sql})" }
end

#find_by_applicable_rpms(_key, operator, value) ⇒ Object



149
150
151
152
153
154
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 149

def find_by_applicable_rpms(_key, operator, value)
  hosts = ::Host::Managed.joins(:applicable_rpms).select(:id)
  return build_condition(operator, hosts) if value.nil?
  conditions = sanitize_sql_for_conditions(["#{Katello::Rpm.table_name}.nvra #{operator} ?", value_to_sql(operator, value)])
  { :conditions => "#{::Host::Managed.table_name}.id IN (#{hosts.where(conditions).to_sql})" }
end

#find_by_image_mode(_key, _operator, value) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 97

def find_by_image_mode(_key, _operator, value)
  # operator is always '='
  state = ::Foreman::Cast.to_bool(value)
  if state
    hosts = ::Host::Managed.joins(:content_facet).select(:id).where.not("#{::Katello::Host::ContentFacet.table_name}.bootc_booted_image" => nil)
  else
    # left_outer_joins will include hosts without a content facet. We assume such hosts are package-mode hosts.
    hosts = ::Host::Managed.left_outer_joins(:content_facet).select(:id).where("#{::Katello::Host::ContentFacet.table_name}.bootc_booted_image" => nil)
  end
  { :conditions => "#{::Host::Managed.table_name}.id IN (#{hosts.to_sql})" }
end

#find_by_installable_debs(_key, operator, value) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 136

def find_by_installable_debs(_key, operator, value)
  if value.nil?
    facets = Katello::Host::ContentFacet.joins_installable_debs.select(:host_id)
    return build_condition(operator, facets)
  end
  facets = find_by_debs(Katello::Host::ContentFacet.joins_installable_debs, operator, value)
  if facets.empty?
    { :conditions => "1=0" }
  else
    { :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.pluck(:host_id).join(',')})" }
  end
end

#find_by_installable_errata(_key, operator, value) ⇒ Object



116
117
118
119
120
121
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 116

def find_by_installable_errata(_key, operator, value)
  facets = Katello::Host::ContentFacet.joins_installable_errata.select(:host_id)
  return build_condition(operator, facets) if value.nil?
  conditions = sanitize_sql_for_conditions(["#{Katello::Erratum.table_name}.errata_id #{operator} ?", value_to_sql(operator, value)])
  { :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.where(conditions).to_sql})" }
end

#find_by_installable_rpms(_key, operator, value) ⇒ Object



156
157
158
159
160
161
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 156

def find_by_installable_rpms(_key, operator, value)
  facets = Katello::Host::ContentFacet.joins_installable_rpms.select(:host_id)
  return build_condition(operator, facets) if value.nil?
  conditions = sanitize_sql_for_conditions(["#{Katello::Rpm.table_name}.nvra #{operator} ?", value_to_sql(operator, value)])
  { :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.where(conditions).to_sql})" }
end

#find_by_repository_content_label(_key, operator, value) ⇒ Object



163
164
165
166
167
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 163

def (_key, operator, value)
  conditions = sanitize_sql_for_conditions(["#{Katello::Content.table_name}.label #{operator} ?", value_to_sql(operator, value)])
  facets = Katello::Host::ContentFacet.joins_repositories.select(:host_id).where(conditions)
  { :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.to_sql})" }
end

#find_by_repository_name(_key, operator, value) ⇒ Object



169
170
171
172
173
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 169

def find_by_repository_name(_key, operator, value)
  conditions = sanitize_sql_for_conditions(["#{Katello::RootRepository.table_name}.name #{operator} ?", value_to_sql(operator, value)])
  facets = Katello::Host::ContentFacet.joins_repositories.select(:host_id).where(conditions)
  { :conditions => "#{::Host::Managed.table_name}.id IN (#{facets.to_sql})" }
end

#in_content_view_environment(content_view: nil, lifecycle_environment: nil) ⇒ Object



186
187
188
189
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 186

def in_content_view_environment(content_view: nil, lifecycle_environment: nil)
  Rails.logger.warn "DEPRECATION WARNING: in_content_view_environment is deprecated and will be removed in Katello 4.8.  Please use in_content_view_environments instead."
  in_content_view_environments(:content_views => content_view, :lifecycle_environments => lifecycle_environment)
end

#in_content_view_environments(content_views: nil, lifecycle_environments: nil) ⇒ Object



175
176
177
178
179
180
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 175

def in_content_view_environments(content_views: nil, lifecycle_environments: nil)
  relation = self.joins(:content_facet => {:content_view_environment_content_facets => :content_view_environment })
  relation = relation.where("#{::Katello::ContentViewEnvironment.table_name}.content_view_id" => content_views) if content_views
  relation = relation.where("#{::Katello::ContentViewEnvironment.table_name}.environment_id" => lifecycle_environments) if lifecycle_environments
  relation
end

#in_environment(lifecycle_environment) ⇒ Object



191
192
193
194
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 191

def in_environment(lifecycle_environment)
  Rails.logger.warn "DEPRECATION WARNING: in_environment is deprecated and will be removed in Katello 4.9.  Please use in_environments instead."
  in_environments(lifecycle_environment)
end

#in_environments(lifecycle_environments) ⇒ Object



182
183
184
# File 'app/models/katello/concerns/content_facet_host_extensions.rb', line 182

def in_environments(lifecycle_environments)
  in_content_view_environments(:lifecycle_environments => lifecycle_environments)
end