Skip to content

(MODULES-2767) allow to check if table exists before grant #776

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 2 commits into from
Nov 9, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions lib/puppet/parser/functions/mysql_table_exists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Puppet::Parser::Functions
newfunction(:mysql_table_exists, :type => :rvalue, :doc => <<-EOS
Check if table exists in database.

For example:

mysql_table_exists('*.*') or mysql_table_exists('example_database.*') always return true
mysql_table_exists('example_db.example_table') check existence table `example_table` in `example_database`

EOS
) do |args|

if args.size != 1
raise(Puppet::ParseError, 'mysql_table_exists(): Wrong number of arguments ' + "given (#{args.size} for 1)")
end

if match = args[0].match(/(.*)\.(.*)/)
db_name, table_name = match.captures
if db_name.eql?('*') or table_name.eql?('*')
true
else
query = "SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME = '#{table_name}' AND TABLE_SCHEMA = '#{db_name}';"

%x{mysql #{defaults_file} -NBe #{query}}.strip.eql?(table_name)
end
else
raise(Puppet::ParseError, 'mysql_table_exists() accept 1 argument - table string like \'database_name.table_name\'')
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should revert this whole thing for early returns:

return raise(Puppet::ParseError, "mysql_table_exists() accept 1 argument - table string like 'database_name.table_name'") unless match = args[0].match(/(.*)\.(.*)/)
db_name, table_name = match.captures
return true if (db_name.eql?('*') or table_name.eql?('*')) # this is actually not true
query = "SELECT TABLE_NAME FROM information_schema, tables WHERE TABLE_NAME etc.."
%x{mysql etc..}


end
end

def defaults_file
if File.file?("#{Facter.value(:root_home)}/.my.cnf")
"--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf"
else
nil
end
end
72 changes: 72 additions & 0 deletions spec/acceptance/types/mysql_grant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,76 @@ class { 'mysql::server':
end
end

describe 'adding privileges to specific table' do
# Using puppet_apply as a helper
it 'setup mysql server' do
pp = <<-EOS
class { 'mysql::server': override_options => { 'root_password' => 'password' } }
EOS

apply_manifest(pp, :catch_failures => true)
end

it 'creates grant on missing table will fail' do
pp = <<-EOS
mysql_grant { 'test@localhost/grant_spec_db.grant_spec_table':
user => 'test@localhost',
privileges => ['SELECT'],
table => 'grant_spec_db.grant_spec_table',
}
EOS
expect(apply_manifest(pp, :expect_failures => true).stderr).to match(/Table 'grant_spec_db\.grant_spec_table' doesn't exist/)
end

it 'checks if table exists before grant' do
pp = <<-EOS
if mysql_table_exists('grant_spec_db.grant_spec_table') {
mysql_grant { 'test@localhost/grant_spec_db.grant_spec_table':
user => 'test@localhost',
privileges => 'ALL',
table => 'grant_spec_db.grant_spec_table',
}
}
EOS
apply_manifest(pp, :catch_changes => true)
end

it 'creates table' do
pp = <<-EOS
file { '/tmp/grant_spec_table.sql':
ensure => file,
content => 'CREATE TABLE grant_spec_table (id int);',
before => Mysql::Db['grant_spec_db'],
}
mysql::db { 'grant_spec_db':
user => 'root1',
password => 'password',
sql => '/tmp/grant_spec_table.sql',
}
EOS

apply_manifest(pp, :catch_failures => true)
end

it 'should have the table' do
expect(shell("mysql -e 'show tables;' grant_spec_db|grep grant_spec_table").exit_code).to be_zero
end

it 'checks if table exists before grant' do
pp = <<-EOS
if mysql_table_exists('grant_spec_db.grant_spec_table') {
mysql_grant { 'test@localhost/grant_spec_db.grant_spec_table':
user => 'test@localhost',
privileges => ['SELECT'],
table => 'grant_spec_db.grant_spec_table',
}
}
EOS
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end


end

end
22 changes: 22 additions & 0 deletions spec/unit/puppet/functions/mysql_table_exists_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

describe 'the mysql_table_exists function' do
before :all do
Puppet::Parser::Functions.autoloader.loadall
end

let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

it 'should exist' do
expect(Puppet::Parser::Functions.function('mysql_table_exists')).to eq('function_mysql_table_exists')
end

it 'should raise a ParseError if there is less than 1 arguments' do
expect { scope.function_mysql_table_exists([]) }.to( raise_error(Puppet::ParseError))
end

it 'should raise a ParseError if there is more than 1 arguments' do
expect { scope.function_mysql_table_exists(%w(foo bar)) }.to( raise_error(Puppet::ParseError))
end

end