diff --git a/app/models/katello/concerns/subscription_facet_host_extensions.rb b/app/models/katello/concerns/subscription_facet_host_extensions.rb index 766c4ac4302..eab06c6fd7c 100644 --- a/app/models/katello/concerns/subscription_facet_host_extensions.rb +++ b/app/models/katello/concerns/subscription_facet_host_extensions.rb @@ -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 diff --git a/app/models/katello/host/subscription_facet.rb b/app/models/katello/host/subscription_facet.rb index 9ea4e2ae61d..a9cf4d30697 100644 --- a/app/models/katello/host/subscription_facet.rb +++ b/app/models/katello/host/subscription_facet.rb @@ -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) diff --git a/app/views/katello/api/v2/subscription_facet/base.json.rabl b/app/views/katello/api/v2/subscription_facet/base.json.rabl index 56790c7c822..ae3014f663c 100644 --- a/app/views/katello/api/v2/subscription_facet/base.json.rabl +++ b/app/views/katello/api/v2/subscription_facet/base.json.rabl @@ -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 diff --git a/db/migrate/20240729192228_add_convert2rhel_to_host_facets.rb b/db/migrate/20240729192228_add_convert2rhel_to_host_facets.rb new file mode 100644 index 00000000000..5441b9b2927 --- /dev/null +++ b/db/migrate/20240729192228_add_convert2rhel_to_host_facets.rb @@ -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 diff --git a/test/models/host/subscription_facet_test.rb b/test/models/host/subscription_facet_test.rb index 338680c2592..a169706fba9 100644 --- a/test/models/host/subscription_facet_test.rb +++ b/test/models/host/subscription_facet_test.rb @@ -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