lib/flapjack/data/notification_rule.rb in flapjack-0.7.26 vs lib/flapjack/data/notification_rule.rb in flapjack-0.7.27

- old
+ new

@@ -11,11 +11,12 @@ class NotificationRule extend Flapjack::Utility attr_accessor :id, :contact_id, :entities, :tags, :time_restrictions, - :warning_media, :critical_media, :warning_blackhole, :critical_blackhole + :unknown_media, :warning_media, :critical_media, + :unknown_blackhole, :warning_blackhole, :critical_blackhole def self.exists_with_id?(rule_id, options = {}) raise "Redis connection not set" unless redis = options[:redis] raise "No id value passed" unless not (rule_id.nil? || rule_id == '') logger = options[:logger] @@ -68,12 +69,12 @@ IceCube::Schedule.from_hash(tr) end def to_json(*args) self.class.hashify(:id, :contact_id, :tags, :entities, - :time_restrictions, :warning_media, :critical_media, - :warning_blackhole, :critical_blackhole) {|k| + :time_restrictions, :unknown_media, :warning_media, :critical_media, + :unknown_blackhole, :warning_blackhole, :critical_blackhole) {|k| [k, self.send(k)] }.to_json end # entity names match? @@ -87,16 +88,19 @@ return false unless @tags && @tags.length > 0 @tags.subset?(event_tags) end def blackhole?(severity) - ('warning'.eql?(severity.downcase) && @warning_blackhole) || + ('unknown'.eql?(severity.downcase) && @unknown_blackhole) || + ('warning'.eql?(severity.downcase) && @warning_blackhole) || ('critical'.eql?(severity.downcase) && @critical_blackhole) end def media_for_severity(severity) case severity + when 'unknown' + @unknown_media when 'warning' @warning_media when 'critical' @critical_media end @@ -121,10 +125,11 @@ redis = options[:redis] raise "a redis connection must be supplied" unless redis logger = options[:logger] # make some assumptions about the incoming data + rule_data[:unknown_blackhole] = rule_data[:unknown_blackhole] || false rule_data[:warning_blackhole] = rule_data[:warning_blackhole] || false rule_data[:critical_blackhole] = rule_data[:critical_blackhole] || false if rule_data[:tags].is_a?(Array) rule_data[:tags] = Flapjack::Data::TagSet.new(rule_data[:tags]) end @@ -139,12 +144,14 @@ :id => rule_data[:id].to_s, :contact_id => rule_data[:contact_id].to_s, :entities => Oj.dump(rule_data[:entities]), :tags => Oj.dump(tag_data), :time_restrictions => Oj.dump(rule_data[:time_restrictions]), + :unknown_media => Oj.dump(rule_data[:unknown_media]), :warning_media => Oj.dump(rule_data[:warning_media]), :critical_media => Oj.dump(rule_data[:critical_media]), + :unknown_blackhole => rule_data[:unknown_blackhole], :warning_blackhole => rule_data[:warning_blackhole], :critical_blackhole => rule_data[:critical_blackhole], } logger.debug("NotificationRule#add_or_update json_rule_data: #{json_rule_data.inspect}") if logger @@ -243,10 +250,16 @@ } ) } => "time restrictions are invalid", # TODO should the media types be checked against a whitelist? + proc { !d.has_key?(:unknown_media) || + ( d[:unknown_media].nil? || + d[:unknown_media].is_a?(Array) && + d[:unknown_media].all? {|et| et.is_a?(String)} ) } => + "unknown_media must be a list of strings", + proc { !d.has_key?(:warning_media) || ( d[:warning_media].nil? || d[:warning_media].is_a?(Array) && d[:warning_media].all? {|et| et.is_a?(String)} ) } => "warning_media must be a list of strings", @@ -255,10 +268,14 @@ ( d[:critical_media].nil? || d[:critical_media].is_a?(Array) && d[:critical_media].all? {|et| et.is_a?(String)} ) } => "critical_media must be a list of strings", + proc { !d.has_key?(:unknown_blackhole) || + [TrueClass, FalseClass].include?(d[:unknown_blackhole].class) } => + "unknown_blackhole must be true or false", + proc { !d.has_key?(:warning_blackhole) || [TrueClass, FalseClass].include?(d[:warning_blackhole].class) } => "warning_blackhole must be true or false", proc { !d.has_key?(:critical_blackhole) || @@ -287,11 +304,13 @@ @contact_id = rule_data['contact_id'] tags = Oj.load(rule_data['tags'] || '') @tags = tags ? Flapjack::Data::TagSet.new(tags) : nil @entities = Oj.load(rule_data['entities'] || '') @time_restrictions = Oj.load(rule_data['time_restrictions'] || '') + @unknown_media = Oj.load(rule_data['unknown_media'] || '') @warning_media = Oj.load(rule_data['warning_media'] || '') @critical_media = Oj.load(rule_data['critical_media'] || '') + @unknown_blackhole = ((rule_data['unknown_blackhole'] || 'false').downcase == 'true') @warning_blackhole = ((rule_data['warning_blackhole'] || 'false').downcase == 'true') @critical_blackhole = ((rule_data['critical_blackhole'] || 'false').downcase == 'true') end end