Skip to content

Commit d8f909e

Browse files
Merge pull request #1165 from Vampouille/fix_extension_port
Fix custom port in extension
2 parents 7bd2d0f + c1ca98f commit d8f909e

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

manifests/server/extension.pp

+18-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
# version may be set to a specific version, in which case the extension is updated using ALTER EXTENSION "extension" UPDATE TO 'version'
1010
# eg. If extension is set to postgis and version is set to 2.3.3, this will apply the SQL ALTER EXTENSION "postgis" UPDATE TO '2.3.3' to this database only.
1111
# version may be omitted, in which case no ALTER EXTENSION... SQL is applied, and the version will be left unchanged.
12-
#
12+
#
1313
# @param ensure Specifies whether to activate or deactivate the extension. Valid options: 'present' or 'absent'.
1414
# @param package_name Specifies a package to install prior to activating the extension.
1515
# @param package_ensure Overrides default package deletion behavior. By default, the package specified with package_name is installed when the extension is activated and removed when the extension is deactivated. To override this behavior, set the ensure value for the package.
16+
# @param port Port to use when connecting.
1617
# @param connect_settings Specifies a hash of environment variables used when connecting to a remote server.
1718
# @param database_resource_name Specifies the resource name of the DB being managed. Defaults to the parameter $database, if left blank.
1819
define postgresql::server::extension (
@@ -23,6 +24,7 @@
2324
String[1] $ensure = 'present',
2425
$package_name = undef,
2526
$package_ensure = undef,
27+
Optional[Integer] $port = undef,
2628
$connect_settings = postgresql::default('default_connect_settings'),
2729
$database_resource_name = $database,
2830
) {
@@ -33,7 +35,7 @@
3335
case $ensure {
3436
'present': {
3537
$command = "CREATE EXTENSION \"${extension}\""
36-
$unless_mod = ''
38+
$unless_mod = undef
3739
$package_require = []
3840
$package_before = Postgresql_psql["${database}: ${command}"]
3941
}
@@ -57,6 +59,17 @@
5759
}
5860
}
5961

62+
#
63+
# Port, order of precedence: $port parameter, $connect_settings[PGPORT], $postgresql::server::port
64+
#
65+
if $port != undef {
66+
$port_override = $port
67+
} elsif $connect_settings != undef and has_key( $connect_settings, 'PGPORT') {
68+
$port_override = undef
69+
} else {
70+
$port_override = $postgresql::server::port
71+
}
72+
6073
postgresql_psql { "${database}: ${command}":
6174

6275
psql_user => $user,
@@ -65,6 +78,7 @@
6578
connect_settings => $connect_settings,
6679

6780
db => $database,
81+
port => $port_override,
6882
command => $command,
6983
unless => "SELECT 1 WHERE ${unless_mod}EXISTS (SELECT 1 FROM pg_extension WHERE extname = '${extension}')",
7084
}
@@ -90,6 +104,7 @@
90104
psql_path => $psql_path,
91105
connect_settings => $connect_settings,
92106
db => $database,
107+
port => $port_override,
93108
require => Postgresql_psql["${database}: ${command}"],
94109
}
95110

@@ -119,6 +134,7 @@
119134
}
120135
postgresql_psql { "${database}: ${alter_extension_sql}":
121136
db => $database,
137+
port => $port_override,
122138
psql_user => $user,
123139
psql_group => $group,
124140
psql_path => $psql_path,

spec/unit/defines/server/extension_spec.rb

+84
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,93 @@
178178
end
179179

180180
context 'without including postgresql::server' do
181+
let :pre_condition do
182+
"class {'postgresql::server':}"
183+
end
184+
181185
it {
182186
is_expected.to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"')
183187
.with(db: 'postgres', command: 'CREATE EXTENSION "pg_repack"')
184188
}
185189
end
190+
191+
context 'default port' do
192+
let :params do
193+
{
194+
database: 'postgres',
195+
extension: 'pg_repack',
196+
}
197+
end
198+
199+
let :pre_condition do
200+
"class {'postgresql::server':}"
201+
end
202+
203+
it { is_expected.to compile.with_all_deps }
204+
it { is_expected.to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"').with_port('5432') }
205+
end
206+
207+
context 'port overriden by explicit parameter' do
208+
let :params do
209+
{
210+
database: 'postgres',
211+
extension: 'pg_repack',
212+
port: 1234,
213+
}
214+
end
215+
216+
let :pre_condition do
217+
"class {'postgresql::server':}"
218+
end
219+
220+
it { is_expected.to compile.with_all_deps }
221+
it { is_expected.to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"').with_port('1234') }
222+
end
223+
224+
context 'with specific db connection settings' do
225+
let :params do
226+
{
227+
database: 'postgres',
228+
extension: 'pg_repack',
229+
connect_settings: { 'PGHOST' => 'postgres-db-server',
230+
'DBVERSION' => '9.1',
231+
'PGPORT' => '1234' },
232+
}
233+
end
234+
235+
let :pre_condition do
236+
"class {'postgresql::server':}"
237+
end
238+
239+
it { is_expected.to compile.with_all_deps }
240+
it {
241+
is_expected.to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"')
242+
.with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234')
243+
.with_port(nil)
244+
}
245+
end
246+
247+
context 'with specific db connection settings - port overriden by explicit parameter' do
248+
let :params do
249+
{
250+
database: 'postgres',
251+
extension: 'pg_repack',
252+
connect_settings: { 'PGHOST' => 'postgres-db-server',
253+
'DBVERSION' => '9.1',
254+
'PGPORT' => '1234' },
255+
port: 5678,
256+
}
257+
end
258+
259+
let :pre_condition do
260+
"class {'postgresql::server':}"
261+
end
262+
263+
it { is_expected.to compile.with_all_deps }
264+
it {
265+
is_expected.to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"')
266+
.with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234')
267+
.with_port('5678')
268+
}
269+
end
186270
end

0 commit comments

Comments
 (0)