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

Use Host#uid_ems to link host records to ems_events #20348

Merged
merged 2 commits into from
Jul 10, 2020
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
10 changes: 10 additions & 0 deletions app/models/ems_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,17 @@ def self.process_vm_in_event!(event, options = {})
end

def self.process_host_in_event!(event, options = {})
uid_ems = event.delete(:host_uid_ems)
process_object_in_event!(Host, event, options)

if event[:host_id].nil? && uid_ems.present?
# Attempt to find a host in the current EMS first, then fallback to archived hosts
host = Host.where(:uid_ems => uid_ems, :ems_id => [event[:ems_id], nil]).order("ems_id NULLS LAST").first
unless host.nil?
event[:host_id] = host.id
event[:host_name] ||= host.name
end
end
end

def self.process_container_entities_in_event!(event, _options = {})
Expand Down
38 changes: 38 additions & 0 deletions spec/models/ems_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,44 @@
end
end
end

context "with a host" do
let(:event) do
{
:ems_id => ems.id,
:event_type => "HostAddEvent",
:host_uid_ems => host.uid_ems
}
end

context "with an active host" do
let(:host) { FactoryBot.create(:host, :uid_ems => "6f3fa3f1-bbe0-4aab-9a69-5d652324357f", :ext_management_system => ems) }

it "should link the event to the host" do
ems_event = described_class.add(ems.id, event)
expect(ems_event.host).to eq(host)
end
end

context "with an archived host" do
let(:host) { FactoryBot.create(:host, :uid_ems => "6f3fa3f1-bbe0-4aab-9a69-5d652324357f") }

it "should link the event to the host" do
ems_event = described_class.add(ems.id, event)
expect(ems_event.host).to eq(host)
end
end

context "with active and archived hosts with the same uid_ems" do
let!(:archived_host) { FactoryBot.create(:host, :uid_ems => "6f3fa3f1-bbe0-4aab-9a69-5d652324357f") }
let!(:host) { FactoryBot.create(:host, :uid_ems => "6f3fa3f1-bbe0-4aab-9a69-5d652324357f", :ext_management_system => ems) }

it "should prefer the active host" do
ems_event = described_class.add(ems.id, event)
expect(ems_event.host).to eq(host)
end
end
end
end

context '.event_groups' do
Expand Down