lib/flapjack/data/notification_rule.rb in flapjack-0.7.20 vs lib/flapjack/data/notification_rule.rb in flapjack-0.7.21

- old
+ new

@@ -2,18 +2,19 @@ require 'oj' require 'active_support/time' require 'ice_cube' require 'flapjack/utility' +require 'flapjack/data/tag_set' module Flapjack module Data class NotificationRule extend Flapjack::Utility - attr_accessor :id, :contact_id, :entities, :entity_tags, :time_restrictions, + attr_accessor :id, :contact_id, :entities, :tags, :time_restrictions, :warning_media, :critical_media, :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 == '') @@ -66,25 +67,29 @@ IceCube::Schedule.from_hash(tr) end def to_json(*args) - self.class.hashify(:id, :contact_id, :entity_tags, :entities, + self.class.hashify(:id, :contact_id, :tags, :entities, :time_restrictions, :warning_media, :critical_media, :warning_blackhole, :critical_blackhole) {|k| [k, self.send(k)] }.to_json end - # tags or entity names match? - # nil @entity_tags and nil @entities matches - def match_entity?(event) - # TODO: return true if event's entity tags match entity tag list on the rule - ((@entity_tags.nil? || @entity_tags.empty?) && (@entities.nil? || @entities.empty?)) || - (@entities.include?(event.split(':').first)) + # entity names match? + def match_entity?(event_id) + return false unless @entities + @entities.include?(event_id.split(':').first) end + # tags match? + def match_tags?(event_tags) + return false unless @tags && @tags.length > 0 + @tags.subset?(event_tags) + end + def blackhole?(severity) ('warning'.eql?(severity.downcase) && @warning_blackhole) || ('critical'.eql?(severity.downcase) && @critical_blackhole) end @@ -97,11 +102,11 @@ end end def is_specific? (!@entities.nil? && !@entities.empty?) || - (!@entity_tags.nil? && !@entity_tags.empty?) + (!@tags.nil? && !@tags.empty?) end private def initialize(rule_data, opts = {}) @@ -118,21 +123,25 @@ logger = options[:logger] # make some assumptions about the incoming data 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 errors = self.validate_data(rule_data, options) return errors unless errors.nil? || errors.empty? # whitelisting fields, rather than passing through submitted data directly + tag_data = rule_data[:tags].is_a?(Set) ? rule_data[: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]), - :entity_tags => Oj.dump(rule_data[:entity_tags]), + :tags => Oj.dump(tag_data), :time_restrictions => Oj.dump(rule_data[:time_restrictions]), :warning_media => Oj.dump(rule_data[:warning_media]), :critical_media => Oj.dump(rule_data[:critical_media]), :warning_blackhole => rule_data[:warning_blackhole], :critical_blackhole => rule_data[:critical_blackhole], @@ -219,24 +228,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.has_key?(:entity_tags) || - ( d[:entity_tags].nil? || - d[:entity_tags].is_a?(Array) && - d[:entity_tags].all? {|et| et.is_a?(String)} ) } => - "entity_tags must be a list of strings", + proc { !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", - #proc { (d.has_key?(:entities) && - # d[:entities].is_a?(Array) && - # (d[:entities].size > 0)) || - # (d.has_key?(:entity_tags) && - # d[:entity_tags].is_a?(Array) && - # (d[:entity_tags].size > 0)) } => - #"entities or entity tags must have at least one value", - proc { !d.has_key?(:time_restrictions) || ( d[:time_restrictions].nil? || d[:time_restrictions].all? {|tr| !!prepare_time_restriction(symbolize(tr)) } ) @@ -282,10 +283,11 @@ def refresh rule_data = @redis.hgetall("notification_rule:#{@id}") @contact_id = rule_data['contact_id'] - @entity_tags = Oj.load(rule_data['entity_tags'] || '') + 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'] || '') @warning_media = Oj.load(rule_data['warning_media'] || '') @critical_media = Oj.load(rule_data['critical_media'] || '') @warning_blackhole = ((rule_data['warning_blackhole'] || 'false').downcase == 'true')