Class: LogStash::Inputs::Salesforce

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/inputs/salesforce.rb

Overview

This Logstash input plugin allows you to query Salesforce using SOQL and puts the results into Logstash, one row per event. You can configure it to pull entire sObjects or only specific fields.

NOTE: This input plugin will stop after all the results of the query are processed and will need to be re-run to fetch new results. It does not utilize the streaming API.

In order to use this plugin, you will need to create a new SFDC Application using oauth. More details can be found here: help.salesforce.com/apex/HTViewHelpDoc?id=connected_app_create.htm

You will also need a username, password, and security token for your salesforce instance. More details for generating a token can be found here: help.salesforce.com/apex/HTViewHelpDoc?id=user_security_token.htm

In addition to specifying an sObject, you can also supply a list of API fields that will be used in the SOQL query.

Example

This example prints all the Salesforce Opportunities to standard out

source,ruby

input {

salesforce {
  client_id => 'OAUTH CLIENT ID FROM YOUR SFDC APP'
  client_secret => 'OAUTH CLIENT SECRET FROM YOUR SFDC APP'
  username => '[email protected]'
  password => 'super-secret'
  security_token => 'SECURITY TOKEN FOR THIS USER'
  sfdc_object_name => 'Opportunity'
}

}

output {

stdout {
  codec => rubydebug
}

}


Instance Method Summary collapse

Instance Method Details

#registerObject



99
100
101
102
103
104
# File 'lib/logstash/inputs/salesforce.rb', line 99

def register
  require 'restforce'
  obj_desc = client.describe(@sfdc_object_name)
  @sfdc_field_types = get_field_types(obj_desc)
  @sfdc_fields = get_all_fields if @sfdc_fields.empty?
end

#run(queue) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/logstash/inputs/salesforce.rb', line 107

def run(queue)
  results = client.query(get_query())
  if results && results.first
    results.each do |result|
      event = LogStash::Event.new()
      decorate(event)
      @sfdc_fields.each do |field|
        field_type = @sfdc_field_types[field]
        value = result.send(field)
        event_key = @to_underscores ? underscore(field) : field
        if not value.nil?
          case field_type
          when 'datetime', 'date'
            event.set(event_key, format_time(value))
          else
            event.set(event_key, value)
          end
        end
      end
      queue << event
    end
  end
end