Skip to content

Commit

Permalink
Add event_stream_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 10, 2021
1 parent 83e706c commit de03579
Show file tree
Hide file tree
Showing 8 changed files with 108 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
@@ -1,5 +1,6 @@
class EmsEvent < EventStream
include_concern 'Automate'
supports :timeline

CLONE_TASK_COMPLETE = "CloneVM_Task_Complete"
SOURCE_DEST_TASKS = [
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions app/models/event_stream.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class EventStream < ApplicationRecord
include_concern 'Purging'
include SupportsFeatureMixin
serialize :full_data

belongs_to :target, :polymorphic => true
Expand Down Expand Up @@ -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|
Expand All @@ -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
Expand All @@ -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
13 changes: 13 additions & 0 deletions app/models/miq_event.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class MiqEvent < EventStream
supports :timeline

CHILD_EVENTS = {
:assigned_company_tag => {
:Host => [:vms_and_templates],
Expand All @@ -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.
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_ALLOW = "Success"
CONDITION_DENY = "Failure"

def self.policy_towhat_applies_to_classes
TOWHAT_APPLIES_TO_CLASSES.index_with { |key| ui_lookup(:model => key) }
end
Expand Down
4 changes: 4 additions & 0 deletions spec/models/custom_button_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/models/custom_event_spec.rb
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions spec/models/event_stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/models/request_event_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit de03579

Please sign in to comment.