Class: FlapjackConfigurator::FlapjackContact

Inherits:
FlapjackObjectBase show all
Defined in:
lib/flapjack_configurator/flapjack_contact.rb

Overview

Class representing a Flapjack contact

Instance Attribute Summary collapse

Attributes inherited from FlapjackObjectBase

#config, #obj_exists

Instance Method Summary collapse

Methods inherited from FlapjackObjectBase

#_load_from_api, #_reload_config, #_update, #delete, #id

Constructor Details

#initialize(my_id, current_config, diner, logger, current_media = [], current_notification_rules = []) ⇒ FlapjackContact

Returns a new instance of FlapjackContact.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/flapjack_configurator/flapjack_contact.rb', line 13

def initialize(my_id, current_config, diner, logger, current_media = [], current_notification_rules = [])
  @diner = diner
  @logger = logger
  super(my_id, current_config, diner.method(:contacts), diner.method(:create_contacts), diner.method(:update_contacts), diner.method(:delete_contacts), logger, 'contact')

  # Select our media out from a premade hash of all media built from a single API call
  @media = current_media.select { |m| m.config[:links][:contacts].include? id }

  # Select notification rules the same way.
  @notification_rules = current_notification_rules.select { |m| m.config[:links][:contacts].include? id }
end

Instance Attribute Details

#mediaObject (readonly)

Returns the value of attribute media.



11
12
13
# File 'lib/flapjack_configurator/flapjack_contact.rb', line 11

def media
  @media
end

Instance Method Details

#_create(config) ⇒ Object

Define our own _create as it doesn’t use an ID



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/flapjack_configurator/flapjack_contact.rb', line 36

def _create(config)
  fail("Object #{id} exists") if @obj_exists
  # AFAIK there is not an easy way to convert hash keys to symbols outside of Rails
  config.each { |k, v| @config[k.to_sym] = v }
  @logger.info("Creating contact #{id} with config #{@config}")
  fail "Failed to create contact #{id}" unless @create_method.call([@config])
  _reload_config

  # Creating an entity auto generates notification rules
  # Regenerate the notification rules
  @notification_rules = []
  @config[:links][:notification_rules].each do |nr_id|
    @notification_rules << FlapjackNotificationRule.new(nr_id, nil, @diner, @logger)
  end
end

#update(config_obj) ⇒ Object

Update all the things



26
27
28
29
30
31
32
33
# File 'lib/flapjack_configurator/flapjack_contact.rb', line 26

def update(config_obj)
  ret_val = false
  ret_val = true if update_attributes(config_obj)
  ret_val = true if update_entities(config_obj)
  ret_val = true if update_media(config_obj)
  ret_val = true if update_notification_rules(config_obj)
  return ret_val
end

#update_attributes(config_obj) ⇒ Object

Update attributes from the config, creating the contact if needed (Chef definition of “update”) Does not handle entites or notifications



55
56
57
58
59
60
61
62
63
# File 'lib/flapjack_configurator/flapjack_contact.rb', line 55

def update_attributes(config_obj)
  @logger.debug("Updating attributes for contact #{id}")
  if @obj_exists
    return _update(config_obj.contact_config(id)['details'])
  else
    _create(config_obj.contact_config(id)['details'])
    return true
  end
end

#update_entities(config_obj) ⇒ Object

Update entities for the contact, creating or removing as needed



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/flapjack_configurator/flapjack_contact.rb', line 66

def update_entities(config_obj)
  fail("Contact #{id} doesn't exist yet") unless @obj_exists
  @logger.debug("Updating entities for contact #{id}")

  wanted_entities = config_obj.entity_map.entities_for_contact(id)
  current_entities = @config[:links][:entities]
  ret_val = false

  (wanted_entities - current_entities).each do |entity_id|
    @logger.info("Associating entity #{entity_id} to contact #{id}")
    fail("Failed to add entity #{entity_id} to contact #{id}") unless @diner.update_contacts(id, add_entity: entity_id)
    ret_val = true
  end

  (current_entities - wanted_entities).each do |entity_id|
    @logger.info("Removing entity #{entity_id} from contact #{id}")
    fail("Failed to remove entity #{entity_id} from contact #{id}") unless @diner.update_contacts(id, remove_entity: entity_id)
    ret_val = true
  end

  _reload_config
  return ret_val
end

#update_media(config_obj) ⇒ Object

Update the media for the contact



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/flapjack_configurator/flapjack_contact.rb', line 91

def update_media(config_obj)
  @logger.debug("Updating media for contact #{id}")
  media_config = config_obj.media(id)
  ret_val = false

  media_config_types = media_config.keys
  @media.each do |media_obj|
    if media_config_types.include? media_obj.type
      ret_val = true if media_obj.update(media_config[media_obj.type])
      # Delete the ID from the type array. This will result in media_config_types being a list of types that need to be created at the end of the loop
      media_config_types.delete(media_obj.type)
    else
      media_obj.delete
      ret_val = true
    end
  end

  # Delete outside the loop as deleting inside the loop messes up the each iterator
  @media.delete_if { |obj| !obj.obj_exists }

  media_config_types.each do |type|
    # Pagerduty special case again
    # TODO: Push this back up so that the if isn't done here
    if type == 'pagerduty'
      media_obj = FlapjackPagerduty.new(nil, @diner, @logger)
      media_obj.create(id, media_config[type])
    else
      media_obj = FlapjackMedia.new(nil, @diner, @logger)
      media_obj.create(id, type, media_config[type])
    end
    @media << media_obj
  end

  return ret_val || media_config_types.length > 0
end

#update_notification_rules(config_obj) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/flapjack_configurator/flapjack_contact.rb', line 127

def update_notification_rules(config_obj)
  @logger.debug("Updating notification rules for contact #{id}")
  nr_config = config_obj.notification_rules(id)
  nr_config_ids = nr_config.keys
  ret_val = false

  @notification_rules.each do |nr_obj|
    if nr_config_ids.include? nr_obj.id
      ret_val = true if nr_obj.update(nr_config[nr_obj.id])
      # Delete the ID from the type array. This will result in nr_config_ids being a list of types that need to be created at the end of the loop
      nr_config_ids.delete(nr_obj.id)
    else
      nr_obj.delete
      ret_val = true
    end
  end

  # Delete outside the loop as deleting inside the loop messes up the each iterator
  @notification_rules.delete_if { |obj| !obj.obj_exists }

  nr_config_ids.each do |nr_id|
    nr_obj = FlapjackNotificationRule.new(nr_id, nil, @diner, @logger)
    nr_obj.create(id, nr_config[nr_id])
    @notification_rules << (nr_obj)
  end

  return ret_val || nr_config_ids.length > 0
end