From d7a753a4538e91121832a204ed48a53d381bccda Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Wed, 10 Nov 2021 18:08:49 -0500 Subject: [PATCH] Add timeline_options to expose OPTIONS to the API For issue: https://github.com/ManageIQ/manageiq-api/issues/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. --- app/models/ems_event.rb | 19 +++++++++++++++++++ app/models/event_stream.rb | 18 ++++++++++++++++++ app/models/miq_event.rb | 11 +++++++++++ app/models/miq_policy.rb | 3 +++ spec/models/event_stream_spec.rb | 24 ++++++++++++++++++++++++ 5 files changed, 75 insertions(+) diff --git a/app/models/ems_event.rb b/app/models/ems_event.rb index 7763a91b9e88..966c7c188090 100644 --- a/app/models/ems_event.rb +++ b/app/models/ems_event.rb @@ -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 diff --git a/app/models/event_stream.rb b/app/models/event_stream.rb index 1bf14a2488fd..3703fca57d1a 100644 --- a/app/models/event_stream.rb +++ b/app/models/event_stream.rb @@ -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| @@ -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 @@ -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 diff --git a/app/models/miq_event.rb b/app/models/miq_event.rb index 288aeb218c16..91c98f61caa3 100644 --- a/app/models/miq_event.rb +++ b/app/models/miq_event.rb @@ -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. diff --git a/app/models/miq_policy.rb b/app/models/miq_policy.rb index 5dc3a6074838..aea5984d15a2 100644 --- a/app/models/miq_policy.rb +++ b/app/models/miq_policy.rb @@ -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 diff --git a/spec/models/event_stream_spec.rb b/spec/models/event_stream_spec.rb index 3597c0f1ce41..b22838d7f384 100644 --- a/spec/models/event_stream_spec.rb +++ b/spec/models/event_stream_spec.rb @@ -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