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

Overview

postgresql::postgresql_acls_to_resources_hash(Any *$args)Data type

This is an autogenerated function, ported from the original legacy version. It /should work/ as is, but will not have all the benefits of the modern function API. You should see the function docs to learn how to add function signatures for type safety and to document this function using puppet-strings.

puppet.com/docs/puppet/latest/custom_functions_ruby.html

—- original file header —- postgresql_acls_to_resources_hash.rb —- original file header —-

suitable for create_resources. It is not intended to be used outside of the
postgresql internal classes/defined resources.

@return This function accepts an array of strings that are pg_hba.conf rules. It
will return a hash that can be fed into create_resources to create multiple
individual pg_hba_rule resources.

The second parameter is an identifier that will be included in the namevar
to provide uniqueness. It must be a string.

The third parameter is an order offset, so you can start the order at an
arbitrary starting point.

Parameters:

  • *args (Any)

    The original array of arguments. Port this to individually managed params to get the full benefit of the modern function API.

Returns:

  • (Data type)

    Describe what the function returns here



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/puppet/functions/postgresql/postgresql_acls_to_resources_hash.rb', line 28

Puppet::Functions.create_function(:'postgresql::postgresql_acls_to_resources_hash') do
  # @param args
  #   The original array of arguments. Port this to individually managed params
  #   to get the full benefit of the modern function API.
  #
  # @return [Data type]
  #   Describe what the function returns here
  #
  dispatch :default_impl do
    # Call the method named 'default_impl' when this is matched
    # Port this to match individual params for better type safety
    repeated_param 'Any', :args
  end

  def default_impl(*args)
    func_name = 'postgresql_acls_to_resources_hash()'

    if args.size != 3
      raise(Puppet::ParseError, "#{func_name}: Wrong number of arguments " \
        "given (#{args.size} for 3)")
    end

    acls = args[0]
    raise(Puppet::ParseError, "#{func_name}: first argument must be an array") \
      unless acls.instance_of? Array

    id = args[1]
    raise(Puppet::ParseError, "#{func_name}: second argument must be a string") \
      unless id.instance_of? String

    offset = args[2].to_i
    raise(Puppet::ParseError, "#{func_name}: third argument must be a number") \
      unless offset.is_a? Integer

    resources = {}
    acls.each do |acl|
      index = acls.index(acl)

      parts = acl.split

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

      resource = {
        'type' => parts[0],
        'database' => parts[1],
        'user' => parts[2],
        'order' => format('%03d', offset + index), # rubocop:disable Style/FormatString
      }
      if parts[0] == 'local'
        resource['auth_method'] = parts[3]
        if parts.length > 4
          resource['auth_option'] = parts.last(parts.length - 4).join(' ')
        end
      elsif parts[4] =~ %r{^\d}
        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