From d084cfe971498b0ea42f543c65fd0739cfed7a82 Mon Sep 17 00:00:00 2001 From: Johan De Wit Date: Wed, 17 May 2023 14:05:22 +0200 Subject: [PATCH] makes extracor type idempotent in version 4+ --- .../provider/graylog_extractor/graylog_api.rb | 39 ++++++++++++------- lib/puppet/type/graylog_extractor.rb | 33 +++++++++------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/lib/puppet/provider/graylog_extractor/graylog_api.rb b/lib/puppet/provider/graylog_extractor/graylog_api.rb index d756ad7..6ab2523 100644 --- a/lib/puppet/provider/graylog_extractor/graylog_api.rb +++ b/lib/puppet/provider/graylog_extractor/graylog_api.rb @@ -5,16 +5,16 @@ EXTRACTOR_TYPES = { copy_input: 'COPY_INPUT', grok: 'GROK', - json: 'JSON', + json: 'JSON', regex: 'REGEX', regex_replace: 'REGEX_REPLACE', split_and_index: 'SPLIT_AND_INDEX', - substring: 'SUBSTRING' + substring: 'SUBSTRING' } mk_resource_methods - - def self.instances + + def self.instances results = get('system/inputs') input_list = results['inputs'] @@ -22,30 +22,43 @@ def self.instances results = get("system/inputs/#{input_data['id']}/extractors") extractors = results['extractors'].map do |data| + if major_version < 4 + _type = data['extractor_type'] + else + _type = data['type'] + end extractor = new( ensure: :present, input: input_data['title'], name: data['title'], - cut_or_copy: data['cut_or_copy'], + cut_or_copy: if data['cut_or_copy'].nil? + :copy + else + data['cut_or_copy'].to_sym + end, source_field: data['source_field'], target_field: data['target_field'], - type: data['extractor_type'], + type: _type, configuration: data['extractor_config'], - converters: data['converters'], + converters: if data['converters'] == [] + {} + else + data['converters'] + end, condition_type: data['condition_type'], condition_value: data['condition_value'], order: data['order'] ) extractor.rest_id = data['id'] extractor - end + end - acc.concat(extractors) + acc.concat(extractors) end extractors end - + def flush input_rest_id = get_input_rest_id(resource[:input]) @@ -54,7 +67,7 @@ def flush cut_or_copy: resource[:cut_or_copy], source_field: resource[:source_field], target_field: resource[:target_field], - extractor_type: resource[:type], + extractor_type: resource[:type].downcase, extractor_config: resource[:configuration], condition_type: resource[:condition_type], condition_value: resource[:condition_value], @@ -73,7 +86,7 @@ def get_input_rest_id(name) id_list = results['inputs'] .select {|data|data['title'] == name} .map {|data|data['id']} - + if id_list.length == 0 raise "Input #{name} doesn't exist" end @@ -81,4 +94,4 @@ def get_input_rest_id(name) id_list.first end -end \ No newline at end of file +end diff --git a/lib/puppet/type/graylog_extractor.rb b/lib/puppet/type/graylog_extractor.rb index b3bfed6..a002d2f 100644 --- a/lib/puppet/type/graylog_extractor.rb +++ b/lib/puppet/type/graylog_extractor.rb @@ -30,28 +30,31 @@ end newproperty(:input) do - desc 'Title of the input this extractor is attached to.' - isrequired + desc 'Title of the input this extractor is attached to.' + isrequired end newproperty(:type) do - desc 'The type of the Extractor. Must be the Java enum constant for the extractor, such as REGEX' - isrequired + desc 'The type of the Extractor. Must be the Java enum constant for the extractor, such as REGEX' + isrequired + munge do |value| + value.downcase + end end newproperty(:source_field) do - desc 'Source field' - isrequired + desc 'Source field' + isrequired end newproperty(:target_field) do - desc 'Choose a field name to store the extracted value. It can only contain alphanumeric characters and underscores. Example: http_response_code.' - isrequired + desc 'Choose a field name to store the extracted value. It can only contain alphanumeric characters and underscores. Example: http_response_code.' + isrequired end newproperty(:configuration) do - desc 'A hash of configuration values for the extractor; structure varies by extractor type.' - isrequired + desc 'A hash of configuration values for the extractor; structure varies by extractor type.' + isrequired end newproperty(:cut_or_copy) do @@ -61,14 +64,14 @@ end newproperty(:condition_type) do - desc 'Extracting only from messages that match a certain condition helps you avoiding wrong or unnecessary extractions and can also save CPU resources.' - newvalues(:none, :regex, :string) - defaultto(:none) + desc 'Extracting only from messages that match a certain condition helps you avoiding wrong or unnecessary extractions and can also save CPU resources.' + newvalues(:none, :regex, :string) + defaultto(:none) end newproperty(:condition_value) do - desc 'Condition value' - defaultto('') + desc 'Condition value' + defaultto('') end newproperty(:converters) do