Skip to content

Commit b525756

Browse files
bastelfreakvaol
authored andcommitted
Prefer $connect_settings over explicit parameter
This is the outcome of a large refactoring in puppetlabs#1450 and discussions on IRC and slack. We noticed that the behaviour of `$connect_settings` is currently inconsistent. Sometimes it's preferred over explicity parameters, sometimes not. Reminder: The idea of `$connect_settings` is to provide a hash with environment variable to tell puppet to manage a remote database instead of a local instance. One example is: https://github.com/puppetlabs/puppetlabs-postgresql/blob/93386b48861ff41d5f47033afee592e0506526c5/manifests/server/grant.pp#L80-L86 ``` if $port { $port_override = $port } elsif 'PGPORT' in $connect_settings { $port_override = undef } else { $port_override = $postgresql::server::port } ``` Here the local $port parameter is preferred over `$connect_settings`. The problem is that we now cannot set a default in the resource for `$port`. The default is hardcoded to `$postgresql::server::port`. This becomes. Other classes handle it in a different way: https://github.com/puppetlabs/puppetlabs-postgresql/blob/93386b48861ff41d5f47033afee592e0506526c5/manifests/server/database.pp#L41-L46 ``` if 'PGPORT' in $connect_settings { $port_override = undef } else { $port_override = $port } ``` Here `$connct_settings` is checked first. It defaults to an empty hash. If `PGPORT` isn't in it, `$port` is used. The logic is shorter and enables us to expose $port as a parameter with a default value. With this approach users can decide if they pass `$port` or `$connect_settings`. At the moment the remote database support is broken because the logic to parse `$connect_settings` isn't consistent. The goal of this PR is to unify the logic and prefer `$connect_settings` if it's provided by the user.
1 parent 0ef6460 commit b525756

17 files changed

