diff --git a/app/models/ems_event.rb b/app/models/ems_event.rb index 7763a91b9e88..f69180036dbb 100644 --- a/app/models/ems_event.rb +++ b/app/models/ems_event.rb @@ -1,5 +1,6 @@ class EmsEvent < EventStream include_concern 'Automate' + supports :timeline CLONE_TASK_COMPLETE = "CloneVM_Task_Complete" SOURCE_DEST_TASKS = [ @@ -10,6 +11,24 @@ class EmsEvent < EventStream 'Rename_Task', ] + def self.description + "Management Events" + end + + def self.group_names_and_levels + event_groups.each_with_object({}) do |arr, hsh| + hsh["group_names"] ||= {} + hsh["group_names"][arr.first] = arr.second[:name] + + arr.second.keys.each do |level| + level = level.to_s + next if level == 'name' + hsh["group_levels"] ||= {} + hsh["group_levels"][level] ||= level.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..d855f7af809a 100644 --- a/app/models/event_stream.rb +++ b/app/models/event_stream.rb @@ -1,5 +1,6 @@ class EventStream < ApplicationRecord include_concern 'Purging' + include SupportsFeatureMixin serialize :full_data belongs_to :target, :polymorphic => true @@ -35,14 +36,22 @@ class EventStream < ApplicationRecord after_commit :emit_notifications, :on => :create + supports_not :timeline, :reason => "Only subclasses can be on a timeline" + + # 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 +65,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 +92,34 @@ 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.group_and_level_details + {'description' => description}.merge!(group_names_and_levels) + end + + private_class_method def self.group_names_and_levels + raise NotImplementedError, _("group_names_and_levels must be implemented in a subclass") + end + + def self.event_stream_options + EventStream.subclasses.select {|s| s.supports?(:timeline)}.map { |c| [c.name, c.group_and_level_details] }.to_h + end end diff --git a/app/models/miq_event.rb b/app/models/miq_event.rb index 288aeb218c16..f30c88bf08b3 100644 --- a/app/models/miq_event.rb +++ b/app/models/miq_event.rb @@ -1,4 +1,6 @@ class MiqEvent < EventStream + supports :timeline + CHILD_EVENTS = { :assigned_company_tag => { :Host => [:vms_and_templates], @@ -20,6 +22,17 @@ class MiqEvent < EventStream ContainerReplicator, ContainerGroup, ContainerProject, ContainerNode, ContainerImage, PhysicalServer].freeze + def self.description + "Policy Events" + end + + def self.group_names_and_levels + hsh = {} + hsh["group_names"] = MiqEventDefinitionSet.all.map {|s| [s.name, s.description]}.to_h + hsh["group_levels"] =[MiqPolicy::CONDITION_ALLOW, MiqPolicy::CONDITION_DENY].map {|c| [c.downcase, c]}.to_h + hsh + 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..1525692c17d2 100644 --- a/app/models/miq_policy.rb +++ b/app/models/miq_policy.rb @@ -11,6 +11,9 @@ class MiqPolicy < ApplicationRecord PhysicalServer Vm).freeze + CONDITION_ALLOW = "Success" + CONDITION_DENY = "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/custom_button_event_spec.rb b/spec/models/custom_button_event_spec.rb index 1dc653c34980..7df34c14aa98 100644 --- a/spec/models/custom_button_event_spec.rb +++ b/spec/models/custom_button_event_spec.rb @@ -39,4 +39,8 @@ expect(cb_event.button_name).to eq("Test Button") end end + + it "doesn't support timeline" do + expect(described_class.new.supports?(:timeline)).to be_falsey + end end diff --git a/spec/models/custom_event_spec.rb b/spec/models/custom_event_spec.rb new file mode 100644 index 000000000000..e9a012f482e7 --- /dev/null +++ b/spec/models/custom_event_spec.rb @@ -0,0 +1,5 @@ +RSpec.describe CustomEvent do + it "doesn't support timeline" do + expect(described_class.new.supports?(:timeline)).to be_falsey + end +end diff --git a/spec/models/event_stream_spec.rb b/spec/models/event_stream_spec.rb index 3597c0f1ce41..aedeee68c097 100644 --- a/spec/models/event_stream_spec.rb +++ b/spec/models/event_stream_spec.rb @@ -21,4 +21,38 @@ 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 "group_and_level_details" do + it "raises NotImplementedError, for subclasses to implement" do + expect {described_class.group_and_level_details}.to raise_error(NotImplementedError) + end + end + + it "doesn't support timeline" do + expect(described_class.new.supports?(:timeline)).to be_falsey + end + + context "event_stream_options" do + it "works" do + MiqEventDefinitionSet.seed + options = described_class.event_stream_options + expect(options.keys.sort).to eq %w[EmsEvent MiqEvent] + + expect(options["EmsEvent"].keys.sort).to eq %w[description group_levels group_names] + expect(options["EmsEvent"]["description"]).to eq(EmsEvent.description) + expect(options["EmsEvent"]["group_levels"].keys.sort).to eq %w[critical detail warning] + expect(options["EmsEvent"]["group_names"].keys).to include *%i[addition configuration console deletion devices] + + expect(options["MiqEvent"].keys.sort).to eq %w[description group_levels group_names] + expect(options["MiqEvent"]["description"]).to eq(MiqEvent.description) + expect(options["MiqEvent"]["group_levels"].keys.sort).to eq %w[failure success] + expect(options["MiqEvent"]["group_names"].keys).to include *%w[auth_validation authentication compliance container_operations ems_operations evm_operations] + end + end end diff --git a/spec/models/request_event_spec.rb b/spec/models/request_event_spec.rb new file mode 100644 index 000000000000..89f4042a705e --- /dev/null +++ b/spec/models/request_event_spec.rb @@ -0,0 +1,5 @@ +RSpec.describe RequestEvent do + it "doesn't support timeline" do + expect(described_class.new.supports?(:timeline)).to be_falsey + end +end