Skip to content

Commit

Permalink
Fixes #37696 - Add convert2rhel env fact to host subscription facet
Browse files Browse the repository at this point in the history
  • Loading branch information
chris1984 committed Aug 14, 2024
1 parent 06dacfb commit 4bd1489
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module SubscriptionFacetHostExtensions
scoped_search :on => :registered_through, :relation => :subscription_facet, :complete_value => true, :only_explicit => true
scoped_search :on => :registered_at, :relation => :subscription_facet, :rename => :registered_at, :only_explicit => true
scoped_search :on => :uuid, :relation => :subscription_facet, :rename => :subscription_uuid, :only_explicit => true
scoped_search :on => :convert2rhel_through_foreman, :relation => :subscription_facet, :only_explicit => true
scoped_search :relation => :activation_keys, :on => :name, :rename => :activation_key, :complete_value => true, :ext_method => :find_by_activation_key
scoped_search :relation => :activation_keys, :on => :id, :rename => :activation_key_id, :complete_value => true, :ext_method => :find_by_activation_key_id,
:only_explicit => true, :validator => ScopedSearch::Validators::INTEGER
Expand Down
12 changes: 12 additions & 0 deletions app/models/katello/host/subscription_facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ def backend_update_needed?
false
end

def self.populate_fields_from_facts(host, parser, _type, _source_proxy)
has_convert2rhel = parser.facts.key?('conversions.env.CONVERT2RHEL_THROUGH_FOREMAN')
# Add in custom convert2rhel fact if system was converted using convert2rhel through Katello
# We want the value nil unless the custom fact is present otherwise we get a 0 in the database which if debugging
# might make you think it was converted2rhel but not with satellite, that is why I have the tenary below.
facet = host.subscription_facet || host.build_subscription_facet
facet.attributes = {
convert2rhel_through_foreman: has_convert2rhel ? ::Foreman::Cast.to_bool(parser.facts['conversions.env.CONVERT2RHEL_THROUGH_FOREMAN']) : nil
}.compact
facet.save unless facet.new_record?
end

private

def update_status(status_class, **args)
Expand Down
2 changes: 1 addition & 1 deletion app/views/katello/api/v2/subscription_facet/base.json.rabl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
attributes :id, :uuid, :last_checkin, :service_level, :release_version, :autoheal, :registered_at, :registered_through, :purpose_role, :purpose_usage, :hypervisor
attributes :id, :uuid, :last_checkin, :service_level, :release_version, :autoheal, :registered_at, :registered_through, :purpose_role, :purpose_usage, :hypervisor, :convert2rhel_through_foreman

child :user => :user do
attributes :id, :login
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20240729192228_add_convert2rhel_to_host_facets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddConvert2rhelToHostFacets < ActiveRecord::Migration[6.1]
def up
add_column :katello_subscription_facets, :convert2rhel_through_foreman, :int4
end

def down
remove_column :subscription_facets, :convert2rhel_through_foreman
end
end
13 changes: 13 additions & 0 deletions test/models/host/subscription_facet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ def test_search_role
assert_includes ::Host.search_for("role = satellite"), host
end

def test_convert2rhel_through_foreman_on_host
subscription_facet.update(convert2rhel_through_foreman: 1)
assert_equal 1, host.subscription_facet.convert2rhel_through_foreman
assert_includes ::Host.search_for("convert2rhel_through_foreman = 1"), host
end

def test_convert2rhel_through_foreman_not_on_host
# We want the value nil unless the custom fact is present otherwise we get a 0 in the database which if debugging
# might make you think it was converted2rhel but not with satellite.
assert_nil host.subscription_facet.convert2rhel_through_foreman
refute_equal 0, host.subscription_facet.convert2rhel_through_foreman
end

def test_search_addon
host.subscription_facet.purpose_addons << katello_purpose_addons(:addon)
assert_includes ::Host.search_for("addon = \"Test Addon\""), host
Expand Down

0 comments on commit 4bd1489

Please sign in to comment.