lib/flapjack/data/notification_rule.rb in flapjack-0.8.9 vs lib/flapjack/data/notification_rule.rb in flapjack-0.8.10
- old
+ new
@@ -10,11 +10,11 @@
module Data
class NotificationRule
extend Flapjack::Utility
- attr_accessor :id, :contact_id, :entities, :tags, :regex_tags,
+ attr_accessor :id, :contact_id, :entities, :regex_entities, :tags, :regex_tags,
:time_restrictions, :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]
@@ -72,11 +72,11 @@
IceCube::Schedule.from_hash(tr)
end
def to_json(*args)
- self.class.hashify(:id, :contact_id, :tags, :regex_tags, :entities,
+ self.class.hashify(:id, :contact_id, :tags, :regex_tags, :entities, :regex_entities,
:time_restrictions, :unknown_media, :warning_media, :critical_media,
:unknown_blackhole, :warning_blackhole, :critical_blackhole) {|k|
[k, self.send(k)]
}.to_json
end
@@ -85,10 +85,21 @@
def match_entity?(event_id)
return false unless @entities
@entities.include?(event_id.split(':').first)
end
+ # entity names match regex?
+ def match_regex_entities?(event_id)
+ return false unless @regex_entities && @regex_entities.length > 0
+ entity = event_id.split(':').first
+ matches = 0
+ @regex_entities.each do |regex_entity|
+ matches += 1 if /#{regex_entity}/ === entity
+ end
+ matches >= @regex_entities.length
+ end
+
# tags match?
def match_tags?(event_tags)
return false unless @tags && @tags.length > 0
@tags.subset?(event_tags)
end
@@ -122,10 +133,11 @@
end
end
def is_specific?
(!@entities.nil? && !@entities.empty?) ||
+ (!@regex_entities.nil? && !@regex_entities.empty?) ||
(!@tags.nil? && !@tags.empty?) ||
(!@regex_tags.nil? && !@regex_tags.empty?)
end
private
@@ -170,10 +182,11 @@
regex_tag_data = rule_data[:regex_tags].is_a?(Set) ? rule_data[:regex_tags].to_a : nil
json_rule_data = {
:id => rule_data[:id].to_s,
:contact_id => rule_data[:contact_id].to_s,
:entities => Oj.dump(rule_data[:entities]),
+ :regex_entities => Oj.dump(rule_data[:regex_entities]),
:tags => Oj.dump(tag_data),
:regex_tags => Oj.dump(regex_tag_data),
:time_restrictions => Oj.dump(rule_data[:time_restrictions], :mode => :compat),
:unknown_media => Oj.dump(rule_data[:unknown_media]),
:warning_media => Oj.dump(rule_data[:warning_media]),
@@ -260,10 +273,16 @@
( d[:entities].nil? ||
d[:entities].is_a?(Array) &&
d[:entities].all? {|e| e.is_a?(String)} ) } =>
"entities must be a list of strings",
+ proc {|d| !d.has_key?(:regex_entities) ||
+ ( d[:regex_entities].nil? ||
+ d[:regex_entities].is_a?(Array) &&
+ d[:regex_entities].all? {|e| e.is_a?(String)} ) } =>
+ "regex_entities must be a list of strings",
+
proc {|d| !d.has_key?(:tags) ||
( d[:tags].nil? ||
d[:tags].is_a?(Flapjack::Data::TagSet) &&
d[:tags].all? {|et| et.is_a?(String)} ) } =>
"tags must be a tag_set of strings",
@@ -343,9 +362,10 @@
tags = Oj.load(rule_data['tags'] || '')
@tags = tags ? Flapjack::Data::TagSet.new(tags) : nil
regex_tags = Oj.load(rule_data['regex_tags'] || '')
@regex_tags = regex_tags ? Flapjack::Data::TagSet.new(regex_tags) : nil
@entities = Oj.load(rule_data['entities'] || '')
+ @regex_entities = Oj.load(rule_data['regex_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')