Skip to content

Commit 63013e0

Browse files
committed
refactor: Add support for plain SOQL query
feat: Include deleted records
1 parent f8e6274 commit 63013e0

File tree

3 files changed

+26
-38
lines changed

3 files changed

+26
-38
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 4.0.0
2+
- refactor: Change behavior to support SOQL queries
3+
- feat: Include deleted records
4+
5+
## 3.4.0
6+
- feat: Add support for plain SOQL query
7+
18
## 3.3.0
29
- feat: Added `timeout` configuration to control RESTForce timeout settings (defaults to `60`)
310
- feat: Added support for reference fields (`parent__r.child`)

lib/logstash/inputs/salesforce.rb

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# username => '[email protected]'
3434
# password => 'super-secret'
3535
# security_token => 'SECURITY TOKEN FOR THIS USER'
36-
# sfdc_object_name => 'Opportunity'
36+
# sfdc_soql_query => 'SELECT Id FROM Account'
3737
# }
3838
# }
3939
#
@@ -58,6 +58,8 @@ class LogStash::Inputs::Salesforce < LogStash::Inputs::Base
5858
# Set this to true to connect to a sandbox sfdc instance
5959
# logging in through test.salesforce.com
6060
config :use_test_sandbox, :validate => :boolean, :default => false
61+
# Include deleted records
62+
config :include_deleted, :validate => :boolean, :default => false
6163
# Set this to the instance url of the sfdc instance you want
6264
# to connect to already during login. If you have configured
6365
# a MyDomain in your sfdc instance you would provide
@@ -85,29 +87,25 @@ class LogStash::Inputs::Salesforce < LogStash::Inputs::Base
8587
# generting a security token, see:
8688
# https://help.salesforce.com/apex/HTViewHelpDoc?id=user_security_token.htm
8789
config :security_token, :validate => :string, :required => true
88-
# The name of the salesforce object you are creating or updating
89-
config :sfdc_object_name, :validate => :string, :required => true
90-
# These are the field names to return in the Salesforce query
91-
# If this is empty, all fields are returned.
92-
config :sfdc_fields, :validate => :array, :default => []
93-
# These options will be added to the WHERE clause in the
94-
# SOQL statement. Additional fields can be filtered on by
95-
# adding field1 = value1 AND field2 = value2 AND...
96-
config :sfdc_filters, :validate => :string, :default => ""
90+
# Plain SOQL query
91+
config :sfdc_soql_query, :validate => :string, :required => true
9792
# Setting this to true will convert SFDC's NamedFields__c to named_fields__c
9893
config :to_underscores, :validate => :boolean, :default => false
9994

10095
public
10196
def register
10297
require 'restforce'
103-
obj_desc = client.describe(@sfdc_object_name)
104-
@sfdc_field_types = get_field_types(obj_desc)
105-
@sfdc_fields = get_all_fields if @sfdc_fields.empty?
98+
@sfdc_fields = get_sfdc_fields
10699
end # def register
107100

108101
public
109102
def run(queue)
110-
results = client.query(get_query())
103+
if @include_deleted
104+
results = client.query_all(@sfdc_soql_query)
105+
else
106+
results = client.query(@sfdc_soql_query)
107+
end
108+
111109
@logger.debug("Query results:", :results => results)
112110
if results && results.first
113111
results.each do |result|
@@ -162,29 +160,12 @@ def client_options
162160
end
163161

164162
private
165-
def get_query()
166-
query = ["SELECT",@sfdc_fields.join(','),
167-
"FROM",@sfdc_object_name]
168-
query << ["WHERE",@sfdc_filters] unless @sfdc_filters.empty?
169-
query << "ORDER BY LastModifiedDate DESC" if @sfdc_fields.include?('LastModifiedDate')
170-
query_str = query.flatten.join(" ")
171-
@logger.debug? && @logger.debug("SFDC Query", :query => query_str)
172-
return query_str
173-
end
174-
175-
private
176-
def get_field_types(obj_desc)
177-
field_types = {}
178-
obj_desc.fields.each do |f|
179-
field_types[f.name] = f.type
180-
end
181-
@logger.debug? && @logger.debug("Field types", :field_types => field_types.to_s)
182-
return field_types
183-
end
184-
185-
private
186-
def get_all_fields
187-
return @sfdc_field_types.keys
163+
def get_sfdc_fields
164+
extracted_fields = sfdc_soql_query.gsub(/SELECT (.+) FROM.+/i, '\1')
165+
extracted_fields_as_array = extracted_fields.split(',').collect { |item| item.strip }
166+
@logger.debug("Extracted fields: ", :extracted_fields_as_array => extracted_fields_as_array.to_s)
167+
168+
return extracted_fields_as_array;
188169
end
189170

190171
private

logstash-input-salesforce.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'logstash-input-salesforce'
3-
s.version = '3.3.0'
3+
s.version = '4.0.0'
44
s.licenses = ['Apache-2.0']
55
s.summary = "Creates events based on a Salesforce SOQL query"
66
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"

0 commit comments

Comments
 (0)