bin/handler-zendesk.rb in sensu-plugins-zendesk-0.0.3 vs bin/handler-zendesk.rb in sensu-plugins-zendesk-1.0.0

- old
+ new

@@ -25,53 +25,52 @@ require 'sensu-handler' require 'zendesk_api' class Zendesk < Sensu::Handler - def handle # rubocop:disable all + def ticket_description + "Sensu Alert\r\n" \ + 'Client: ' + @event['client']['name'] + "\r\n" \ + 'Address: ' + @event['client']['address'] + "\r\n" \ + 'Subscriptions: ' + @event['client']['subscriptions'].join(', ') + "\r\n" \ + 'Check: ' + @event['check']['name'] + "\r\n" \ + 'Output: ' + @event['check']['output'] + "\r\n" + end + + def ticket_tags + tags = [] + unless settings['zendesk']['tags'].nil? + tags << settings['zendesk']['tags'] + end + if settings['zendesk']['subscriptions_to_tags'] + tags << @event['client']['subscriptions'] + end + tags + end + + def ticket_subject + 'Alert - ' + @event['client']['name'] + ' - ' + @event['check']['name'] + end + + def handle client = ZendeskAPI::Client.new do |config| config.url = settings['zendesk']['url'] # Basic / Token Authentication config.username = settings['zendesk']['username'] # Choose one of the following depending on your authentication choice - # #YELOW - unless settings['zendesk']['token'].nil? # rubocop:disable UnlessElse - config.token = settings['zendesk']['token'] - else + if settings['zendesk']['token'].nil? config.password = settings['zendesk']['password'] + else + config.token = settings['zendesk']['token'] end config.retry = true end - def ticket_subject - 'Alert - ' + @event['client']['name'] + ' - ' + @event['check']['name'] - end - - def ticket_description - "Sensu Alert\r\n" \ - 'Client: ' + @event['client']['name'] + "\r\n" \ - 'Address: ' + @event['client']['address'] + "\r\n" \ - 'Subscriptions: ' + @event['client']['subscriptions'].join(', ') + "\r\n" \ - 'Check: ' + @event['check']['name'] + "\r\n" \ - 'Output: ' + @event['check']['output'] + "\r\n" - end - - def ticket_tags - tags = [] - unless settings['zendesk']['tags'].nil? - tags << settings['zendesk']['tags'] - end - if settings['zendesk']['subscriptions_to_tags'] - tags << @event['client']['subscriptions'] - end - tags - end - begin - timeout(60) do + Timeout.timeout(60) do if settings['zendesk']['status_to_use'].include?(@event['check']['status']) ZendeskAPI::Ticket.create( client, subject: ticket_subject, comment: { value: ticket_description }, @@ -80,10 +79,10 @@ type: settings['zendesk']['type'] || 'incident', tags: ticket_tags ) end end - rescue Timeout::Error - puts 'zendesk -- timed out while attempting to create a ticket for #{ticket_subject} --' + rescue Timeout::Error + puts 'zendesk -- timed out while attempting to create a ticket for #{ticket_subject} --' end end end