diff --git a/app/models/ems_cluster.rb b/app/models/ems_cluster.rb index 9666cd76df0..ad8d4b9c3b1 100644 --- a/app/models/ems_cluster.rb +++ b/app/models/ems_cluster.rb @@ -182,6 +182,17 @@ def parent_datacenter detect_ancestor(:of_type => 'EmsFolder') { |a| a.kind_of?(Datacenter) } end + has_many :ems_events, + ->(cluster) { + where("ems_cluster_id" => cluster.id) + # TODO: event_where_clause tacks on additional possibly expensive queries + # such as all events for all hosts or vms in the cluster + #.or(where("host_id" => host_ids)). + #or(where("dest_host_id" => host_ids)). + #or(where("vm_or_template_id" => vm_or_template_ids)). + #or(where("dest_vm_or_template_id" => vm_or_template_ids)) + }, + :class_name => "EmsEvent" def event_where_clause(assoc = :ems_events) return ["ems_cluster_id = ?", id] if assoc.to_sym == :policy_events diff --git a/spec/models/mixins/event_mixin_spec.rb b/spec/models/mixins/event_mixin_spec.rb index 95ac5b2bf50..b84dbd1b299 100644 --- a/spec/models/mixins/event_mixin_spec.rb +++ b/spec/models/mixins/event_mixin_spec.rb @@ -58,20 +58,26 @@ def event_where_clause(assoc) VmOrTemplate vm_or_template_id Vm vm_or_template_id ].each_slice(2) do |klass, column| - it "#{klass} uses #{column} and target_id and target_type" do - obj = FactoryBot.create(klass.tableize.singularize) - expect(obj.event_stream_filters["EmsEvent"]).to eq(column => obj.id) - expect(obj.event_stream_filters.dig("MiqEvent", "target_id")).to eq(obj.id) - expect(obj.event_stream_filters.dig("MiqEvent", "target_type")).to eq(obj.class.base_class.name) - end - - it "#{klass} behaves like event_where_clause for ems_events" do - obj = FactoryBot.create(klass.tableize.singularize) - event = FactoryBot.create(:event_stream, column => obj.id) - FactoryBot.create(:event_stream) - expect(EventStream.where(obj.event_stream_filters["EmsEvent"]).to_a).to eq([event]) - expect(EventStream.where(obj.event_where_clause(:ems_events)).to_a).to eq([event]) + klasses = klass.constantize.descendants.collect(&:name).unshift(klass) + klasses.each do |klass| + it "#{klass} uses #{column} and target_id and target_type" do + begin + obj = FactoryBot.build(klass.tableize.singularize, :name => "test") + rescue NameError + skip "Unable to build factory from name: #{klass}, possibly due to inflections" + end + expect(obj.event_stream_filters["EmsEvent"]).to eq(column => obj.id) + expect(obj.event_stream_filters.dig("MiqEvent", "target_id")).to eq(obj.id) + expect(obj.event_stream_filters.dig("MiqEvent", "target_type")).to eq(obj.class.base_class.name) + end end + it "#{klass} behaves like event_where_clause for ems_events" do + obj = FactoryBot.create(klass.tableize.singularize) + event = FactoryBot.create(:event_stream, column => obj.id) + FactoryBot.create(:event_stream) + expect(EventStream.where(obj.event_stream_filters["EmsEvent"]).to_a).to eq([event]) + expect(EventStream.where(obj.event_where_clause(:ems_events)).to_a).to eq([event]) + end # # TODO: some classes don't have this implemented or don't have columns for this # # Do we consolidate policy events and miq events?