Skip to content

Commit

Permalink
Add ems_events for ems_event_filter_column for ems cluster subclasses
Browse files Browse the repository at this point in the history
EmsCluster was properly having ems_cluster_id as the ems_event_filter_column,
subclasses had cluster_id as the column.
  • Loading branch information
jrafanie committed Dec 16, 2024
1 parent d4f76b2 commit e74baad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
11 changes: 11 additions & 0 deletions app/models/ems_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 19 additions & 13 deletions spec/models/mixins/event_mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit e74baad

Please sign in to comment.