From 549d581d95920762e3516c3ab50a70914e4a819a Mon Sep 17 00:00:00 2001 From: Pier-Hugues Pellerin Date: Mon, 2 May 2016 16:27:41 -0400 Subject: [PATCH] Updating this plugin to the v2.0 api --- .travis.yml | 10 +++-- Gemfile | 6 ++- lib/logstash/filters/xml.rb | 12 ++--- logstash-filter-xml.gemspec | 2 +- spec/filters/xml_spec.rb | 89 ++++++++++++++++++------------------- 5 files changed, 61 insertions(+), 58 deletions(-) diff --git a/.travis.yml b/.travis.yml index 350c4eb..6b858e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,11 @@ +--- sudo: false language: ruby cache: bundler rvm: - - jruby-1.7.23 -script: - - bundle exec rspec spec +- jruby-1.7.25 +script: +- bundle exec rspec spec +jdk: oraclejdk8 +before_install: +- git clone -b feature/event_interface https://github.com/elastic/logstash diff --git a/Gemfile b/Gemfile index d926697..c781dcb 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,6 @@ source 'https://rubygems.org' -gemspec \ No newline at end of file +gemspec +gem "logstash-core", :path => "./logstash/logstash-core" +gem "logstash-core-plugin-api", :path => "./logstash/logstash-core-plugin-api" +gem "logstash-core-event-java", :path => "./logstash/logstash-core-event-java" +gem "logstash-devutils", :github => "elastic/logstash-devutils", :branch => "feature/plugin-api-2_0" diff --git a/lib/logstash/filters/xml.rb b/lib/logstash/filters/xml.rb index e960bc9..cd1d3fd 100644 --- a/lib/logstash/filters/xml.rb +++ b/lib/logstash/filters/xml.rb @@ -99,7 +99,7 @@ def filter(event) @logger.debug? && @logger.debug("Running xml filter", :event => event) - value = event[@source] + value = event.get(@source) return unless value if value.is_a?(Array) @@ -149,13 +149,9 @@ def filter(event) # the array should probably be created once, filled in the loop and set at after the loop but the return # statement above screws this strategy and is likely a bug anyway so I will not touch this until I can # deep a big deeper and verify there is a sufficient test harness to refactor this. - data = event[xpath_dest] || [] + data = event.get(xpath_dest) || [] data << value.to_s - event[xpath_dest] = data - - # do not use the following construct to set the event, we cannot assume anymore that the field values are in-place mutable - # event[xpath_dest] ||= [] - # event[xpath_dest] << value.to_s + event.set(xpath_dest, data) end end end @@ -163,7 +159,7 @@ def filter(event) if @store_xml begin - event[@target] = XmlSimple.xml_in(value, "ForceArray" => @force_array) + event.set(@target, XmlSimple.xml_in(value, "ForceArray" => @force_array)) matched = true rescue => e event.tag(XMLPARSEFAILURE_TAG) diff --git a/logstash-filter-xml.gemspec b/logstash-filter-xml.gemspec index 17ed62a..22988aa 100644 --- a/logstash-filter-xml.gemspec +++ b/logstash-filter-xml.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |s| s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" } # Gem dependencies - s.add_runtime_dependency "logstash-core-plugin-api", "~> 1.0" + s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0" s.add_runtime_dependency 'nokogiri' s.add_runtime_dependency 'xml-simple' diff --git a/spec/filters/xml_spec.rb b/spec/filters/xml_spec.rb index b1dfcc1..af68c7f 100644 --- a/spec/filters/xml_spec.rb +++ b/spec/filters/xml_spec.rb @@ -15,55 +15,55 @@ CONFIG sample("raw" => '') do - insist { subject["tags"] }.nil? - insist { subject["data"]} == {"key" => "value"} + insist { subject.get("tags") }.nil? + insist { subject.get("data")} == {"key" => "value"} end #From parse xml with array as a value sample("raw" => 'value1value2') do - insist { subject["tags"] }.nil? - insist { subject["data"]} == {"key" => ["value1", "value2"]} + insist { subject.get("tags") }.nil? + insist { subject.get("data")} == {"key" => ["value1", "value2"]} end #From parse xml with hash as a value sample("raw" => 'value') do - insist { subject["tags"] }.nil? - insist { subject["data"]} == {"key1" => [{"key2" => ["value"]}]} + insist { subject.get("tags") }.nil? + insist { subject.get("data")} == {"key1" => [{"key2" => ["value"]}]} end # parse xml in single item array sample("raw" => [""]) do - insist { subject["tags"] }.nil? - insist { subject["data"]} == {"bar" => "baz"} + insist { subject.get("tags") }.nil? + insist { subject.get("data")} == {"bar" => "baz"} end # fail in multi items array sample("raw" => ["", "jojoba"]) do - insist { subject["tags"] }.include?("_xmlparsefailure") - insist { subject["data"]} == nil + insist { subject.get("tags") }.include?("_xmlparsefailure") + insist { subject.get("data")} == nil end # fail in empty array sample("raw" => []) do - insist { subject["tags"] }.include?("_xmlparsefailure") - insist { subject["data"]} == nil + insist { subject.get("tags") }.include?("_xmlparsefailure") + insist { subject.get("data")} == nil end # fail for non string field sample("raw" => {"foo" => "bar"}) do - insist { subject["tags"] }.include?("_xmlparsefailure") - insist { subject["data"]} == nil + insist { subject.get("tags") }.include?("_xmlparsefailure") + insist { subject.get("data")} == nil end # fail for non string single item array sample("raw" => [{"foo" => "bar"}]) do - insist { subject["tags"] }.include?("_xmlparsefailure") - insist { subject["data"]} == nil + insist { subject.get("tags") }.include?("_xmlparsefailure") + insist { subject.get("data")} == nil end #From bad xml sample("raw" => ' '') do - insist { subject["tags"] }.nil? - insist { subject["data"]} == nil + insist { subject.get("tags") }.nil? + insist { subject.get("data")} == nil end end @@ -97,14 +97,14 @@ # Single value sample("raw" => 'value') do - insist { subject["tags"] }.nil? - insist { subject["xpath_field"]} == ["value"] + insist { subject.get("tags") }.nil? + insist { subject.get("xpath_field")} == ["value"] end #Multiple values sample("raw" => 'value1value2') do - insist { subject["tags"] }.nil? - insist { subject["xpath_field"]} == ["value1","value2"] + insist { subject.get("tags") }.nil? + insist { subject.get("xpath_field")} == ["value1","value2"] end end @@ -121,25 +121,25 @@ CONFIG sample("xmldata" => '') do - insist { subject["tags"] }.nil? - insist { subject["data"]} == {"key" => "value"} + insist { subject.get("tags") }.nil? + insist { subject.get("data") } == {"key" => "value"} end #From parse xml with array as a value sample("xmldata" => 'value1value2') do - insist { subject["tags"] }.nil? - insist { subject["data"]} == {"key" => ["value1", "value2"]} + insist { subject.get("tags") }.nil? + insist { subject.get("data") } == {"key" => ["value1", "value2"]} end #From parse xml with hash as a value sample("xmldata" => 'value') do - insist { subject["tags"] }.nil? - insist { subject["data"]} == {"key1" => [{"key2" => ["value"]}]} + insist { subject.get("tags") }.nil? + insist { subject.get("data") } == {"key1" => [{"key2" => ["value"]}]} end #From bad xml sample("xmldata" => ' '') do - insist { subject["tags"] }.nil? - insist { subject["data"]} == nil + insist { subject.get("tags") }.nil? + insist { subject.get("data")} == nil end end @@ -173,14 +173,14 @@ # Single value sample("xmldata" => 'value') do - insist { subject["tags"] }.nil? - insist { subject["xpath_field"]} == ["value"] + insist { subject.get("tags") }.nil? + insist { subject.get("xpath_field") } == ["value"] end #Multiple values sample("xmldata" => 'value1value2') do - insist { subject["tags"] }.nil? - insist { subject["xpath_field"]} == ["value1","value2"] + insist { subject.get("tags") }.nil? + insist { subject.get("xpath_field") } == ["value1","value2"] end end @@ -197,8 +197,8 @@ # Single value sample("xmldata" => 'Français') do - insist { subject["tags"] }.nil? - insist { subject["xpath_field"]} == ["Français"] + insist { subject.get("tags") }.nil? + insist { subject.get("xpath_field")} == ["Français"] end end @@ -215,7 +215,7 @@ # Single value sample("xmldata" => 'Content') do - insist { subject["xpath_field"] } == ["Content"] + insist { subject.get("xpath_field") } == ["Content"] end end @@ -233,7 +233,7 @@ # Single value sample("xmldata" => 'Content') do - insist { subject["xpath_field"] } == ["Content"] + insist { subject.get("xpath_field") } == ["Content"] end end @@ -251,7 +251,7 @@ # Single value sample("xmldata" => 'Content') do - insist { subject["xpath_field"] } == ["Content"] + insist { subject.get("xpath_field") } == ["Content"] end end @@ -268,7 +268,7 @@ # Single value sample("xmldata" => 'Content') do - insist { subject["xpath_field"] } == ["
Content
"] + insist { subject.get("xpath_field") } == ["
Content
"] end end @@ -285,7 +285,7 @@ # Single value sample("xmldata" => 'Content') do - insist { subject["parseddata"] } == { "bar" => ["Content"] } + insist { subject.get("parseddata") } == { "bar" => ["Content"] } end end @@ -302,8 +302,7 @@ # Single value sample("xmldata" => 'Content') do - insist { subject["parseddata"] } == { "bar" => "Content" } + insist { subject.get("parseddata") } == { "bar" => "Content" } end end - end