Skip to content

Commit 61ad33d

Browse files
committed
(maint) address comments
Addresses a few of @hasegeli\'s comments. Adds a `command` parameter for more customizability.
1 parent ac21c60 commit 61ad33d

File tree

7 files changed

+28
-76
lines changed

7 files changed

+28
-76
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,12 @@ Default value: ''
16681668

16691669
When using a Unix socket and ident auth, this is the user you are running as.
16701670

1671+
##### `command`
1672+
1673+
This is the command run against the target database to verify connectivity.
1674+
1675+
Default value: 'SELECT 1'
1676+
16711677
##### `host`
16721678

16731679
Sets the hostname of the database you wish to test.

lib/puppet/type/postgresql_conn_validator.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535
desc 'The port that the PostgreSQL server should be listening on.'
3636

3737
validate do |value|
38-
if value
39-
value =~ /[0-9]+/
40-
end
38+
Integer(value)
39+
end
40+
munge do |value|
41+
Integer(value)
4142
end
4243
end
4344

4445
newparam(:connect_settings) do
4546
desc 'Hash of environment variables for connection to a db.'
46-
4747
end
4848

4949
newparam(:sleep) do
@@ -79,4 +79,10 @@
7979
newparam(:run_as) do
8080
desc "System user that will run the psql command."
8181
end
82+
83+
newparam(:command) do
84+
desc "Command to run against target database."
85+
86+
defaultto "SELECT 1"
87+
end
8288
end

lib/puppet/util/postgresql_validator.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ def initialize(resource)
1010
def build_psql_cmd
1111
final_cmd = []
1212

13-
cmd_init = "#{@resource[:psql_path]} --tuples-only --quiet "
13+
cmd_init = "#{@resource[:psql_path]} --tuples-only --quiet --no-psqlrc"
1414

1515
final_cmd.push cmd_init
1616

1717
cmd_parts = {
18-
:host => "-h #{@resource[:host]}",
19-
:port => "-p #{@resource[:port]}",
20-
:db_username => "-U #{@resource[:db_username]}",
21-
:db_name => "--dbname #{@resource[:db_name]}"
18+
:host => "--host=#{@resource[:host]}",
19+
:port => "--port=#{@resource[:port]}",
20+
:db_username => "--username=#{@resource[:db_username]}",
21+
:db_name => "--dbname=#{@resource[:db_name]}",
22+
:command => "--command='#{@resource[:command]}'"
2223
}
2324

24-
cmd_parts[:db_password] = "-w " if @resource[:db_password]
25+
cmd_parts[:db_password] = "--no-password " if @resource[:db_password]
2526

2627
cmd_parts.each do |k,v|
2728
final_cmd.push v if @resource[k]
@@ -57,7 +58,7 @@ def execute_command
5758
end
5859

5960
def build_validate_cmd
60-
"/bin/echo 'SELECT 1' | #{parse_connect_settings.join(' ')} #{build_psql_cmd} "
61+
"#{parse_connect_settings.join(' ')} #{build_psql_cmd} "
6162
end
6263
end
6364
end

manifests/validate_db_connection.pp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@
6565
$env = $pass_env
6666
}
6767

68-
warning($validate_cmd)
69-
warning($env)
70-
7168
$exec_name = "validate postgres connection for ${database_username}@${database_host}:${database_port}/${database_name}"
7269

7370
exec { $exec_name:

ruby.rb

Lines changed: 0 additions & 58 deletions
This file was deleted.

spec/unit/puppet/provider/postgresql_conn_validator/ruby_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
{
1212
:psql_path => '/usr/bin/psql',
1313
:host => 'db.test.com',
14-
:port => '4444',
14+
:port => 4444,
1515
:db_username => 'testuser',
1616
:db_password => 'testpass'
1717
}
1818
end
1919

2020
describe '#build_psql_cmd' do
2121
it 'contains expected commandline options' do
22-
expect(provider.validator.build_psql_cmd).to match /\/usr\/bin\/psql.*-h.*-p.*-U.*/
22+
expect(provider.validator.build_psql_cmd).to match /\/usr\/bin\/psql.*--host=.*--port=.*--username=.*/
2323
end
2424
end
2525

spec/unit/puppet/type/postgresql_conn_validator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
end
1010

1111
describe "when validating attributes" do
12-
[:name, :db_name, :db_username, :host, :port, :connect_settings, :sleep, :tries, :psql_path].each do |param|
12+
[:name, :db_name, :db_username, :command, :host, :port, :connect_settings, :sleep, :tries, :psql_path].each do |param|
1313
it "should have a #{param} parameter" do
1414
expect(described_class.attrtype(param)).to eq(:param)
1515
end
@@ -18,7 +18,7 @@
1818

1919
describe "when validating values" do
2020
describe "tries and sleep" do
21-
[:tries, :sleep].each do |param|
21+
[:tries, :sleep, :port].each do |param|
2222
it "#{param} should be able to cast value as integer" do
2323
expect { described_class.new(:name => 'test', param => '1') }.to_not raise_error
2424
expect { described_class.new(:name => 'test', param => 1) }.to_not raise_error

0 commit comments

Comments
 (0)