Module: AwsCleaner::Webhooks

Defined in:
lib/aws-cleaner.rb

Class Method Summary collapse

Class Method Details

.fire_webhook(hook_config, config, instance_id) ⇒ Object

call an HTTP endpoint



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/aws-cleaner.rb', line 154

def self.fire_webhook(hook_config, config, instance_id)
  # generate templated URL
  if hook_config[:template_variables] && hook_config[:url] =~ /\{\S+\}/
    url = AwsCleaner::Webhooks.generate_template(
      hook_config[:url],
      hook_config[:template_variables][:method],
      hook_config[:template_variables][:variable],
      config,
      instance_id
    )
    return false unless url
  else
    url = hook_config[:url]
  end

  hook = { method: hook_config[:method].to_sym, url: url }
  begin
    RestClient::Request.execute(hook)
  rescue RestClient::ExceptionWithResponse
    return false
  end
  # notify chat when webhook is successful
  if hook_config[:chat][:enable]
    msg = AwsCleaner::Webhooks.generate_template(
      hook_config[:chat][:message],
      hook_config[:chat][:method],
      hook_config[:chat][:variable],
      config,
      instance_id
    )
    AwsCleaner::Notify.notify_chat(msg, config)
    return true
  end
end

.generate_template(item, template_variable_method, template_variable, config, instance_id) ⇒ Object

generate the URL for the webhook



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/aws-cleaner.rb', line 137

def self.generate_template(item, template_variable_method, template_variable, config, instance_id)
  if template_variable_method == 'get_chef_fqdn'
    replacement = AwsCleaner::Chef.get_chef_fqdn(instance_id, config)
  elsif template_variable_method == 'get_chef_node_name'
    replacement = AwsCleaner::Chef.get_chef_node_name(instance_id, config)
  else
    raise 'Unknown templating method'
  end
  replaced_item = item.gsub(/{#{template_variable}}/, replacement)
rescue StandardError => e
  puts "Error generating template: #{e.message}"
  return false
else
  replaced_item
end