Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #36796 - Make host_facts_updated event visible #9851

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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