lib/flapjack/data/notification_rule.rb in flapjack-0.7.16 vs lib/flapjack/data/notification_rule.rb in flapjack-0.7.17
- old
+ new
@@ -35,19 +35,21 @@
# replacing save! etc
def self.add(rule_data, options = {})
raise "Redis connection not set" unless redis = options[:redis]
rule_id = SecureRandom.uuid
- self.add_or_update(rule_data.merge(:id => rule_id), options)
+ errors = self.add_or_update(rule_data.merge(:id => rule_id), options)
+ return errors unless errors.nil? || errors.empty?
self.find_by_id(rule_id, :redis => redis)
end
def update(rule_data, opts = {})
- return false unless self.class.add_or_update({:contact_id => @contact_id}.merge(rule_data.merge(:id => @id)),
+ errors = self.class.add_or_update({:contact_id => @contact_id}.merge(rule_data.merge(:id => @id)),
:redis => @redis, :logger => opts[:logger])
+ return errors unless errors.nil? || errors.empty?
refresh
- true
+ nil
end
# NB: ice_cube doesn't have much rule data validation, and has
# problems with infinite loops if the data can't logically match; see
# https://github.com/seejohnrun/ice_cube/issues/127 &
@@ -117,12 +119,14 @@
# 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
- return unless self.validate_data(rule_data, options)
+ errors = self.validate_data(rule_data, options)
+ return errors unless errors.nil? || errors.empty?
+
# whitelisting fields, rather than passing through submitted data directly
json_rule_data = {
:id => rule_data[:id].to_s,
:contact_id => rule_data[:contact_id].to_s,
:entities => Yajl::Encoder.encode(rule_data[:entities]),
@@ -137,11 +141,11 @@
redis.sadd("contact_notification_rules:#{json_rule_data[:contact_id]}",
json_rule_data[:id])
redis.hmset("notification_rule:#{json_rule_data[:id]}",
*json_rule_data.flatten)
- true
+ nil
end
def self.prepare_time_restriction(time_restriction, timezone = nil)
# this will hand back a 'deep' copy
tr = symbolize(time_restriction)
@@ -264,17 +268,17 @@
errors = validations.keys.inject([]) {|ret,vk|
ret << "Rule #{validations[vk]}" unless vk.call
ret
}
- return true if errors.empty?
+ return if errors.empty?
if logger = options[:logger]
error_str = errors.join(", ")
logger.info "validation error: #{error_str}"
logger.debug "rule failing validations: #{d.inspect}"
end
- false
+ errors
end
def refresh
rule_data = @redis.hgetall("notification_rule:#{@id}")