Skip to content

Commit 94b7555

Browse files
committed
refactor: Add support for plain SOQL query
1 parent f8e6274 commit 94b7555

File tree

3 files changed

+15
-38
lines changed

3 files changed

+15
-38
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.4.0
2+
- feat: Add support for plain SOQL query
3+
14
## 3.3.0
25
- feat: Added `timeout` configuration to control RESTForce timeout settings (defaults to `60`)
36
- feat: Added support for reference fields (`parent__r.child`)

lib/logstash/inputs/salesforce.rb

Lines changed: 11 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
#
@@ -85,29 +85,20 @@ class LogStash::Inputs::Salesforce < LogStash::Inputs::Base
8585
# generting a security token, see:
8686
# https://help.salesforce.com/apex/HTViewHelpDoc?id=user_security_token.htm
8787
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 => ""
88+
# Plain SOQL query
89+
config :sfdc_soql_query, :validate => :string, :required => true
9790
# Setting this to true will convert SFDC's NamedFields__c to named_fields__c
9891
config :to_underscores, :validate => :boolean, :default => false
9992

10093
public
10194
def register
10295
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?
96+
@sfdc_fields = get_sfdc_fields
10697
end # def register
10798

10899
public
109100
def run(queue)
110-
results = client.query(get_query())
101+
results = client.query(@sfdc_soql_query)
111102
@logger.debug("Query results:", :results => results)
112103
if results && results.first
113104
results.each do |result|
@@ -162,29 +153,12 @@ def client_options
162153
end
163154

164155
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
156+
def get_sfdc_fields
157+
extracted_fields = sfdc_soql_query.gsub(/SELECT (.+) FROM.+/i, '\1')
158+
extracted_fields_as_array = extracted_fields.split(',').collect { |item| item.strip }
159+
logger.debug("Extracted fields: ", :extracted_fields_as_array => extracted_fields_as_array.to_s)
160+
161+
return extracted_fields_as_array;
188162
end
189163

190164
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)