Skip to content

Fix custom port in extension #1165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions manifests/server/extension.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
# version may be set to a specific version, in which case the extension is updated using ALTER EXTENSION "extension" UPDATE TO 'version'
# 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.
# version may be omitted, in which case no ALTER EXTENSION... SQL is applied, and the version will be left unchanged.
#
#
# @param ensure Specifies whether to activate or deactivate the extension. Valid options: 'present' or 'absent'.
# @param package_name Specifies a package to install prior to activating the extension.
# @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.
# @param port Port to use when connecting.
# @param connect_settings Specifies a hash of environment variables used when connecting to a remote server.
# @param database_resource_name Specifies the resource name of the DB being managed. Defaults to the parameter $database, if left blank.
define postgresql::server::extension (
Expand All @@ -23,6 +24,7 @@
String[1] $ensure = 'present',
$package_name = undef,
$package_ensure = undef,
Optional[Integer] $port = undef,
$connect_settings = postgresql::default('default_connect_settings'),
$database_resource_name = $database,
) {
Expand All @@ -33,7 +35,7 @@
case $ensure {
'present': {
$command = "CREATE EXTENSION \"${extension}\""
$unless_mod = ''
$unless_mod = undef
$package_require = []
$package_before = Postgresql_psql["${database}: ${command}"]
}
Expand All @@ -57,6 +59,17 @@
}
}

#
# Port, order of precedence: $port parameter, $connect_settings[PGPORT], $postgresql::server::port
#
if $port != undef {
$port_override = $port
} elsif $connect_settings != undef and has_key( $connect_settings, 'PGPORT') {
$port_override = undef
} else {
$port_override = $postgresql::server::port
}

postgresql_psql { "${database}: ${command}":

psql_user => $user,
Expand All @@ -65,6 +78,7 @@
connect_settings => $connect_settings,

db => $database,
port => $port_override,
command => $command,
unless => "SELECT 1 WHERE ${unless_mod}EXISTS (SELECT 1 FROM pg_extension WHERE extname = '${extension}')",
}
Expand All @@ -90,6 +104,7 @@
psql_path => $psql_path,
connect_settings => $connect_settings,
db => $database,
port => $port_override,
require => Postgresql_psql["${database}: ${command}"],
}

Expand Down Expand Up @@ -119,6 +134,7 @@
}
postgresql_psql { "${database}: ${alter_extension_sql}":
db => $database,
port => $port_override,
psql_user => $user,
psql_group => $group,
psql_path => $psql_path,
Expand Down
84 changes: 84 additions & 0 deletions spec/unit/defines/server/extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,93 @@
end

context 'without including postgresql::server' do
let :pre_condition do
"class {'postgresql::server':}"
end

it {
is_expected.to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"')
.with(db: 'postgres', command: 'CREATE EXTENSION "pg_repack"')
}
end

context 'default port' do
let :params do
{
database: 'postgres',
extension: 'pg_repack',
}
end

let :pre_condition do
"class {'postgresql::server':}"
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"').with_port('5432') }
end

context 'port overriden by explicit parameter' do
let :params do
{
database: 'postgres',
extension: 'pg_repack',
port: 1234,
}
end

let :pre_condition do
"class {'postgresql::server':}"
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"').with_port('1234') }
end

context 'with specific db connection settings' do
let :params do
{
database: 'postgres',
extension: 'pg_repack',
connect_settings: { 'PGHOST' => 'postgres-db-server',
'DBVERSION' => '9.1',
'PGPORT' => '1234' },
}
end

let :pre_condition do
"class {'postgresql::server':}"
end

it { is_expected.to compile.with_all_deps }
it {
is_expected.to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"')
.with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234')
.with_port(nil)
}
end

context 'with specific db connection settings - port overriden by explicit parameter' do
let :params do
{
database: 'postgres',
extension: 'pg_repack',
connect_settings: { 'PGHOST' => 'postgres-db-server',
'DBVERSION' => '9.1',
'PGPORT' => '1234' },
port: 5678,
}
end

let :pre_condition do
"class {'postgresql::server':}"
end

it { is_expected.to compile.with_all_deps }
it {
is_expected.to contain_postgresql_psql('postgres: CREATE EXTENSION "pg_repack"')
.with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGPORT' => '1234')
.with_port('5678')
}
end
end