Class: FlapjackConfigurator::UserConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/flapjack_configurator/user_configuration.rb

Overview

User Configuration: Class representing the desired configuration as passed into the utility

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, diner, logger) ⇒ UserConfiguration

Returns a new instance of UserConfiguration.



11
12
13
14
15
16
17
18
19
# File 'lib/flapjack_configurator/user_configuration.rb', line 11

def initialize(config, diner, logger)
  @config = config
  @logger = logger
  @media_config = {}

  _sanity_check

  @entity_map = EntityMapper.new(self, diner)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/flapjack_configurator/user_configuration.rb', line 9

def config
  @config
end

#entity_mapObject (readonly)

Returns the value of attribute entity_map.



9
10
11
# File 'lib/flapjack_configurator/user_configuration.rb', line 9

def entity_map
  @entity_map
end

Instance Method Details

#_complete_config_merge(contact_id, config_key) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flapjack_configurator/user_configuration.rb', line 59

def _complete_config_merge(contact_id, config_key)
  contact_settings = contact_config(contact_id)[config_key]
  fail("Missing #{config_key} settings for contact #{contact_id}") if contact_settings.nil?

  baseline_opts = baseline_config.key?(config_key) ? baseline_config[config_key] : {}
  contact_defaults = contact_settings.key?('defaults') ? contact_settings['defaults'] : {}

  merged_config = {}
  (contact_settings.keys - %w(defaults)).each do |key|
    # Only merge baseline/defaults if the contact has the setting defined
    # This is to prevent errors from partial configs built from only partial defaults.
    if baseline_opts.key? key
      merged_config[key] = baseline_opts[key].merge(contact_defaults.merge(contact_settings[key]))
    else
      merged_config[key] = contact_defaults.merge(contact_settings[key])
    end
  end

  @logger.debug("#{contact_id} #{config_key} complete config: #{merged_config}")
  return merged_config
end

#_sanity_checkObject



21
22
23
24
25
26
27
28
29
# File 'lib/flapjack_configurator/user_configuration.rb', line 21

def _sanity_check
  # Check that required keys are present
  fail('Config missing contacts block') unless @config.key? 'contacts'
  @config['contacts'].each do |contact_id, contact_val|
    %w(details notification_media notification_rules).each do |contact_opt|
      fail("#{contact_id} contact config missing #{contact_opt} block") unless contact_val.key? contact_opt
    end
  end
end

#baseline_configObject



51
52
53
54
55
56
57
# File 'lib/flapjack_configurator/user_configuration.rb', line 51

def baseline_config
  if @config.key? 'baseline_options'
    return @config['baseline_options']
  else
    return {}
  end
end

#contact_config(contact_id) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/flapjack_configurator/user_configuration.rb', line 35

def contact_config(contact_id)
  return nil unless @config['contacts'].key? contact_id

  # Merge in defaults for keys which may be omitted
  return {
    'entities' => { 'exact' => [], 'regex' => [] },
    'entities_blacklist' => { 'exact' => [], 'regex' => [] }
  }.deep_merge(@config['contacts'][contact_id])
end

#contact_idsObject



31
32
33
# File 'lib/flapjack_configurator/user_configuration.rb', line 31

def contact_ids
  @config['contacts'].keys
end

#default_contactsObject

Return a list of contacts with the default bit set This is pretty entitymapper centric, but it makes more sense here due to current layout.



47
48
49
# File 'lib/flapjack_configurator/user_configuration.rb', line 47

def default_contacts
  @config['contacts'].select { |_, contact| contact.key? 'entities' }.select { |_, c| c['entities']['default'] }.keys
end

#media(contact_id) ⇒ Object



81
82
83
84
85
86
# File 'lib/flapjack_configurator/user_configuration.rb', line 81

def media(contact_id)
  if @media_config[contact_id].nil?
    @media_config[contact_id] = _complete_config_merge(contact_id, 'notification_media')
  end
  return @media_config[contact_id]
end

#notification_rules(contact_id) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/flapjack_configurator/user_configuration.rb', line 88

def notification_rules(contact_id)
  notification_rules = _complete_config_merge(contact_id, 'notification_rules')

  # Double check that the defined rules call for media which exists
  notification_rules.each do |nr_id, nr_val|
    %w(warning_media critical_media unknown_media).each do |alert_type|
      next unless nr_val.key? alert_type
      nr_val[alert_type].each do |alert_media|
        unless media(contact_id).keys.include? alert_media
          @logger.warn("Notification rule #{nr_id} for contact #{contact_id} calls for media #{alert_media} in #{alert_type} which isn't defined for #{contact_id}")
        end
      end
    end
  end

  # The notification rules need to have unique IDs contianing the contact id
  return {}.tap { |rv| notification_rules.each { |nr_id, nr_val| rv["#{contact_id}_#{nr_id}"] = nr_val } }
end