Skip to content
This repository was archived by the owner on Jun 11, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 27 additions & 20 deletions manifests/grant.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,44 @@
# grant statement and then applies it.
#
# Supported arguments:
# $mysql_db - The database to apply the grant to.
# If not set, defaults to == $title
# It supports SQL wildcards (%), ie: 'somedatab%'.
# The special value '*' means 'ALL DATABASES'
# $mysql_user - User to grant the permissions to.
# $mysql_password - Plaintext password for the user.
# $mysql_create_db - If you want a $mysql_db database created or not.
# Default: true.
# $mysql_privileges - Privileges to grant to the user.
# Defaults to 'ALL'
# $mysql_host - Host where the user can connect from. Accepts SQL wildcards.
# Default: 'localhost'
# $mysql_grant_filepath - Path where the grant files will be stored.
# Default: '/root/puppet-mysql'
# $mysql_db - The database to apply the grant to.
# If not set, defaults to == $title
# It supports SQL wildcards (%), ie: 'somedatab%'.
# The special value '*' means 'ALL DATABASES'
# $mysql_db_create_options - Special create options e.g. 'character set utf8'.
# $mysql_user - User to grant the permissions to.
# $mysql_password - Plaintext password for the user.
# $mysql_create_db - If you want a $mysql_db database created or not.
# Default: true.
# $mysql_privileges - Privileges to grant to the user.
# Defaults to 'ALL'
# $mysql_host - Host where the user can connect from. Accepts SQL wildcards.
# Default: 'localhost'
# $mysql_grant_filepath - Path where the grant files will be stored.
# Default: '/root/puppet-mysql'

define mysql::grant (
$mysql_db = '',
$mysql_db = '',
$mysql_db_create_options = '',
$mysql_user,
$mysql_password,
$mysql_create_db = true,
$mysql_privileges = 'ALL',
$mysql_host = 'localhost',
$mysql_grant_filepath = '/root/puppet-mysql'
$mysql_create_db = true,
$mysql_privileges = 'ALL',
$mysql_host = 'localhost',
$mysql_grant_filepath = '/root/puppet-mysql'
) {

require mysql

$dbname = $mysql_db ? {
'' => $name,
default => $mysql_db,
}
}

$real_db_create_options = $mysql_db_create_options ? {
'' => '',
default => " $mysql_db_create_options",
}

# Check for wildcards
$real_db = $dbname ? {
Expand Down
14 changes: 14 additions & 0 deletions spec/defines/grant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@
it { should contain_file('mysqlgrant-someuser-localhost-sample6_db%.sql').with_content("# This file is managed by Puppet. DO NOT EDIT.
GRANT ALL ON `sample6_db%`.* TO 'someuser'@'localhost' IDENTIFIED BY 'somepassword';
FLUSH PRIVILEGES ;
") }
end

describe 'Test grant on a single db with create options' do
let(:params) { { :name => 'sample7',
:mysql_db => 'sample7_db',
:mysql_db_create_options => 'character set utf8',
:mysql_user => 'someuser',
:mysql_password => 'somepassword', } }
it { should contain_file('mysqlgrant-someuser-localhost-sample7_db.sql').with_content(
"# This file is managed by Puppet. DO NOT EDIT.
CREATE DATABASE IF NOT EXISTS `sample7_db` character set utf8;
GRANT ALL ON `sample7_db`.* TO 'someuser'@'localhost' IDENTIFIED BY 'somepassword';
FLUSH PRIVILEGES ;
") }
end
end
2 changes: 1 addition & 1 deletion templates/grant.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is managed by Puppet. DO NOT EDIT.
<% if @bool_mysql_create_db -%>
CREATE DATABASE IF NOT EXISTS <%= @real_db %>;
CREATE DATABASE IF NOT EXISTS <%= @real_db %><%= @real_db_create_options %>;
<% end -%>
GRANT <%= @mysql_privileges %> ON <%= @real_db %>.* TO '<%= @mysql_user %>'@'<%= @mysql_host %>' IDENTIFIED BY '<%= @mysql_password %>';
FLUSH PRIVILEGES ;