Skip to content

Commit 97c4d4e

Browse files
committed
Merge pull request #489 from xcompass/exported-resource-mysql-db
Allow to use different name for db resource other than db name
2 parents 6506c1d + df706ee commit 97c4d4e

File tree

6 files changed

+98
-7
lines changed

6 files changed

+98
-7
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,25 @@ Creates a database with a user and assigns some privileges.
434434
}
435435
```
436436

437+
Or using a different resource name with exported resources,
438+
439+
```puppet
440+
@@mysql::db { "mydb_${fqdn}":
441+
user => 'myuser',
442+
password => 'mypass',
443+
dbname => 'mydb',
444+
host => ${fqdn},
445+
grant => ['SELECT', 'UPDATE'],
446+
tag => $domain,
447+
}
448+
```
449+
450+
Then collect it on the remote DB server.
451+
452+
```puppet
453+
Mysql::Db <<| tag == $domain |>>
454+
```
455+
437456
###Providers
438457

439458
####mysql_database

manifests/db.pp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
define mysql::db (
33
$user,
44
$password,
5+
$dbname = $name,
56
$charset = 'utf8',
67
$collate = 'utf8_general_ci',
78
$host = 'localhost',
@@ -13,18 +14,18 @@
1314
#input validation
1415
validate_re($ensure, '^(present|absent)$',
1516
"${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
16-
$table = "${name}.*"
17+
$table = "${dbname}.*"
1718

1819
include '::mysql::client'
1920

20-
mysql_database { $name:
21+
$db_resource = {
2122
ensure => $ensure,
2223
charset => $charset,
2324
collate => $collate,
2425
provider => 'mysql',
2526
require => [ Class['mysql::server'], Class['mysql::client'] ],
26-
before => Mysql_user["${user}@${host}"],
2727
}
28+
ensure_resource('mysql_database', $dbname, $db_resource)
2829

2930
$user_resource = {
3031
ensure => $ensure,
@@ -40,19 +41,19 @@
4041
provider => 'mysql',
4142
user => "${user}@${host}",
4243
table => $table,
43-
require => [ Mysql_user["${user}@${host}"], Class['mysql::server'] ],
44+
require => [Mysql_database[$dbname], Mysql_user["${user}@${host}"], Class['mysql::server'] ],
4445
}
4546

4647
$refresh = ! $enforce_sql
4748

4849
if $sql {
49-
exec{ "${name}-import":
50-
command => "/usr/bin/mysql ${name} < ${sql}",
50+
exec{ "${dbname}-import":
51+
command => "/usr/bin/mysql ${dbname} < ${sql}",
5152
logoutput => true,
5253
environment => "HOME=${::root_home}",
5354
refreshonly => $refresh,
5455
require => Mysql_grant["${user}@${host}/${table}"],
55-
subscribe => Mysql_database[$name],
56+
subscribe => Mysql_database[$dbname],
5657
}
5758
}
5859
}

spec/acceptance/mysql_db_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,26 @@ class { 'mysql::server': override_options => { 'root_password' => 'password' } }
4646
expect(shell("mysql -e 'show tables;' spec2|grep table1").exit_code).to be_zero
4747
end
4848
end
49+
50+
describe 'creating a database with dbname parameter' do
51+
# Using puppet_apply as a helper
52+
it 'should work with no errors' do
53+
pp = <<-EOS
54+
class { 'mysql::server': override_options => { 'root_password' => 'password' } }
55+
mysql::db { 'spec1':
56+
user => 'root1',
57+
password => 'password',
58+
dbname => 'realdb',
59+
}
60+
EOS
61+
62+
# Run it twice and test for idempotency
63+
apply_manifest(pp, :catch_failures => true)
64+
apply_manifest(pp, :catch_changes => true)
65+
end
66+
67+
it 'should have the database named realdb' do
68+
expect(shell("mysql -e 'show databases;'|grep realdb").exit_code).to be_zero
69+
end
70+
end
4971
end

spec/defines/mysql_db_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@
4848
'collate' => 'utf8_danish_ci',
4949
})
5050
end
51+
52+
it 'should use dbname parameter as database name instead of name' do
53+
params.merge!({'dbname' => 'real_db'})
54+
should contain_mysql_database('real_db')
55+
end
5156
end

spec/system/mysql_db_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,31 @@ class { 'mysql::server': override_options => { 'root_password' => 'password' } }
5858
end
5959
end
6060
end
61+
62+
describe 'creating a database with dbname parameter' do
63+
# Using puppet_apply as a helper
64+
it 'should work with no errors' do
65+
pp = <<-EOS
66+
class { 'mysql::server': override_options => { 'root_password' => 'password' } }
67+
mysql::db { 'spec1':
68+
user => 'root1',
69+
password => 'password',
70+
dbname => 'realdb',
71+
}
72+
EOS
73+
74+
# Run it twice and test for idempotency
75+
puppet_apply(pp) do |r|
76+
[0,2].should include r.exit_code
77+
r.refresh
78+
r.exit_code.should be_zero
79+
end
80+
end
81+
82+
it 'should have the database' do
83+
shell("mysql -e 'show databases;'|grep realdb") do |s|
84+
s.exit_code.should be_zero
85+
end
86+
end
87+
end
6188
end

tests/mysql_db.pp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class { 'mysql::server':
2+
config_hash => {'root_password' => 'password'}
3+
}
4+
mysql::db { 'mydb':
5+
user => 'myuser',
6+
password => 'mypass',
7+
host => 'localhost',
8+
grant => ['SELECT', 'UPDATE'],
9+
}
10+
mysql::db { "mydb_${fqdn}":
11+
user => 'myuser',
12+
password => 'mypass',
13+
dbname => 'mydb',
14+
host => ${fqdn},
15+
grant => ['SELECT', 'UPDATE'],
16+
tag => $domain,
17+
}

0 commit comments

Comments
 (0)