Class: NotificationClass

Inherits:
Object
  • Object
show all
Defined in:
lib/miq_utilities/notification.rb

Overview

Wrapper class to notify stakeholders

Instance Method Summary collapse

Instance Method Details

#notify(event_level:, event_message:, email_to: nil, email_from: nil, signature: nil, webhookurl: nil, subjecttype: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/miq_utilities/notification.rb', line 19

def notify(event_level:, event_message:, email_to: nil, email_from: nil, signature: nil, webhookurl: nil, subjecttype: nil)
  @logger = LoggingClass.new(self.class.to_s)
  request_or_vm = $evm.root.attributes['miq_provision_id'] || $evm.root.attributes['vm']
  user = $evm.root.attributes['userid']
  userid = $evm.root['user'].attributes['userid']
  message = "Request ID/VM Name:<#{request_or_vm}>\n User:<#{user}> ID:<#{userid}>\n" + event_message
  signature ||= 'ManageIQ'
  case event_level.upcase
  when 'INFO'
    send_slack_message(message: message, webhookurl: webhookurl)
  when 'WARN', 'WARNING'
    subject = 'ManageIQ Warning'
    send_slack_message(message: message, webhookurl: webhookurl)
    if $evm.nil?
      @logger.log(level: 'warn', message: 'The $evm object is null. Unable to send email notification.')
    else
      send_email(message: message, subject: subject, from_email: email_from, signature: signature, to_email: email_to)
    end
  when 'ERR', 'ERROR'
    subject = 'ManageIQ Error'
    send_slack_message(message: message, webhookurl: webhookurl)
    if $evm.nil?
      @logger.log(level: 'warn', message: 'The $evm object is null. Unable to send email or on-screen notifications.')
    else
      on_screen_message(level: event_level, message: message, type: subjecttype)
      send_email(message: message, subject: subject, from_email: email_from, signature: signature, to_email: email_to)
    end
  when 'NOTE'
    if $evm.nil?
      @logger.log(level: 'warn', message: 'The $evm object is null. Unable to send on-screen notification.')
    else
      on_screen_message(level: event_level, message: message, type: subjecttype)
    end
  end
rescue => err
  raise("<#{err}>")
end

#on_screen_message(level:, message:, subject: nil, type: nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/miq_utilities/notification.rb', line 85

def on_screen_message(level:, message:, subject: nil, type: nil)
  if subject.nil?
    vm = $evm.root['vm']
    prov = $evm.root['miq_provision']
    subject ||= vm unless vm.nil?
    subject ||= prov.miq_request unless prov.nil?
    prov.message = message unless prov.nil?
  end

  levelvalid = %w[info warn error].any? { |msglevel| level.upcase.include? msglevel.to_s.upcase }
  level = 'info' unless levelvalid

  begin
    if type.nil?
      $evm.create_notification(level: level, subject: subject, message: message)
    else
      $evm.create_notification(type: type, subject: subject)
    end
  rescue => err
    raise("\n    Class:<#{self.class}>\n    Method:<#{__method__}>\n    Error:<#{err} - #{err.backtrace.join("\n")}>")
  end
end

#send_email(message:, subject:, from_email:, signature:, to_email: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/miq_utilities/notification.rb', line 57

def send_email(message:, subject:, from_email:, signature:, to_email: nil)
  if to_email.nil?
    user = $evm.root['user']
    to = user.attributes['email']
    # Look at the Event Type in the Current Object or in the Root Object
    event_type = $evm.object['event'] || $evm.root['event_type']
  end
  event_type ||= 'MIQ Event'
  to ||= to_email

  subject += " - #{event_type}"
  body = 'Hello, '
  body += message
  body += 'Thank you,'
  body += signature

  $evm.execute(:send_email, to, from_email, subject, body)
rescue => err
  raise("\n    Class:<#{self.class}>\n    Method:<#{__method__}>\n    Error:<#{err} - #{err.backtrace.join("\n")}>")
end

#send_slack_message(message:, webhookurl:) ⇒ Object



78
79
80
81
82
83
# File 'lib/miq_utilities/notification.rb', line 78

def send_slack_message(message:, webhookurl:)
  notifier = Slack::Notifier.new webhookurl
  notifier.ping message
rescue => err
  raise("\n    Class:<#{self.class}>\n    Method:<#{__method__}>\n    Error:<#{err} - #{err.backtrace.join("\n")}>")
end