Skip to content

Commit 02d7002

Browse files
Merge pull request #1163 from alexjfisher/finish_postgresql_acls_to_resources_hash_conversion
Finish conversion of `postgresql_acls_to_resources_hash` function
2 parents 368b48d + face6c3 commit 02d7002

File tree

7 files changed

+180
-376
lines changed

7 files changed

+180
-376
lines changed

REFERENCE.md

Lines changed: 8 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,18 @@ _Private Classes_
6161

6262
**Functions**
6363

64+
_Public Functions_
65+
6466
* [`postgresql::default`](#postgresqldefault): This function pull default values from the `params` class or `globals` class if the value is not present in `params`.
65-
* [`postgresql::postgresql_acls_to_resources_hash`](#postgresqlpostgresql_acls_to_resources_hash): This internal function translates the ipv(4|6)acls format into a resource
6667
* [`postgresql::postgresql_escape`](#postgresqlpostgresql_escape): This function safely escapes a string using a consistent random tag
6768
* [`postgresql::postgresql_password`](#postgresqlpostgresql_password): This function returns the postgresql password hash from the clear text username / password
68-
* [`postgresql_acls_to_resources_hash`](#postgresql_acls_to_resources_hash): This internal function translates the ipv(4|6)acls format into a resource suitable for create_resources. It is not intended to be used outsid
6969
* [`postgresql_escape`](#postgresql_escape): This function safely escapes a string using a consistent random tag
7070
* [`postgresql_password`](#postgresql_password): DEPRECATED. Use the namespaced function [`postgresql::postgresql_password`](#postgresqlpostgresql_password) instead.
7171

72+
_Private Functions_
73+
74+
* `postgresql::postgresql_acls_to_resources_hash`: This internal function translates the ipv(4|6)acls format into a resource suitable for create_resources.
75+
7276
**Tasks**
7377

7478
* [`sql`](#sql): Allows you to execute arbitary SQL
@@ -842,15 +846,15 @@ Default value: $postgresql::params::ip_mask_allow_all_users
842846

843847
##### `ipv4acls`
844848

845-
Data type: `Any`
849+
Data type: `Array[String[1]]`
846850

847851
Lists strings for access control for connection method, users, databases, IPv4 addresses;
848852

849853
Default value: $postgresql::params::ipv4acls
850854

851855
##### `ipv6acls`
852856

853-
Data type: `Any`
857+
Data type: `Array[String[1]]`
854858

855859
Lists strings for access control for connection method, users, databases, IPv6 addresses.
856860

@@ -2779,69 +2783,6 @@ Data type: `String`
27792783

27802784

27812785

2782-
### postgresql::postgresql_acls_to_resources_hash
2783-
2784-
Type: Ruby 4.x API
2785-
2786-
This is an autogenerated function, ported from the original legacy version.
2787-
It /should work/ as is, but will not have all the benefits of the modern
2788-
function API. You should see the function docs to learn how to add function
2789-
signatures for type safety and to document this function using puppet-strings.
2790-
2791-
https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
2792-
2793-
---- original file header ----
2794-
postgresql_acls_to_resources_hash.rb
2795-
---- original file header ----
2796-
2797-
suitable for create_resources. It is not intended to be used outside of the
2798-
postgresql internal classes/defined resources.
2799-
2800-
@return This function accepts an array of strings that are pg_hba.conf rules. It
2801-
will return a hash that can be fed into create_resources to create multiple
2802-
individual pg_hba_rule resources.
2803-
2804-
The second parameter is an identifier that will be included in the namevar
2805-
to provide uniqueness. It must be a string.
2806-
2807-
The third parameter is an order offset, so you can start the order at an
2808-
arbitrary starting point.
2809-
2810-
#### `postgresql::postgresql_acls_to_resources_hash(Any *$args)`
2811-
2812-
This is an autogenerated function, ported from the original legacy version.
2813-
It /should work/ as is, but will not have all the benefits of the modern
2814-
function API. You should see the function docs to learn how to add function
2815-
signatures for type safety and to document this function using puppet-strings.
2816-
2817-
https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
2818-
2819-
---- original file header ----
2820-
postgresql_acls_to_resources_hash.rb
2821-
---- original file header ----
2822-
2823-
suitable for create_resources. It is not intended to be used outside of the
2824-
postgresql internal classes/defined resources.
2825-
2826-
@return This function accepts an array of strings that are pg_hba.conf rules. It
2827-
will return a hash that can be fed into create_resources to create multiple
2828-
individual pg_hba_rule resources.
2829-
2830-
The second parameter is an identifier that will be included in the namevar
2831-
to provide uniqueness. It must be a string.
2832-
2833-
The third parameter is an order offset, so you can start the order at an
2834-
arbitrary starting point.
2835-
2836-
Returns: `Data type` Describe what the function returns here
2837-
2838-
##### `*args`
2839-
2840-
Data type: `Any`
2841-
2842-
The original array of arguments. Port this to individually managed params
2843-
to get the full benefit of the modern function API.
2844-
28452786
### postgresql::postgresql_escape
28462787

28472788
Type: Ruby 4.x API
@@ -2891,40 +2832,6 @@ Data type: `Variant[String[1],Integer]`
28912832

28922833
The clear text `password`
28932834

2894-
### postgresql_acls_to_resources_hash
2895-
2896-
Type: Ruby 3.x API
2897-
2898-
This internal function translates the ipv(4|6)acls format into a resource
2899-
suitable for create_resources. It is not intended to be used outside of the
2900-
postgresql internal classes/defined resources.
2901-
2902-
will return a hash that can be fed into create_resources to create multiple
2903-
individual pg_hba_rule resources.
2904-
2905-
The second parameter is an identifier that will be included in the namevar
2906-
to provide uniqueness. It must be a string.
2907-
2908-
The third parameter is an order offset, so you can start the order at an
2909-
arbitrary starting point.
2910-
2911-
#### `postgresql_acls_to_resources_hash()`
2912-
2913-
This internal function translates the ipv(4|6)acls format into a resource
2914-
suitable for create_resources. It is not intended to be used outside of the
2915-
postgresql internal classes/defined resources.
2916-
2917-
will return a hash that can be fed into create_resources to create multiple
2918-
individual pg_hba_rule resources.
2919-
2920-
The second parameter is an identifier that will be included in the namevar
2921-
to provide uniqueness. It must be a string.
2922-
2923-
The third parameter is an order offset, so you can start the order at an
2924-
arbitrary starting point.
2925-
2926-
Returns: `Any` This function accepts an array of strings that are pg_hba.conf rules. It
2927-
29282835
### postgresql_escape
29292836

29302837
Type: Ruby 3.x API

lib/puppet/functions/postgresql/postgresql_acls_to_resources_hash.rb

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,38 @@
1-
# This is an autogenerated function, ported from the original legacy version.
2-
# It /should work/ as is, but will not have all the benefits of the modern
3-
# function API. You should see the function docs to learn how to add function
4-
# signatures for type safety and to document this function using puppet-strings.
5-
#
6-
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
7-
#
8-
# ---- original file header ----
9-
# postgresql_acls_to_resources_hash.rb
10-
# ---- original file header ----
11-
#
12-
# @summary
13-
# This internal function translates the ipv(4|6)acls format into a resource
14-
# suitable for create_resources. It is not intended to be used outside of the
15-
# postgresql internal classes/defined resources.
16-
#
17-
# @return This function accepts an array of strings that are pg_hba.conf rules. It
18-
# will return a hash that can be fed into create_resources to create multiple
19-
# individual pg_hba_rule resources.
20-
#
21-
# The second parameter is an identifier that will be included in the namevar
22-
# to provide uniqueness. It must be a string.
23-
#
24-
# The third parameter is an order offset, so you can start the order at an
25-
# arbitrary starting point.
26-
#
27-
#
1+
# @summary This internal function translates the ipv(4|6)acls format into a resource suitable for create_resources.
2+
# @api private
283
Puppet::Functions.create_function(:'postgresql::postgresql_acls_to_resources_hash') do
29-
# @param args
30-
# The original array of arguments. Port this to individually managed params
31-
# to get the full benefit of the modern function API.
32-
#
33-
# @return [Data type]
34-
# Describe what the function returns here
4+
# @param acls
5+
# An array of strings that are pg_hba.conf rules.
6+
# @param id
7+
# An identifier that will be included in the namevar to provide uniqueness.
8+
# @param offset
9+
# An order offset, so you can start the order at an arbitrary starting point.
3510
#
11+
# @return [Hash]
12+
# A hash that can be fed into create_resources to create multiple individual pg_hba_rule resources.
3613
dispatch :default_impl do
37-
# Call the method named 'default_impl' when this is matched
38-
# Port this to match individual params for better type safety
39-
repeated_param 'Any', :args
14+
param 'Array[String]', :acls
15+
param 'String[1]', :id
16+
param 'Integer[0]', :offset
4017
end
4118

42-
def default_impl(*args)
43-
func_name = 'postgresql_acls_to_resources_hash()'
44-
45-
if args.size != 3
46-
raise(Puppet::ParseError, "#{func_name}: Wrong number of arguments " \
47-
"given (#{args.size} for 3)")
48-
end
49-
50-
acls = args[0]
51-
raise(Puppet::ParseError, "#{func_name}: first argument must be an array") \
52-
unless acls.instance_of? Array
53-
54-
id = args[1]
55-
raise(Puppet::ParseError, "#{func_name}: second argument must be a string") \
56-
unless id.instance_of? String
57-
58-
offset = args[2].to_i
59-
raise(Puppet::ParseError, "#{func_name}: third argument must be a number") \
60-
unless offset.is_a? Integer
61-
19+
def default_impl(acls, id, offset)
6220
resources = {}
6321
acls.each do |acl|
6422
index = acls.index(acl)
6523

6624
parts = acl.split
6725

6826
unless parts.length >= 4
69-
raise(Puppet::ParseError, "#{func_name}: acl line #{index} does not " \
27+
raise(Puppet::ParseError, "postgresql::postgresql_acls_to_resources_hash(): acl line #{index} does not " \
7028
'have enough parts')
7129
end
7230

7331
resource = {
74-
'type' => parts[0],
32+
'type' => parts[0],
7533
'database' => parts[1],
76-
'user' => parts[2],
77-
'order' => format('%03d', offset + index), # rubocop:disable Style/FormatString
34+
'user' => parts[2],
35+
'order' => '%03d' % (offset + index),
7836
}
7937
if parts[0] == 'local'
8038
resource['auth_method'] = parts[3]

lib/puppet/parser/functions/postgresql_acls_to_resources_hash.rb

Lines changed: 0 additions & 75 deletions
This file was deleted.

manifests/server.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@
100100
$port = $postgresql::params::port,
101101
$ip_mask_deny_postgres_user = $postgresql::params::ip_mask_deny_postgres_user,
102102
$ip_mask_allow_all_users = $postgresql::params::ip_mask_allow_all_users,
103-
$ipv4acls = $postgresql::params::ipv4acls,
104-
$ipv6acls = $postgresql::params::ipv6acls,
103+
Array[String[1]] $ipv4acls = $postgresql::params::ipv4acls,
104+
Array[String[1]] $ipv6acls = $postgresql::params::ipv6acls,
105105

106106
$initdb_path = $postgresql::params::initdb_path,
107107
$createdb_path = $postgresql::params::createdb_path,

manifests/server/config.pp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,16 @@
8888
}
8989
}
9090

91-
# ipv4acls are passed as an array of rule strings, here we transform
92-
# them into a resources hash, and pass the result to create_resources
93-
$ipv4acl_resources = postgresql_acls_to_resources_hash($ipv4acls,
94-
'ipv4acls', 10)
95-
create_resources('postgresql::server::pg_hba_rule', $ipv4acl_resources)
96-
97-
98-
# ipv6acls are passed as an array of rule strings, here we transform
99-
# them into a resources hash, and pass the result to create_resources
100-
$ipv6acl_resources = postgresql_acls_to_resources_hash($ipv6acls,
101-
'ipv6acls', 102)
102-
create_resources('postgresql::server::pg_hba_rule', $ipv6acl_resources)
91+
# $ipv4acls and $ipv6acls are arrays of rule strings
92+
# They are converted into hashes we can iterate over to create postgresql::server::pg_hba_rule resources.
93+
(
94+
postgresql::postgresql_acls_to_resources_hash($ipv4acls, 'ipv4acls', 10) +
95+
postgresql::postgresql_acls_to_resources_hash($ipv6acls, 'ipv6acls', 102)
96+
).each | String $key, Hash $attrs| {
97+
postgresql::server::pg_hba_rule { $key:
98+
* => $attrs,
99+
}
100+
}
103101
}
104102

105103
if $listen_addresses {

0 commit comments

Comments
 (0)