diff --git a/manifests/server/extension.pp b/manifests/server/extension.pp index 25626fd0ae..71b11d869b 100644 --- a/manifests/server/extension.pp +++ b/manifests/server/extension.pp @@ -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 ( @@ -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, ) { @@ -33,7 +35,7 @@ case $ensure { 'present': { $command = "CREATE EXTENSION \"${extension}\"" - $unless_mod = '' + $unless_mod = undef $package_require = [] $package_before = Postgresql_psql["${database}: ${command}"] } @@ -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, @@ -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}')", } @@ -90,6 +104,7 @@ psql_path => $psql_path, connect_settings => $connect_settings, db => $database, + port => $port_override, require => Postgresql_psql["${database}: ${command}"], } @@ -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, diff --git a/spec/unit/defines/server/extension_spec.rb b/spec/unit/defines/server/extension_spec.rb index ece6206292..14fe09500e 100644 --- a/spec/unit/defines/server/extension_spec.rb +++ b/spec/unit/defines/server/extension_spec.rb @@ -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