Class: FlapjackConfigurator::FlapjackConfig

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

Overview

Class representing the overall Flapjack config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, diner, logger) ⇒ FlapjackConfig

Returns a new instance of FlapjackConfig.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flapjack_configurator/flapjack_config.rb', line 14

def initialize(config, diner, logger)
  @config_obj = UserConfiguration.new(config, diner, logger)
  @diner  = diner
  @logger = logger

  # Media will be tied in via the contacts, however pregenerate objects off a single API call for speed.
  media = @diner.media.map { |api_media| FlapjackMedia.new(api_media, @diner, logger) }
  # Also add PagerDuty creds into media.  PD creds are handled separately by the API but can be grouped thanks to our class handling.
  media += @diner.pagerduty_credentials.map { |api_pd| FlapjackPagerduty.new(api_pd, @diner, logger) }

  # Prebuild notification rules for the same reason
  notification_rules = @diner.notification_rules.map { |api_nr| FlapjackNotificationRule.new(nil, api_nr, @diner, logger) }

  @contacts = {}.tap { |ce| @diner.contacts.map { |api_contact| ce[api_contact[:id]] = FlapjackContact.new(nil, api_contact, @diner, logger, media, notification_rules) } }
end

Instance Attribute Details

#config_objObject (readonly)

Returns the value of attribute config_obj.



12
13
14
# File 'lib/flapjack_configurator/flapjack_config.rb', line 12

def config_obj
  @config_obj
end

#contactsObject (readonly)

Returns the value of attribute contacts.



12
13
14
# File 'lib/flapjack_configurator/flapjack_config.rb', line 12

def contacts
  @contacts
end

Instance Method Details

#add_all_entityObject



65
66
67
68
69
70
# File 'lib/flapjack_configurator/flapjack_config.rb', line 65

def add_all_entity
  return false if @diner.entities('ALL')
  @logger.info('Creating the ALL magic entity')
  fail('Failed to create ALL entity') unless @diner.create_entities(id: 'ALL', name: 'ALL')
  return true
end

#update_contactsObject

Loop over the contacts and call update/create/remove methods as needed Builds the @contacts hash



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
# File 'lib/flapjack_configurator/flapjack_config.rb', line 32

def update_contacts
  config_contact_ids = @config_obj.contact_ids
  ret_val = false

  # Iterate over a list of keys to avoid the iterator being impacted by deletes
  @contacts.keys.each do |id|
    if config_contact_ids.include? id
      ret_val = true if @contacts[id].update(@config_obj)

      # Delete the ID from the id array
      # This will result in config_contact_ids being a list of IDs that need to be created at the end of the loop
      config_contact_ids.delete(id)
    else
      # Delete contact from Flapjack
      @contacts[id].delete
      @contacts.delete(id)
      ret_val = true
    end
  end

  # Add new contacts to Flapjack
  config_contact_ids.each do |new_id|
    contact_obj = FlapjackContact.new(new_id, nil, @diner, @logger)
    contact_obj.update(@config_obj)
    @contacts[new_id] = contact_obj
  end

  # Return true if changes made
  return ret_val || config_contact_ids.length > 0
end