Skip to content

makes extracor type idempotent in version 4+ #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions lib/puppet/provider/graylog_extractor/graylog_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,60 @@
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']

extractors = input_list.reduce([]) do |acc, input_data|
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])

Expand All @@ -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],
Expand All @@ -73,12 +86,12 @@ 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

id_list.first
end

end
end
33 changes: 18 additions & 15 deletions lib/puppet/type/graylog_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down