Skip to content

Commit

Permalink
Add timeline_options to expose OPTIONS to the API
Browse files Browse the repository at this point in the history
For issue: ManageIQ/manageiq-api#1090

Note, currently there are several methods in EventStream whose implementation is
for EmsEvent and should move there.  We'll look at that when we need to
expose/test filtering on MiqEvent levels (success/failure) for the API.
  • Loading branch information
jrafanie committed Nov 16, 2021
1 parent 94ed728 commit 97324f8
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/models/ems_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ class EmsEvent < EventStream
'Rename_Task',
]

def self.description
_("Management Events")
end

def self.group_names_and_levels
result = {:description => description}
event_groups.each_with_object(result) do |(group_name, group_details), hash|
hash[:group_names] ||= {}
hash[:group_names][group_name] = group_details[:name]

group_details.each_key do |level|
next if level == :name

hash[:group_levels] ||= {}
hash[:group_levels][level] ||= level.to_s.capitalize
end
end
end

def handle_event
EmsEventHelper.new(self).handle
rescue => err
Expand Down
18 changes: 18 additions & 0 deletions app/models/event_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ class EventStream < ApplicationRecord

after_commit :emit_notifications, :on => :create

# TODO: Consider moving since this is EmsEvent specific. group, group_level and group_name exposed as a virtual columns for reports/api.
GROUP_LEVELS = %i(critical detail warning).freeze

def self.description
raise NotImplementedError, "Description must be implemented in a subclass"
end

def emit_notifications
Notification.emit_for_event(self)
rescue => err
_log.log_backtrace(err)
end

# TODO: Consider moving since this is EmsEvent specific. group, group_level and group_name exposed as a virtual columns for reports/api.
def self.event_groups
core_event_groups = ::Settings.event_handling.event_groups.to_hash
Settings.ems.each_with_object(core_event_groups) do |(_provider_type, provider_settings), event_groups|
Expand All @@ -56,6 +62,7 @@ def self.event_groups
end
end

# TODO: Consider moving since this is EmsEvent specific. group, group_level and group_name exposed as a virtual columns for reports/api.
def self.group_and_level(event_type)
level = :detail # the level is detail as default
egroups = event_groups
Expand All @@ -82,19 +89,30 @@ def self.group_name(group)
group.nil? ? 'Other' : group[:name]
end

# TODO: Consider moving since this is EmsEvent specific. group, group_level and group_name exposed as a virtual columns for reports/api.
def group
return @group unless @group.nil?
@group, @group_level = self.class.group_and_level(event_type)
@group
end

# TODO: Consider moving since this is EmsEvent specific. group, group_level and group_name exposed as a virtual columns for reports/api.
def group_level
return @group_level unless @group_level.nil?
@group, @group_level = self.class.group_and_level(event_type)
@group_level
end

# TODO: Consider moving since this is EmsEvent specific. group, group_level and group_name exposed as a virtual columns for reports/api.
def group_name
@group_name ||= self.class.group_name(group)
end

def self.timeline_classes
EventStream.subclasses.select { |e| e.respond_to?(:group_names_and_levels) }
end

def self.timeline_options
timeline_classes.map { |c| [c.name.to_sym, c.group_names_and_levels] }.to_h
end
end
11 changes: 11 additions & 0 deletions app/models/miq_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ class MiqEvent < EventStream
ContainerReplicator, ContainerGroup, ContainerProject,
ContainerNode, ContainerImage, PhysicalServer].freeze

def self.description
_("Policy Events")
end

def self.group_names_and_levels
hash = {:description => description}
hash[:group_names] = MiqEventDefinitionSet.all.pluck(:name, :description).to_h.symbolize_keys
hash[:group_levels] = [MiqPolicy::CONDITION_SUCCESS, MiqPolicy::CONDITION_FAILURE].index_by { |c| c.downcase.to_sym }
hash
end

def self.raise_evm_event(target, raw_event, inputs = {}, options = {})
# Target may have been deleted if it's a worker
# Target, in that case will be the worker's server.
Expand Down
3 changes: 3 additions & 0 deletions app/models/miq_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class MiqPolicy < ApplicationRecord
PhysicalServer
Vm).freeze

CONDITION_SUCCESS = N_("Success")
CONDITION_FAILURE = N_("Failure")

def self.policy_towhat_applies_to_classes
TOWHAT_APPLIES_TO_CLASSES.index_with { |key| ui_lookup(:model => key) }
end
Expand Down
24 changes: 24 additions & 0 deletions spec/models/event_stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,28 @@
end
end
end

context "description" do
it "raises NotImplementedError, for subclasses to implement" do
expect { described_class.description }.to raise_error(NotImplementedError)
end
end

context "timeline_options" do
it "has correct structure" do
MiqEventDefinitionSet.seed
options = described_class.timeline_options
expect(options.keys.sort).to eq %i[EmsEvent MiqEvent]

expect(options[:EmsEvent].keys.sort).to eq %i[description group_levels group_names]
expect(options[:EmsEvent][:description]).to eq(EmsEvent.description)
expect(options[:EmsEvent][:group_levels].keys.sort).to eq %i[critical detail warning]
expect(options[:EmsEvent][:group_names].keys).to include(*%i[addition configuration console deletion devices])

expect(options[:MiqEvent].keys.sort).to eq %i[description group_levels group_names]
expect(options[:MiqEvent][:description]).to eq(MiqEvent.description)
expect(options[:MiqEvent][:group_levels].keys.sort).to eq %i[failure success]
expect(options[:MiqEvent][:group_names].keys).to include(*%i[auth_validation authentication compliance container_operations ems_operations evm_operations])
end
end
end

0 comments on commit 97324f8

Please sign in to comment.