diff --git a/lib/puppet/provider/postgresql_conf/parsed.rb b/lib/puppet/provider/postgresql_conf/parsed.rb deleted file mode 100644 index 787125072c..0000000000 --- a/lib/puppet/provider/postgresql_conf/parsed.rb +++ /dev/null @@ -1,45 +0,0 @@ -# frozen_string_literal: true - -require 'puppet/provider/parsedfile' - -Puppet::Type.type(:postgresql_conf).provide( - :parsed, - parent: Puppet::Provider::ParsedFile, - default_target: '/etc/postgresql.conf', - filetype: :flat, -) do - desc 'Set key/values in postgresql.conf.' - - text_line :comment, match: %r{^\s*#} - text_line :blank, match: %r{^\s*$} - - record_line :parsed, - fields: ['key', 'value', 'comment'], - optional: ['comment'], - match: %r{^\s*([\w.]+)\s*=?\s*(.*?)(?:\s*#\s*(.*))?\s*$}, - to_line: proc { |h| - # simple string and numeric values don't need to be enclosed in quotes - val = if h[:value].is_a?(Numeric) - h[:value].to_s - elsif h[:value].is_a?(Array) - # multiple listen_addresses specified as a string containing a comma-speparated list - h[:value].join(', ') - else - h[:value] - end - dontneedquote = val.match(%r{^(\d+.?\d+|\w+)$}) - dontneedequal = h[:key].match(%r{^(include|include_if_exists)$}i) - - str = h[:key].downcase # normalize case - str += dontneedequal ? ' ' : ' = ' - str += "'" unless dontneedquote && !dontneedequal - str += val - str += "'" unless dontneedquote && !dontneedequal - str += " # #{h[:comment]}" unless h[:comment].nil? || h[:comment] == :absent - str - }, - post_parse: proc { |h| - h[:key].downcase! # normalize case - h[:value].gsub!(%r{(^'|'$)}, '') # strip out quotes - } -end diff --git a/lib/puppet/provider/postgresql_conf/ruby.rb b/lib/puppet/provider/postgresql_conf/ruby.rb new file mode 100644 index 0000000000..f00abdb7f2 --- /dev/null +++ b/lib/puppet/provider/postgresql_conf/ruby.rb @@ -0,0 +1,49 @@ +Puppet::Type.type(:postgresql_conf).provide(:ruby) do + desc 'Set key/values in a postgresql config file.' + + confine exists: target + + # file parsing in a seperate function + def parse_config(target) + file = File.open(target) + active_settings = [] + active_values_regex = %r{^\s*(?[\w.]+)\s*=?\s*(?.*?)(?:\s*#\s*(?.*))?\s*$} + file = File.open('/etc/postgresql/14/main/postgresql.conf') + + File.foreach(file).with_index do |line, line_number| + if matches = line.match(active_values_regex) + attributes_hash = { line_number: line_number, key: matches[:key], ensure: 'present', value: matches[:value], comment: matches[:comment] } + active_settings.push(attributes_hash) + end + end + active_settings + end + + #write config file + def write_config(target, lines) + File.open(target, 'w') { |file| file.write(lines.join) } + end + + # check, if resource exists. + def exists? + @result = parse_config(target).each { |setting| setting[:key] == resource[:key] } + end + + # remove resource + def destroy + + end + + # create resource + def create + end + + # getter - get value + def value + end + + # setter - set value + def value=(_value) + end + end +end diff --git a/lib/puppet/type/postgresql_conf.rb b/lib/puppet/type/postgresql_conf.rb index a624db4bcf..62513225ab 100644 --- a/lib/puppet/type/postgresql_conf.rb +++ b/lib/puppet/type/postgresql_conf.rb @@ -20,6 +20,6 @@ newproperty(:target) do desc 'The path to the postgresql config file' - newvalues(%r{^\/[a-z0-9]+[a-z0-9(\/)(\-)]*[\w]+.conf$}) + newvalues(%r{^/[a-z0-9]+[a-z0-9(/)-]*\w+.conf$}) end end