Skip to content

Commit

Permalink
Fixes #36796 - Make host_facts_updated event visible
Browse files Browse the repository at this point in the history
  • Loading branch information
ofedoren authored and adamruzicka committed Oct 5, 2023
1 parent d3aeb14 commit 001a7ff
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/models/concerns/foreman/observable_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def set_crud_hooks(model_name, namespace: Foreman::Observable::DEFAULT_NAMESPACE
end
end

def register_custom_hook(hook_name, namespace: Foreman::Observable::DEFAULT_NAMESPACE)
event_name = Foreman::Observable.event_name_for(hook_name, namespace: namespace)
self.event_subscription_hooks |= [event_name]
end

def preload_scopes_builder
@preload_scopes_builder ||= Foreman::PreloadScopesBuilder.new(self)
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/host/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Base < ApplicationRecord
include Hostext::Ownership
include Foreman::TelemetryHelper
include Facets::BaseHostExtensions
include Foreman::ObservableModel

self.table_name = :hosts
extend FriendlyId
Expand Down Expand Up @@ -47,6 +48,8 @@ class Base < ApplicationRecord

default_scope -> { where(taxonomy_conditions) }

register_custom_hook :host_facts_updated

def self.taxonomy_conditions
conditions = {}
if Organization.current.nil? && User.current.present? && !User.current.admin?
Expand Down
1 change: 0 additions & 1 deletion app/models/host/managed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class Host::Managed < Host::Base
include HostInfoExtensions
include HostParams
include Facets::ManagedHostExtensions
include Foreman::ObservableModel
include ::ForemanRegister::HostExtensions

has_many :reports, :foreign_key => :host_id, :class_name => 'ConfigReport'
Expand Down
1 change: 1 addition & 0 deletions test/models/hosts/managed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class ManagedTest < ActiveSupport::TestCase
'build_entered.event.foreman',
'build_exited.event.foreman',
'status_changed.event.foreman',
'host_facts_updated.event.foreman',
]

assert_same_elements expected, Host::Managed.event_subscription_hooks
Expand Down
15 changes: 15 additions & 0 deletions test/unit/host_fact_importer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,19 @@ class HostFactImporterTest < ActiveSupport::TestCase
assert_equal 'br_customer', host.primary_interface.identifier
end
end

describe 'events' do
let(:callback) { -> {} }

it 'fires a host_facts_updated event on success import' do
host = FactoryBot.create(:host, :managed)
ActiveSupport::Notifications.subscribed(callback, 'host_facts_updated.event.foreman') do
callback.expects(:call).with do |_name, _started, _finished, _unique_id, payload|
payload[:object].operatingsystem.name == 'CentOS'
end

HostFactImporter.new(host).import_facts(operatingsystemrelease: '6.7', operatingsystem: 'CentOS')
end
end
end
end

0 comments on commit 001a7ff

Please sign in to comment.