File: postgresql_acls_to_resources_hash.rb

package info (click to toggle)
puppet-module-puppetlabs-postgresql 10.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 940 kB
  • sloc: ruby: 731; sh: 66; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,975 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

# @summary This internal function translates the ipv(4|6)acls format into a resource suitable for create_resources.
# @api private
Puppet::Functions.create_function(:'postgresql::postgresql_acls_to_resources_hash') do
  # @param acls
  #   An array of strings that are pg_hba.conf rules.
  # @param id
  #   An identifier that will be included in the namevar to provide uniqueness.
  # @param offset
  #   An order offset, so you can start the order at an arbitrary starting point.
  #
  # @return [Hash]
  #   A hash that can be fed into create_resources to create multiple individual pg_hba_rule resources.
  dispatch :default_impl do
    param 'Array[String]', :acls
    param 'String[1]', :id
    param 'Integer[0]', :offset
  end

  def default_impl(acls, id, offset)
    resources = {}
    acls.each do |acl|
      index = acls.index(acl)

      parts = acl.split

      unless parts.length >= 4
        raise(Puppet::ParseError, "postgresql::postgresql_acls_to_resources_hash(): acl line #{index} does not " \
                                  'have enough parts')
      end

      resource = {
        'type' => parts[0],
        'database' => parts[1],
        'user' => parts[2],
        'order' => '%03d' % (offset + index)
      }
      if parts[0] == 'local'
        resource['auth_method'] = parts[3]
        resource['auth_option'] = parts.last(parts.length - 4).join(' ') if parts.length > 4
      elsif %r{^\d}.match?(parts[4])
        resource['address'] = "#{parts[3]} #{parts[4]}"
        resource['auth_method'] = parts[5]

        resource['auth_option'] = parts.last(parts.length - 6).join(' ') if parts.length > 6
      else
        resource['address'] = parts[3]
        resource['auth_method'] = parts[4]

        resource['auth_option'] = parts.last(parts.length - 5).join(' ') if parts.length > 5
      end
      resources["postgresql class generated rule #{id} #{index}"] = resource
    end
    resources
  end
end