| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
# @summary This internal function translates the ipv(4|6)acls format into a resource suitable for create_resources. |
| 4 |
# @api private |
| 5 |
Puppet::Functions.create_function(:'postgresql::postgresql_acls_to_resources_hash') do |
| 6 |
# @param acls |
| 7 |
# An array of strings that are pg_hba.conf rules. |
| 8 |
# @param id |
| 9 |
# An identifier that will be included in the namevar to provide uniqueness. |
| 10 |
# @param offset |
| 11 |
# An order offset, so you can start the order at an arbitrary starting point. |
| 12 |
# |
| 13 |
# @return [Hash] |
| 14 |
# A hash that can be fed into create_resources to create multiple individual pg_hba_rule resources. |
| 15 |
dispatch :default_impl do |
| 16 |
param 'Array[String]', :acls |
| 17 |
param 'String[1]', :id |
| 18 |
param 'Integer[0]', :offset |
| 19 |
end |
| 20 |
|
| 21 |
def default_impl(acls, id, offset) |
| 22 |
resources = {} |
| 23 |
acls.each do |acl| |
| 24 |
index = acls.index(acl) |
| 25 |
|
| 26 |
parts = acl.split |
| 27 |
|
| 28 |
unless parts.length >= 4 |
| 29 |
raise(Puppet::ParseError, "postgresql::postgresql_acls_to_resources_hash(): acl line #{index} does not " \ |
| 30 |
'have enough parts') |
| 31 |
end |
| 32 |
|
| 33 |
resource = { |
| 34 |
'type' => parts[0], |
| 35 |
'database' => parts[1], |
| 36 |
'user' => parts[2], |
| 37 |
'order' => '%03d' % (offset + index) |
| 38 |
} |
| 39 |
if parts[0] == 'local' |
| 40 |
resource['auth_method'] = parts[3] |
| 41 |
resource['auth_option'] = parts.last(parts.length - 4).join(' ') if parts.length > 4 |
| 42 |
elsif %r{^\d}.match?(parts[4]) |
| 43 |
resource['address'] = "#{parts[3]} #{parts[4]}" |
| 44 |
resource['auth_method'] = parts[5] |
| 45 |
|
| 46 |
resource['auth_option'] = parts.last(parts.length - 6).join(' ') if parts.length > 6 |
| 47 |
else |
| 48 |
resource['address'] = parts[3] |
| 49 |
resource['auth_method'] = parts[4] |
| 50 |
|
| 51 |
resource['auth_option'] = parts.last(parts.length - 5).join(' ') if parts.length > 5 |
| 52 |
end |
| 53 |
resources["postgresql class generated rule #{id} #{index}"] = resource |
| 54 |
end |
| 55 |
resources |
| 56 |
end |
| 57 |
end |