+1117
-1169
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,15 @@ class { 'postgresql::server':
175175

176176
### Manage remote users, roles, and permissions
177177

178-
Remote SQL objects are managed using the same Puppet resources as local SQL objects, along with a [`connect_settings`](#connect_settings) hash. This provides control over how Puppet connects to the remote Postgres instances and which version is used for generating SQL commands.
178+
Remote SQL objects are managed using the same Puppet resources as local SQL objects, along with a `$connect_settings` hash. This provides control over how Puppet connects to the remote Postgres instances and which version is used for generating SQL commands.
179179

180-
The `connect_settings` hash can contain environment variables to control Postgres client connections, such as 'PGHOST', 'PGPORT', 'PGPASSWORD', and 'PGSSLKEY'. See the [PostgreSQL Environment Variables](http://www.postgresql.org/docs/9.4/static/libpq-envars.html) documentation for a complete list of variables.
180+
The `connect_settings` hash can contain environment variables to control Postgres client connections, such as 'PGHOST', 'PGPORT', 'PGPASSWORD', 'PGUSER' and 'PGSSLKEY'. See the [PostgreSQL Environment Variables](https://www.postgresql.org/docs/current/libpq-envars.html) documentation for a complete list of variables.
181181

182-
Additionally, you can specify the target database version with the special value of 'DBVERSION'. If the `connect_settings` hash is omitted or empty, then Puppet connects to the local PostgreSQL instance.
182+
Additionally, you can specify the target database version with the special value of 'DBVERSION'. If the `$connect_settings` hash is omitted or empty, then Puppet connects to the local PostgreSQL instance.
183+
184+
**The $connect_settings hash has priority over the explicit variables like $port and $user**
185+
186+
When a user provides only the `$port` parameter to a resource and no `$connect_settings`, `$port` will be used. When `$connect_settings` contains `PGPORT` and `$port` is set, `$connect_settings['PGPORT']` will be used.
183187

184188
You can provide a `connect_settings` hash for each of the Puppet resources, or you can set a default `connect_settings` hash in `postgresql::globals`. Configuring `connect_settings` per resource allows SQL objects to be created on multiple databases by multiple users.
185189

REFERENCE.md

+1,079-1,065
Large diffs are not rendered by default.

manifests/server/database.pp

+2-14
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,8 @@
3030
String[1] $default_db = $postgresql::server::default_database,
3131
Stdlib::Port $port = $postgresql::server::port
3232
) {
33-
# If possible use the version of the remote database, otherwise
34-
# fallback to our local DB version
35-
if 'DBVERSION' in $connect_settings {
36-
$version = $connect_settings['DBVERSION']
37-
} else {
38-
$version = $postgresql::server::_version
39-
}
40-
41-
# If the connection settings do not contain a port, then use the local server port
42-
if 'PGPORT' in $connect_settings {
43-
$port_override = undef
44-
} else {
45-
$port_override = $port
46-
}
33+
$version = pick($connect_settings['DBVERSION'], $postgresql::server::_version)
34+
$port_override = pick($connect_settings['PGPORT'], $port)
4735

4836
# Set the defaults for the postgresql_psql resource
4937
Postgresql_psql {

manifests/server/database_grant.pp

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
Optional[Enum['present', 'absent']] $ensure = undef,
1717
Optional[String[1]] $psql_db = undef,
1818
String[1] $psql_user = $postgresql::server::user,
19-
Optional[Hash] $connect_settings = undef,
19+
Hash $connect_settings = $postgresql::server::default_connect_settings,
2020
String[1] $psql_group = $postgresql::server::group,
21-
Optional[Stdlib::Port] $port = undef,
21+
Stdlib::Port $port = $postgresql::server::port,
2222
) {
2323
postgresql::server::grant { "database:${name}":
2424
ensure => $ensure,

manifests/server/default_privileges.pp

+2-18
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,8 @@
3535
Stdlib::Absolutepath $psql_path = $postgresql::server::psql_path,
3636
Optional[String] $target_role = undef,
3737
) {
38-
# If possible use the version of the remote database, otherwise
39-
# fallback to our local DB version
40-
if 'DBVERSION' in $connect_settings {
41-
$version = $connect_settings['DBVERSION']
42-
} else {
43-
$version = $postgresql::server::_version
44-
}
38+
$version = pick($connect_settings['DBVERSION'],postgresql::default('version'))
39+
$port_override = pick($connect_settings['PGPORT'], $port)
4540

4641
if (versioncmp($version, '9.6') == -1) {
4742
fail 'Default_privileges is only useable with PostgreSQL >= 9.6'
@@ -59,17 +54,6 @@
5954
}
6055
}
6156

62-
#
63-
# Port, order of precedence: $port parameter, $connect_settings[PGPORT], $postgresql::server::port
64-
#
65-
if $port {
66-
$port_override = $port
67-
} elsif 'PGPORT' in $connect_settings {
68-
$port_override = undef
69-
} else {
70-
$port_override = $postgresql::server::port
71-
}
72-
7357
if $target_role {
7458
$_target_role = " FOR ROLE ${target_role}"
7559
$_check_target_role = "/${target_role}"

manifests/server/extension.pp

+2-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
Optional[String[1]] $version = undef,
3333
Enum['present', 'absent'] $ensure = 'present',
3434
Optional[String[1]] $package_name = undef,
35-
Optional[Stdlib::Port] $port = undef,
35+
Stdlib::Port $port = postgresql::default('port'),
3636
Hash $connect_settings = postgresql::default('default_connect_settings'),
3737
String[1] $database_resource_name = $database,
3838
String[1] $user = postgresql::default('user'),
@@ -76,16 +76,7 @@
7676
}
7777
}
7878

79-
#
80-
# Port, order of precedence: $port parameter, $connect_settings[PGPORT], $postgresql::server::port
81-
#
82-
if $port {
83-
$port_override = $port
84-
} elsif 'PGPORT' in $connect_settings {
85-
$port_override = undef
86-
} else {
87-
$port_override = $postgresql::server::port
88-
}
79+
$port_override = pick($connect_settings['PGPORT'], $port)
8980

9081
postgresql_psql { "${database}: ${command}":
9182
psql_user => $user,

manifests/server/grant.pp

+2-11
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
Array[String[1],0] $object_arguments = [],
4242
String $psql_db = $postgresql::server::default_database,
4343
String $psql_user = $postgresql::server::user,
44-
Optional[Stdlib::Port] $port = undef,
44+
Stdlib::Port $port = $postgresql::server::port,
4545
Boolean $onlyif_exists = false,
4646
Hash $connect_settings = $postgresql::server::default_connect_settings,
4747
Enum['present', 'absent'] $ensure = 'present',
@@ -74,16 +74,7 @@
7474
$_object_name = $object_name
7575
}
7676

77-
#
78-
# Port, order of precedence: $port parameter, $connect_settings[PGPORT], $postgresql::server::port
79-
# We don't use pick() here because that would introduce a hard dependency to the postgresql::server class
80-
if $port {
81-
$port_override = $port
82-
} elsif 'PGPORT' in $connect_settings {
83-
$port_override = undef
84-
} else {
85-
$port_override = $postgresql::server::port
86-
}
77+
$port_override = pick($connect_settings['PGPORT'], $port)
8778

8879
## Munge the input values
8980
$_object_type = upcase($object_type)

manifests/server/reassign_owned_by.pp

+1-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@
2121
$group = $postgresql::server::group
2222
$psql_path = $postgresql::server::psql_path
2323

24-
if 'PGPORT' in $connect_settings {
25-
$port_override = undef
26-
} else {
27-
$port_override = $port
28-
}
24+
$port_override = pick($connect_settings['PGPORT'], $port)
2925

3026
$onlyif = "SELECT tablename FROM pg_catalog.pg_tables WHERE
3127
schemaname NOT IN ('pg_catalog', 'information_schema') AND

manifests/server/role.pp

+3-19
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
Boolean $createdb = false,
3030
Boolean $createrole = false,
3131
String[1] $db = $postgresql::server::default_database,
32-
Optional[Stdlib::Port] $port = undef,
32+
Stdlib::Port $port = postgresql::default('port'),
3333
Boolean $login = true,
3434
Boolean $inherit = true,
3535
Boolean $superuser = false,
@@ -50,24 +50,8 @@
5050
} else {
5151
$password_hash
5252
}
53-
#
54-
# Port, order of precedence: $port parameter, $connect_settings[PGPORT], $postgresql::server::port
55-
#
56-
if $port {
57-
$port_override = $port
58-
} elsif 'PGPORT' in $connect_settings {
59-
$port_override = undef
60-
} else {
61-
$port_override = $postgresql::server::port
62-
}
63-
64-
# If possible use the version of the remote database, otherwise
65-
# fallback to our local DB version
66-
if 'DBVERSION' in $connect_settings {
67-
$version = $connect_settings['DBVERSION']
68-
} else {
69-
$version = $postgresql::server::_version
70-
}
53+
$port_override = pick($connect_settings['PGPORT'], $port)
54+
$version = pick($connect_settings['DBVERSION'], postgresql::default('version'))
7155

7256
Postgresql_psql {
7357
db => $db,

manifests/server/schema.pp

+4-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# @param owner Sets the default owner of the schema.
99
# @param schema Sets the name of the schema.
1010
# @param connect_settings Specifies a hash of environment variables used when connecting to a remote server.
11+
# @param port the post the postgresql instance is listening on.
1112
# @example
1213
# postgresql::server::schema {'private':
1314
# db => 'template1',
@@ -17,6 +18,7 @@
1718
Optional[String[1]] $owner = undef,
1819
String[1] $schema = $title,
1920
Hash $connect_settings = $postgresql::server::default_connect_settings,
21+
Stdlib::Port $port = $postgresql::server::port,
2022
) {
2123
$user = $postgresql::server::user
2224
$group = $postgresql::server::group
@@ -27,18 +29,14 @@
2729
Postgresql::Server::Db <| dbname == $db |> -> Postgresql::Server::Schema[$name]
2830

2931
# If the connection settings do not contain a port, then use the local server port
30-
if 'PGPORT' in $connect_settings {
31-
$port = undef
32-
} else {
33-
$port = $postgresql::server::port
34-
}
32+
$port_override = pick($connect_settings['PGPORT'], $port)
3533

3634
Postgresql_psql {
3735
db => $db,
3836
psql_user => $user,
3937
psql_group => $group,
4038
psql_path => $psql_path,
41-
port => $port,
39+
port => $port_override,
4240
cwd => $module_workdir,
4341
connect_settings => $connect_settings,
4442
}

manifests/server/tablespace.pp

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,28 @@
55
# @param owner Specifies the default owner of the tablespace.
66
# @param spcname Specifies the name of the tablespace.
77
# @param connect_settings Specifies a hash of environment variables used when connecting to a remote server.
8+
# @param port the port of the postgresql instance that sould be used.
89
define postgresql::server::tablespace (
910
String[1] $location,
1011
Boolean $manage_location = true,
1112
Optional[String[1]] $owner = undef,
1213
String[1] $spcname = $title,
1314
Hash $connect_settings = $postgresql::server::default_connect_settings,
15+
Stdlib::Port $port = $postgresql::server::port,
1416
) {
1517
$user = $postgresql::server::user
1618
$group = $postgresql::server::group
1719
$psql_path = $postgresql::server::psql_path
1820
$module_workdir = $postgresql::server::module_workdir
1921

2022
# If the connection settings do not contain a port, then use the local server port
21-
if 'PGPORT' in $connect_settings {
22-
$port = undef
23-
} else {
24-
$port = $postgresql::server::port
25-
}
23+
$port_override = pick($connect_settings['PGPORT'], $port)
2624

2725
Postgresql_psql {
2826
psql_user => $user,
2927
psql_group => $group,
3028
psql_path => $psql_path,
31-
port => $port,
29+
port => $port_override,
3230
connect_settings => $connect_settings,
3331
cwd => $module_workdir,
3432
}

spec/defines/server/database_grant_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
it { is_expected.to compile.with_all_deps }
2626
it { is_expected.to contain_postgresql__server__database_grant('test') }
27-
it { is_expected.to contain_postgresql__server__grant('database:test').with_psql_user('postgres').without_port.with_group('postgres') }
27+
it { is_expected.to contain_postgresql__server__grant('database:test').with_psql_user('postgres').with_port(5432).with_group('postgres') }
2828
end
2929

3030
context 'with different user/group/port' do

spec/defines/server/database_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class {'postgresql::server':}"
4747
'PGPORT' => '1234' } }
4848
end
4949

50-
it { is_expected.to contain_postgresql_psql('CREATE DATABASE "test"').with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234').with_port(nil) }
50+
it { is_expected.to contain_postgresql_psql('CREATE DATABASE "test"').with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234').with_port(1234) }
5151
end
5252

5353
context 'with global db connection settings - including port' do
@@ -61,7 +61,7 @@ class {'postgresql::server':}"
6161
class {'postgresql::server':}"
6262
end
6363

64-
it { is_expected.to contain_postgresql_psql('CREATE DATABASE "test"').with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.2', 'PGPORT' => '1234').with_port(nil) }
64+
it { is_expected.to contain_postgresql_psql('CREATE DATABASE "test"').with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.2', 'PGPORT' => '1234').with_port(1234) }
6565
end
6666

6767
context 'with different owner' do

spec/defines/server/db_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
it { is_expected.to compile.with_all_deps }
2323
it { is_expected.to contain_postgresql__server__db('testdb').without_port.with_user('foo').with_psql_user('postgres').with_psql_group('postgres') }
2424
it { is_expected.to contain_postgresql__server__database('testdb').without_owner.with_user('postgres').with_group('postgres') }
25-
it { is_expected.to contain_postgresql__server__role('foo').that_comes_before('Postgresql::Server::Database[testdb]').without_port.with_psql_user('postgres').with_psql_group('postgres') }
26-
it { is_expected.to contain_postgresql__server__database_grant('GRANT foo - ALL - testdb').without_port.with_psql_user('postgres').with_psql_group('postgres') }
25+
it { is_expected.to contain_postgresql__server__role('foo').that_comes_before('Postgresql::Server::Database[testdb]').with_port(5432).with_psql_user('postgres').with_psql_group('postgres') }
26+
it { is_expected.to contain_postgresql__server__database_grant('GRANT foo - ALL - testdb').with_port(5432).with_psql_user('postgres').with_psql_group('postgres') }
2727
end
2828

2929
context 'without dbname param' do

spec/defines/server/default_privileges_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class { 'postgresql::server': }
257257

258258
it { is_expected.to compile.with_all_deps }
259259
it { is_expected.to contain_postgresql__server__default_privileges('test') }
260-
it { is_expected.to contain_postgresql_psql('default_privileges:test').with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.6', 'PGPORT' => '1234').with_port('5678') }
260+
it { is_expected.to contain_postgresql_psql('default_privileges:test').with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.6', 'PGPORT' => '1234').with_port('1234') }
261261
end
262262

263263
context 'with specific schema name' do

spec/defines/server/extension_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@
211211
it {
212212
expect(subject).to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"')
213213
.with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234')
214-
.with_port(nil)
214+
.with_port(1234)
215215
}
216216
end
217217

@@ -236,7 +236,7 @@
236236
it {
237237
expect(subject).to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"')
238238
.with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234')
239-
.with_port('5678')
239+
.with_port('1234')
240240
}
241241
end
242242
end

spec/defines/server/grant_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155

156156
it { is_expected.to compile.with_all_deps }
157157
it { is_expected.to contain_postgresql__server__grant('test') }
158-
it { is_expected.to contain_postgresql_psql('grant:test').with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234').with_port('5678') }
158+
it { is_expected.to contain_postgresql_psql('grant:test').with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234').with_port('1234') }
159159
end
160160

161161
context 'with specific schema name' do

0 commit comments

Comments
 (0)