Skip to content

Commit

Permalink
Merge pull request #16040 from gtanzillo/alert-severity
Browse files Browse the repository at this point in the history
Add severity to alert definitions
  • Loading branch information
Fryguy authored Oct 5, 2017
2 parents 6b3bfda + 80a6aa1 commit 60d57f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
8 changes: 6 additions & 2 deletions app/models/miq_alert.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class MiqAlert < ApplicationRecord
include UuidMixin

SEVERITIES = [nil, "info", "warning", "error"]

serialize :miq_expression
serialize :hash_expression
serialize :options
Expand All @@ -9,6 +11,7 @@ class MiqAlert < ApplicationRecord
validates_uniqueness_of :description, :guid
validate :validate_automate_expressions
validate :validate_single_expression
validates :severity, :inclusion => { :in => SEVERITIES }

has_many :miq_alert_statuses, :dependent => :destroy
before_save :set_responds_to_events
Expand Down Expand Up @@ -235,12 +238,13 @@ def evaluate(target, inputs = {})
end

def add_status_post_evaluate(target, result, event)
status_description, severity, url, ems_ref, resolved = event.try(:parse_event_metadata)
status_description, event_severity, url, ems_ref, resolved = event.try(:parse_event_metadata)
status = miq_alert_statuses.find_or_initialize_by(:resource => target, :event_ems_ref => ems_ref)
status.result = result
status.ems_id = target.try(:ems_id)
status.description = status_description || description
status.severity = severity unless severity.blank?
status.severity = severity
status.severity = event_severity unless event_severity.blank?
status.url = url unless url.blank?
status.event_ems_ref = ems_ref unless ems_ref.blank?
status.resolved = resolved
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/task_helpers/exports/alerts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"hash_expression" => nil,
"responds_to_events" => nil,
"enabled" => true,
"read_only" => nil
"read_only" => nil,
"severity" => nil
}
}
]
Expand Down
20 changes: 20 additions & 0 deletions spec/models/miq_alert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@
expect(mas.severity).to eq('warning')
end

it "miq_alert_status.severity = MiqAlert#severity if ems_event.full_data.severity not present" do
@alert.severity = "error"
@alert.evaluate(
[@vm.class.base_class.name, @vm.id],
:ems_event => FactoryGirl.create(:ems_event, :full_data => {})
)
mas = @alert.miq_alert_statuses.where(:resource_type => @vm.class.base_class.name, :resource_id => @vm.id).first
expect(mas.severity).to eq('error')
end

it "miq_alert_status.severity = ems_event.full_data.severity if present and MiqAlert#severity is also present" do
@alert.severity = "error"
@alert.evaluate(
[@vm.class.base_class.name, @vm.id],
:ems_event => FactoryGirl.create(:ems_event, :full_data => {:severity => 'info'})
)
mas = @alert.miq_alert_statuses.where(:resource_type => @vm.class.base_class.name, :resource_id => @vm.id).first
expect(mas.severity).to eq('info')
end

it "miq_alert_status.severity = nil if ems_event.full_data.severity not present" do
@alert.evaluate([@vm.class.base_class.name, @vm.id])
mas = @alert.miq_alert_statuses.where(:resource_type => @vm.class.base_class.name, :resource_id => @vm.id).first
Expand Down

0 comments on commit 60d57f3

Please sign in to comment.