Skip to content

Commit 700d2c5

Browse files
committed
Finish conversion of postgresql_password function
In #1129 the 3 functions from this module were ported to the modern function API and namespaced. None of the boilerplate was fixed up, tests weren't updated, documentation wasn't updated etc. This commit finishes the work for the `postgresql_password` function. * Remove old API `puppet/parser/functions` version. * Create a replacement non-namespaced, (but deprecated), new API shim that calls the namespaced version. * Replace argument validation in the function code with data-type based validation for the individual function parameters. * Update the README and REFERENCE.md files. * Test both shim and namespaced version of the function.
1 parent 84df848 commit 700d2c5

File tree

9 files changed

+121
-131
lines changed

9 files changed

+121
-131
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class { 'postgresql::server':
8181
8282
postgresql::server::db { 'mydatabasename':
8383
user => 'mydatabaseuser',
84-
password => postgresql_password('mydatabaseuser', 'mypassword'),
84+
password => postgresql::postgresql_password('mydatabaseuser', 'mypassword'),
8585
}
8686
```
8787

@@ -94,7 +94,7 @@ class { 'postgresql::server':
9494
}
9595
9696
postgresql::server::role { 'marmot':
97-
password_hash => postgresql_password('marmot', 'mypasswd'),
97+
password_hash => postgresql::postgresql_password('marmot', 'mypasswd'),
9898
}
9999
100100
postgresql::server::database_grant { 'test1':

REFERENCE.md

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ _Private Classes_
6767
* [`postgresql::postgresql_password`](#postgresqlpostgresql_password): This function returns the postgresql password hash from the clear text username / password
6868
* [`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
70-
* [`postgresql_password`](#postgresql_password): This function returns the postgresql password hash from the clear text username / password
70+
* [`postgresql_password`](#postgresql_password): DEPRECATED. Use the namespaced function [`postgresql::postgresql_password`](#postgresqlpostgresql_password) instead.
7171

7272
**Tasks**
7373

@@ -1097,6 +1097,14 @@ Sets PostgreSQL version
10971097

10981098
Default value: `undef`
10991099

1100+
##### `extra_systemd_config`
1101+
1102+
Data type: `Any`
1103+
1104+
Adds extra config to systemd config file, can for instance be used to add extra openfiles. This can be a multi line string
1105+
1106+
Default value: $postgresql::params::extra_systemd_config
1107+
11001108
##### `manage_selinux`
11011109

11021110
Data type: `Boolean`
@@ -1667,6 +1675,22 @@ Specifies whether to grant or revoke the privilege. Default is to grant the priv
16671675

16681676
Default value: 'present'
16691677

1678+
##### `group`
1679+
1680+
Data type: `String`
1681+
1682+
Sets the OS group to run psql
1683+
1684+
Default value: $postgresql::server::group
1685+
1686+
##### `psql_path`
1687+
1688+
Data type: `String`
1689+
1690+
Sets the path to psql command
1691+
1692+
Default value: $postgresql::server::psql_path
1693+
16701694
##### `object_arguments`
16711695

16721696
Data type: `Array[String[1],0]`
@@ -2181,6 +2205,38 @@ Specify whether to create or drop the role. Specifying 'present' creates the rol
21812205

21822206
Default value: 'present'
21832207

2208+
##### `psql_user`
2209+
2210+
Data type: `Any`
2211+
2212+
Sets the OS user to run psql
2213+
2214+
Default value: $postgresql::server::user
2215+
2216+
##### `psql_group`
2217+
2218+
Data type: `Any`
2219+
2220+
Sets the OS group to run psql
2221+
2222+
Default value: $postgresql::server::group
2223+
2224+
##### `psql_path`
2225+
2226+
Data type: `Any`
2227+
2228+
Sets path to psql command
2229+
2230+
Default value: $postgresql::server::psql_path
2231+
2232+
##### `module_workdir`
2233+
2234+
Data type: `Any`
2235+
2236+
Specifies working directory under which the psql command should be executed. May need to specify if '/tmp' is on volume mounted with noexec option.
2237+
2238+
Default value: $postgresql::server::module_workdir
2239+
21842240
### postgresql::server::schema
21852241

21862242
Create a new schema.
@@ -2815,26 +2871,25 @@ to get the full benefit of the modern function API.
28152871

28162872
Type: Ruby 4.x API
28172873

2818-
postgresql_password.rb
2819-
---- original file header ----
2874+
This function returns the postgresql password hash from the clear text username / password
28202875

2821-
@return Returns the postgresql password hash from the clear text username / password.
2876+
#### `postgresql::postgresql_password(Variant[String[1],Integer] $username, Variant[String[1],Integer] $password)`
28222877

2823-
#### `postgresql::postgresql_password(Any *$args)`
2878+
The postgresql::postgresql_password function.
28242879

2825-
postgresql_password.rb
2826-
---- original file header ----
2880+
Returns: `String` The postgresql password hash from the clear text username / password.
28272881

2828-
@return Returns the postgresql password hash from the clear text username / password.
2882+
##### `username`
28292883

2830-
Returns: `Data type` Describe what the function returns here
2884+
Data type: `Variant[String[1],Integer]`
28312885

2832-
##### `*args`
2886+
The clear text `username`
28332887

2834-
Data type: `Any`
2888+
##### `password`
28352889

2836-
The original array of arguments. Port this to individually managed params
2837-
to get the full benefit of the modern function API.
2890+
Data type: `Variant[String[1],Integer]`
2891+
2892+
The clear text `password`
28382893

28392894
### postgresql_acls_to_resources_hash
28402895

@@ -2884,15 +2939,21 @@ Returns: `Any` Safely escapes a string using $$ using a random tag which should
28842939

28852940
### postgresql_password
28862941

2887-
Type: Ruby 3.x API
2942+
Type: Ruby 4.x API
28882943

2889-
This function returns the postgresql password hash from the clear text username / password
2944+
DEPRECATED. Use the namespaced function [`postgresql::postgresql_password`](#postgresqlpostgresql_password) instead.
28902945

2891-
#### `postgresql_password()`
2946+
#### `postgresql_password(Any *$args)`
2947+
2948+
The postgresql_password function.
2949+
2950+
Returns: `Any`
2951+
2952+
##### `*args`
2953+
2954+
Data type: `Any`
28922955

2893-
This function returns the postgresql password hash from the clear text username / password
28942956

2895-
Returns: `Any` Returns the postgresql password hash from the clear text username / password.
28962957

28972958
## Tasks
28982959

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,18 @@
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-
# hash a string as mysql's "PASSWORD()" function would do it
10-
require 'digest/md5'
11-
12-
# postgresql_password.rb
13-
# ---- original file header ----
14-
#
15-
# @summary
16-
# This function returns the postgresql password hash from the clear text username / password
17-
# @return Returns the postgresql password hash from the clear text username / password.
18-
#
19-
#
1+
# @summary This function returns the postgresql password hash from the clear text username / password
202
Puppet::Functions.create_function(:'postgresql::postgresql_password') do
21-
# @param args
22-
# The original array of arguments. Port this to individually managed params
23-
# to get the full benefit of the modern function API.
24-
#
25-
# @return [Data type]
26-
# Describe what the function returns here
3+
# @param username
4+
# The clear text `username`
5+
# @param password
6+
# The clear text `password`
277
#
8+
# @return [String]
9+
# The postgresql password hash from the clear text username / password.
2810
dispatch :default_impl do
29-
# Call the method named 'default_impl' when this is matched
30-
# Port this to match individual params for better type safety
31-
repeated_param 'Any', :args
11+
param 'Variant[String[1],Integer]', :username
12+
param 'Variant[String[1],Integer]', :password
3213
end
3314

34-
def default_impl(*args)
35-
if args.size != 2
36-
raise(Puppet::ParseError, 'postgresql_password(): Wrong number of arguments ' \
37-
"given (#{args.size} for 2)")
38-
end
39-
40-
username = args[0]
41-
password = args[1]
42-
15+
def default_impl(username, password)
4316
'md5' + Digest::MD5.hexdigest(password.to_s + username.to_s)
4417
end
4518
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# @summary DEPRECATED. Use the namespaced function [`postgresql::postgresql_password`](#postgresqlpostgresql_password) instead.
2+
Puppet::Functions.create_function(:postgresql_password) do
3+
dispatch :deprecation_gen do
4+
repeated_param 'Any', :args
5+
end
6+
def deprecation_gen(*args)
7+
call_function('deprecation', 'postgresql_password', 'This method is deprecated, please use postgresql::postgresql_password instead.')
8+
call_function('postgresql::postgresql_password', *args)
9+
end
10+
end

lib/puppet/parser/functions/postgresql_password.rb

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'spec_helper'
2+
3+
describe 'postgresql_password' do
4+
it_behaves_like 'postgresql_password function'
5+
end
Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,5 @@
11
require 'spec_helper'
22

33
describe 'postgresql::postgresql_password' do
4-
# without knowing details about the implementation, this is the only test
5-
# case that we can autogenerate. You should add more examples below!
6-
it { is_expected.not_to eq(nil) }
7-
8-
#################################
9-
# Below are some example test cases. You may uncomment and modify them to match
10-
# your needs. Notice that they all expect the base error class of `StandardError`.
11-
# This is because the autogenerated function uses an untyped array for parameters
12-
# and relies on your implementation to do the validation. As you convert your
13-
# function to proper dispatches and typed signatures, you should change the
14-
# expected error of the argument validation examples to `ArgumentError`.
15-
#
16-
# Other error types you might encounter include
17-
#
18-
# * StandardError
19-
# * ArgumentError
20-
# * Puppet::ParseError
21-
#
22-
# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/
23-
#
24-
# it 'raises an error if called with no argument' do
25-
# is_expected.to run.with_params.and_raise_error(StandardError)
26-
# end
27-
#
28-
# it 'raises an error if there is more than 1 arguments' do
29-
# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError)
30-
# end
31-
#
32-
# it 'raises an error if argument is not the proper type' do
33-
# is_expected.to run.with_params('foo').and_raise_error(StandardError)
34-
# end
35-
#
36-
# it 'returns the proper output' do
37-
# is_expected.to run.with_params(123).and_return('the expected output')
38-
# end
39-
#################################
4+
it_behaves_like 'postgresql_password function'
405
end

spec/spec_helper_local.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,17 @@
4141
def param(type, title, param)
4242
param_value(catalogue, type, title, param)
4343
end
44+
45+
shared_examples 'postgresql_password function' do
46+
it { is_expected.not_to eq(nil) }
47+
48+
it {
49+
is_expected.to run.with_params('foo', 'bar').and_return('md596948aad3fcae80c08a35c9b5958cd89')
50+
}
51+
it {
52+
is_expected.to run.with_params('foo', 1234).and_return('md539a0e1b308278a8de5e007cd1f795920')
53+
}
54+
it 'raises an error if there is only 1 argument' do
55+
is_expected.to run.with_params('foo').and_raise_error(StandardError)
56+
end
57+
end

spec/unit/functions/postgresql_password_spec.rb

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

0 commit comments

Comments
 (0)