-
Notifications
You must be signed in to change notification settings - Fork 610
Add onlyif option to postgresql_psql type #527
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
Changes from 1 commit
0540059
61c6f09
d2c9ab5
6f44abe
ff77c52
a7fc928
66c849d
4b7f98c
810f077
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… returns true
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,28 @@ def matches(value) | |
| end | ||
| end | ||
|
|
||
| newparam(:onlyif) do | ||
| desc "An optional SQL command to execute prior to the main :command; " + | ||
| "this is generally intended to be used for idempotency, to check " + | ||
| "for the existence of an object in the database to determine whether " + | ||
| "or not the main SQL command needs to be executed at all." | ||
|
|
||
| # Return true if a matching row is found | ||
| def matches(value) | ||
| if Puppet::PUPPETVERSION.to_f < 4 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you checking against puppet 4? run_unless_sql_command will return an array for version greater than 3.4, so this looks like your code won't work for 3.4 < x < 4
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I just copied another similar stanza |
||
| output, status = provider.run_unless_sql_command(value) | ||
| else | ||
| output = provider.run_unless_sql_command(value) | ||
| status = output.exitcode | ||
| end | ||
| self.fail("Error evaluating 'onlyif' clause, returned #{status}: '#{output}'") unless status == 0 | ||
|
|
||
| result_count = output.strip.to_i | ||
| self.debug("Found #{result_count} row(s) executing 'onlyif' clause") | ||
| result_count > 0 | ||
| end | ||
| end | ||
|
|
||
| newparam(:db) do | ||
| desc "The name of the database to execute the SQL command against." | ||
| end | ||
|
|
@@ -97,7 +119,9 @@ def matches(value) | |
| end | ||
|
|
||
| def should_run_sql(refreshing = false) | ||
| onlyif_param = @parameters[:onlyif] | ||
| unless_param = @parameters[:unless] | ||
| return false if !onlyif_param.nil? && !onlyif_param.value.nil? && !onlyif_param.matches(onlyif_param.value) | ||
| return false if !unless_param.nil? && !unless_param.value.nil? && unless_param.matches(unless_param.value) | ||
| return false if !refreshing && @parameters[:refreshonly].value == :true | ||
| true | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,6 +114,36 @@ class { 'postgresql::server': } -> | |
| end | ||
| end | ||
|
|
||
| it 'should not run some SQL when the onlyif query returns no rows' do | ||
| pp = <<-EOS | ||
| class { 'postgresql::server': } -> | ||
| postgresql_psql { 'foobar': | ||
| db => 'postgres', | ||
| psql_user => 'postgres', | ||
| command => 'select 1', | ||
| onlyif => 'select 1 where 1=2', | ||
| } | ||
| EOS | ||
|
|
||
| apply_manifest(pp, :catch_failures => true) | ||
| apply_manifest(pp, :expect_changes => true) | ||
| end | ||
|
|
||
| it 'should run SQL when the onlyif query returns rows' do | ||
| pp = <<-EOS | ||
| class { 'postgresql::server': } -> | ||
| postgresql_psql { 'foobar': | ||
| db => 'postgres', | ||
| psql_user => 'postgres', | ||
| command => 'select * from pg_database limit 1', | ||
| onlfy => 'select 1 where 1=1', | ||
| } | ||
| EOS | ||
|
|
||
| apply_manifest(pp, :catch_failures => true) | ||
| apply_manifest(pp, :catch_changes => true) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably need some validation here that the SQL did/didn't run.... |
||
| end | ||
|
|
||
| context 'with secure password passing by environment' do | ||
| it 'should run SQL that contanins password passed by environment' do | ||
| select = "select \\'$PASS_TO_EMBED\\'" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and there i thought SQL was supposed to be idempotent to begin with ;)