From dc7c10e63dc5f0d582b8f875fdee1d5679eee3a7 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Thu, 9 Jul 2020 11:44:29 -0400 Subject: [PATCH 1/2] Use Host#uid_ems to link host records to ems_events If we are unable to find the host by ems_ref attempt to use the host_uid_ems attribute --- app/models/ems_event.rb | 10 ++++++++++ spec/models/ems_event_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/app/models/ems_event.rb b/app/models/ems_event.rb index f6b94ff9417..ac79a5f2785 100644 --- a/app/models/ems_event.rb +++ b/app/models/ems_event.rb @@ -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.find_by(:ems_id => event[:ems_id], :uid_ems => uid_ems) || Host.find_by(:ems_id => nil, :uid_ems => uid_ems) + 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 = {}) diff --git a/spec/models/ems_event_spec.rb b/spec/models/ems_event_spec.rb index 926b36f8669..31ada2bb94d 100644 --- a/spec/models/ems_event_spec.rb +++ b/spec/models/ems_event_spec.rb @@ -275,6 +275,34 @@ 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 + end end context '.event_groups' do From 78fec932f94df3584ba146149124e4318ca955b1 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Fri, 10 Jul 2020 08:14:38 -0400 Subject: [PATCH 2/2] Optimize query and add spec test for dup uid_ems's --- app/models/ems_event.rb | 2 +- spec/models/ems_event_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/models/ems_event.rb b/app/models/ems_event.rb index ac79a5f2785..24d9e6f301d 100644 --- a/app/models/ems_event.rb +++ b/app/models/ems_event.rb @@ -95,7 +95,7 @@ def self.process_host_in_event!(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.find_by(:ems_id => event[:ems_id], :uid_ems => uid_ems) || Host.find_by(:ems_id => nil, :uid_ems => uid_ems) + 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 diff --git a/spec/models/ems_event_spec.rb b/spec/models/ems_event_spec.rb index 31ada2bb94d..4f7436ce16b 100644 --- a/spec/models/ems_event_spec.rb +++ b/spec/models/ems_event_spec.rb @@ -302,6 +302,16 @@ 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