aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/configuration_builders/alert_group.rb
blob: 56c446d0bfcff4ba6f18cbf5d6686ff2fbb10c41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# encoding: UTF-8
require 'object_builder'
require 'mauve/notification'
require 'mauve/configuration_builder'
require 'mauve/alert_group'
require 'mauve/notification'

module Mauve
  module ConfigurationBuilders
    #
    # This corresponds to a new "notify" clause an the alert_group config.
    #
    class Notification < ObjectBuilder

      # Sets up the notification
      # 
      # @param [Array] who List of usernames or people_lists to notify
      # @raise [ArgumentError] if a username doesn't exist.
      #
      # @return [Mauve::Notification] New notification instance.
      def builder_setup(*who)
        who = who.map do |username|
          if @context.people[username]
            @context.people[username]

          elsif @context.people_lists[username]
            @context.people_lists[username]

          else
            raise ArgumentError.new("You have not declared who #{username} is")

          end
        end
        @result = Mauve::Notification.new(who, @context.last_alert_group.level)
      end
      
      is_attribute "every"
      is_block_attribute "during"
    end

    # This corresponds to a new alert_group clause
    #
    class AlertGroup < ObjectBuilder

      # Sets up the alert group, and sets the last_alert_group context.
      # 
      # @param [String] name Name of the new alert group
      #
      # @return [Mauve::AlertGroup] New alert group instance
      def builder_setup(name="anonymous_name")
        @result = Mauve::AlertGroup.new(name)
        @context.last_alert_group = @result
      end

      is_block_attribute "includes"
      is_attribute "acknowledgement_time"
      is_attribute "level"
      is_builder "notify", Mauve::ConfigurationBuilders::Notification

      # Method called after the notify clause has been sorted.  Adds new
      # notification clause to the result.
      #
      # @param [Mauve::Notification] notification
      #
      def created_notify(notification)
        @result.notifications ||= []
        @result.notifications << notification
      end

    end

  end

  # These constants define the levels available for alert groups.
  #
  # This should live in AlertGroup but can't due to
  # http://briancarper.net/blog/ruby-instance_eval_constant_scoping_broken
  #
  module AlertGroupConstants
    # Urgent level
    URGENT = :urgent
    # Normal level
    NORMAL = :normal
    # Low level
    LOW    = :low
  end


  class ConfigurationBuilder < ObjectBuilder

    include AlertGroupConstants

    is_builder "alert_group", ConfigurationBuilders::AlertGroup

    # Called after an alert group is created.  Checks to make sure that no more than one alert group shares a name.
    #
    # @param [Mauve::AlertGroup] alert_group The new AlertGroup
    # @raise [ArgumentError] if an AlertGroup with the same name already exists.
    #
    def created_alert_group(alert_group)
      name = alert_group.name
      raise ArgumentError.new("Duplicate alert_group '#{name}'") unless @result.alert_groups.select { |g| g.name == name }.empty?
      @result.alert_groups << alert_group
    end

  end

end