Puppet Function: postgresql::postgresql_acls_to_resources_hash

Defined in:
lib/puppet/functions/postgresql/postgresql_acls_to_resources_hash.rb
Function type:
Ruby 4.x API

Summary

This internal function translates the ipv(4|6)acls format into a resource suitable for create_resources.

Overview

postgresql::postgresql_acls_to_resources_hash(Array[String] $acls, String[1] $id, Integer[0] $offset)Hash

Parameters:

  • acls (Array[String])

    An array of strings that are pg_hba.conf rules.

  • id (String[1])

    An identifier that will be included in the namevar to provide uniqueness.

  • offset (Integer[0])

    An order offset, so you can start the order at an arbitrary starting point.

Returns:

  • (Hash)

    A hash that can be fed into create_resources to create multiple individual pg_hba_rule resources.



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
# File 'lib/puppet/functions/postgresql/postgresql_acls_to_resources_hash.rb', line 5

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