module-puppetlabs-mysql/manifests/db.pp
Pan 2834498e4d Allow use different name for db resource other than db name
When defining the mysql::db as exported resources, there are chances that the
same resource names are defined across the site, which will cause puppet agent
fail. By adding an optional dbname parameter, default to the $name, it allows the
resouce name to be defined differently, e.g. ${dbname}_${domain}.
Also updated test and docs for dbname parameter and add acceptance test for
dbname parameter feature
2014-03-15 13:38:13 -07:00

60 lines
1.6 KiB
Puppet

# See README.md for details.
define mysql::db (
$user,
$password,
$dbname = $name,
$charset = 'utf8',
$collate = 'utf8_general_ci',
$host = 'localhost',
$grant = 'ALL',
$sql = '',
$enforce_sql = false,
$ensure = 'present'
) {
#input validation
validate_re($ensure, '^(present|absent)$',
"${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
$table = "${dbname}.*"
include '::mysql::client'
mysql_database { $dbname:
ensure => $ensure,
charset => $charset,
collate => $collate,
provider => 'mysql',
require => [ Class['mysql::server'], Class['mysql::client'] ],
before => Mysql_user["${user}@${host}"],
}
$user_resource = {
ensure => $ensure,
password_hash => mysql_password($password),
provider => 'mysql',
require => Class['mysql::server'],
}
ensure_resource('mysql_user', "${user}@${host}", $user_resource)
if $ensure == 'present' {
mysql_grant { "${user}@${host}/${table}":
privileges => $grant,
provider => 'mysql',
user => "${user}@${host}",
table => $table,
require => [ Mysql_user["${user}@${host}"], Class['mysql::server'] ],
}
$refresh = ! $enforce_sql
if $sql {
exec{ "${dbname}-import":
command => "/usr/bin/mysql ${dbname} < ${sql}",
logoutput => true,
environment => "HOME=${::root_home}",
refreshonly => $refresh,
require => Mysql_grant["${user}@${host}/${table}"],
subscribe => Mysql_database[$dbname],
}
}
}
}