diff --git a/REFERENCE.md b/REFERENCE.md index 3b5f194fe9..ab1b6fea54 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -63,7 +63,6 @@ * [`postgresql::server::schema`](#postgresql--server--schema): Create a new schema. * [`postgresql::server::table_grant`](#postgresql--server--table_grant): This resource wraps the grant resource to manage table grants specifically. * [`postgresql::server::tablespace`](#postgresql--server--tablespace): This module creates tablespace. -* [`postgresql::validate_db_connection`](#postgresql--validate_db_connection): This type validates that a successful postgres connection. #### Private Defined types @@ -3763,107 +3762,6 @@ Specifies a hash of environment variables used when connecting to a remote serve Default value: `$postgresql::server::default_connect_settings` -### `postgresql::validate_db_connection` - -This validated if the postgres connection can be established -between the node on which this resource is run and a specified postgres -instance (host/port/user/password/database name). - -#### Parameters - -The following parameters are available in the `postgresql::validate_db_connection` defined type: - -* [`database_host`](#-postgresql--validate_db_connection--database_host) -* [`database_name`](#-postgresql--validate_db_connection--database_name) -* [`database_password`](#-postgresql--validate_db_connection--database_password) -* [`database_username`](#-postgresql--validate_db_connection--database_username) -* [`database_port`](#-postgresql--validate_db_connection--database_port) -* [`connect_settings`](#-postgresql--validate_db_connection--connect_settings) -* [`run_as`](#-postgresql--validate_db_connection--run_as) -* [`sleep`](#-postgresql--validate_db_connection--sleep) -* [`tries`](#-postgresql--validate_db_connection--tries) -* [`create_db_first`](#-postgresql--validate_db_connection--create_db_first) - -##### `database_host` - -Data type: `Optional[String[1]]` - -Database host address - -Default value: `undef` - -##### `database_name` - -Data type: `Optional[String[1]]` - -Specifies the name of the database you wish to test. - -Default value: `undef` - -##### `database_password` - -Data type: `Optional[Variant[String, Sensitive[String]]]` - -Specifies the password to connect with. - -Default value: `undef` - -##### `database_username` - -Data type: `Optional[String[1]]` - -Specifies the username to connect with. - -Default value: `undef` - -##### `database_port` - -Data type: `Optional[Variant[String[1], Integer]]` - -Defines the port to use when connecting. - -Default value: `undef` - -##### `connect_settings` - -Data type: `Optional[Hash]` - -Specifies a hash of environment variables used when connecting to a remote server. - -Default value: `undef` - -##### `run_as` - -Data type: `Optional[String[1]]` - -Specifies the user to run the psql command as. - -Default value: `undef` - -##### `sleep` - -Data type: `Integer` - -Sets the number of seconds to sleep for before trying again after a failure. - -Default value: `2` - -##### `tries` - -Data type: `Integer` - -Sets the number of attempts after failure before giving up and failing the resource. - -Default value: `10` - -##### `create_db_first` - -Data type: `Boolean` - -Creates the database when obtaining a successful connection. - -Default value: `true` - ## Resource types ### `postgresql_conf` diff --git a/manifests/validate_db_connection.pp b/manifests/validate_db_connection.pp deleted file mode 100644 index 726264a263..0000000000 --- a/manifests/validate_db_connection.pp +++ /dev/null @@ -1,115 +0,0 @@ -# @summary This type validates that a successful postgres connection. -# -# @note -# This validated if the postgres connection can be established -# between the node on which this resource is run and a specified postgres -# instance (host/port/user/password/database name). -# -# -# @param database_host Database host address -# @param database_name Specifies the name of the database you wish to test. -# @param database_password Specifies the password to connect with. -# @param database_username Specifies the username to connect with. -# @param database_port Defines the port to use when connecting. -# @param connect_settings Specifies a hash of environment variables used when connecting to a remote server. -# @param run_as Specifies the user to run the psql command as. -# @param sleep Sets the number of seconds to sleep for before trying again after a failure. -# @param tries Sets the number of attempts after failure before giving up and failing the resource. -# @param create_db_first Creates the database when obtaining a successful connection. -# -define postgresql::validate_db_connection ( - Optional[String[1]] $database_host = undef, - Optional[String[1]] $database_name = undef, - Optional[Variant[String, Sensitive[String]]] $database_password = undef, - Optional[String[1]] $database_username = undef, - Optional[Variant[String[1], Integer]] $database_port = undef, - Optional[Hash] $connect_settings = undef, - Optional[String[1]] $run_as = undef, - Integer $sleep = 2, - Integer $tries = 10, - Boolean $create_db_first = true -) { - include postgresql::client - include postgresql::params - - warning('postgresql::validate_db_connection is deprecated, please use postgresql_conn_validator.') - - $database_password_unsensitive = if $database_password =~ Sensitive[String] { - $database_password.unwrap - } else { - $database_password - } - - $psql_path = $postgresql::params::psql_path - $module_workdir = $postgresql::params::module_workdir - $validcon_script_path = $postgresql::client::validcon_script_path - - $cmd_init = "${psql_path} --tuples-only --quiet" - $cmd_host = $database_host ? { - undef => '', - default => "-h ${database_host}", - } - $cmd_user = $database_username ? { - undef => '', - default => "-U ${database_username}", - } - $cmd_port = $database_port ? { - undef => '', - default => "-p ${database_port}", - } - $cmd_dbname = $database_name ? { - undef => "--dbname ${postgresql::params::default_database}", - default => "--dbname ${database_name}", - } - $pass_env = $database_password_unsensitive ? { - undef => undef, - default => "PGPASSWORD=${database_password_unsensitive}", - } - $cmd = join([$cmd_init, $cmd_host, $cmd_user, $cmd_port, $cmd_dbname], ' ') - $validate_cmd = [$validcon_script_path, $sleep, $tries, $cmd] - - # This is more of a safety valve, we add a little extra to compensate for the - # time it takes to run each psql command. - $timeout = (($sleep + 2) * $tries) - - # Combine $database_password_unsensitive and $connect_settings into an array of environment - # variables, ensure $database_password_unsensitive is last, allowing it to override a password - # from the $connect_settings hash - if $connect_settings != undef { - if $pass_env != undef { - $env = concat(join_keys_to_values( $connect_settings, '='), $pass_env) - } else { - $env = join_keys_to_values( $connect_settings, '=') - } - } else { - $env = $pass_env - } - - $exec_name = "validate postgres connection for ${database_username}@${database_host}:${database_port}/${database_name}" - $exec_command = "echo 'Unable to connect to defined database using: ${shell_escape($cmd)}' && false" - - exec { $exec_name: - command => $exec_command, - unless => $validate_cmd, - cwd => $module_workdir, - environment => $env, - logoutput => 'on_failure', - user => $run_as, - path => '/bin:/usr/bin:/usr/local/bin', - timeout => $timeout, - require => Class['postgresql::client'], - } - - # This is a little bit of puppet magic. What we want to do here is make - # sure that if the validation and the database instance creation are being - # applied on the same machine, then the database resource is applied *before* - # the validation resource. Otherwise, the validation is guaranteed to fail - # on the first run. - # - # We accomplish this by using Puppet's resource collection syntax to search - # for the Database resource in our current catalog; if it exists, the - # appropriate relationship is created here. - if($create_db_first) { - Postgresql::Server::Database<|title == $database_name|> -> Exec[$exec_name] - } -} diff --git a/spec/defines/validate_db_connection_spec.rb b/spec/defines/validate_db_connection_spec.rb deleted file mode 100644 index c428be4323..0000000000 --- a/spec/defines/validate_db_connection_spec.rb +++ /dev/null @@ -1,68 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'postgresql::validate_db_connection' do - include_examples 'Debian 11' - - let :title do - 'test' - end - - describe 'should work with only default parameters' do - it { is_expected.to contain_postgresql__validate_db_connection('test') } - end - - describe 'should work with all parameters' do - let :params do - { - database_host: 'test', - database_name: 'test', - database_password: 'test', - database_username: 'test', - database_port: 5432, - run_as: 'postgresq', - sleep: 4, - tries: 30 - } - end - - it { is_expected.to contain_postgresql__validate_db_connection('test') } - - it 'has proper path for validate command' do - expect(subject).to contain_exec('validate postgres connection for test@test:5432/test').with(unless: ['/usr/local/bin/validate_postgresql_connection.sh', - 4, - 30, - '/usr/bin/psql --tuples-only --quiet -h test -U test -p 5432 --dbname test']) - end - end - - describe 'should work while specifying validate_connection in postgresql::client' do - let :params do - { - database_host: 'test', - database_name: 'test', - database_password: 'test', - database_username: 'test', - database_port: 5432 - } - end - - let :pre_condition do - <<-MANIFEST - class { 'postgresql::globals': - module_workdir => '/var/tmp', - } -> - class { 'postgresql::client': validcon_script_path => '/opt/something/validate.sh' } - MANIFEST - end - - it 'has proper path for validate command and correct cwd' do - expect(subject).to contain_exec('validate postgres connection for test@test:5432/test').with(unless: ['/opt/something/validate.sh', - 2, - 10, - '/usr/bin/psql --tuples-only --quiet -h test -U test -p 5432 --dbname test'], - cwd: '/var/tmp') - end - end -end