module AwsCleaner::Webhooks

Public Class Methods

fire_webhook(hook_config, config, instance_id) click to toggle source

call an HTTP endpoint

# 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) click to toggle source

generate the URL for the webhook

# 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