Skip to content

Commit 62e02d1

Browse files
committed
Merge pull request voxpupuli#282 from fatmcgav/MODULES-3170_add-db_name
(MODULES-3170) Add `db_name` param to mongodb::db defined type
2 parents af3448b + 227de42 commit 62e02d1

File tree

2 files changed

+76
-31
lines changed

2 files changed

+76
-31
lines changed

manifests/db.pp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@
55
# == Parameters
66
#
77
# user - Database username.
8+
# db_name - Database name. Defaults to $name.
89
# password_hash - Hashed password. Hex encoded md5 hash of "$username:mongo:$password".
910
# password - Plain text user password. This is UNSAFE, use 'password_hash' unstead.
1011
# roles (default: ['dbAdmin']) - array with user roles.
1112
# tries (default: 10) - The maximum amount of two second tries to wait MongoDB startup.
1213
#
1314
define mongodb::db (
1415
$user,
16+
$db_name = $name,
1517
$password_hash = false,
1618
$password = false,
1719
$roles = ['dbAdmin'],
1820
$tries = 10,
1921
) {
2022

21-
mongodb_database { $name:
23+
mongodb_database { $db_name:
2224
ensure => present,
2325
tries => $tries
2426
}
@@ -31,13 +33,13 @@
3133
fail("Parameter 'password_hash' or 'password' should be provided to mongodb::db.")
3234
}
3335

34-
mongodb_user { "User ${user} on db ${name}":
36+
mongodb_user { "User ${user} on db ${db_name}":
3537
ensure => present,
3638
password_hash => $hash,
3739
username => $user,
38-
database => $name,
40+
database => $db_name,
3941
roles => $roles,
40-
require => Mongodb_database[$name],
42+
require => Mongodb_database[$db_name],
4143
}
4244

4345
}

spec/defines/db_spec.rb

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,83 @@
11
require 'spec_helper'
22

33
describe 'mongodb::db', :type => :define do
4-
let(:title) { 'testdb' }
4+
context 'default' do
5+
let(:title) { 'testdb' }
56

6-
let(:params) {
7-
{ 'user' => 'testuser',
8-
'password' => 'testpass',
7+
let(:params) {
8+
{ 'user' => 'testuser',
9+
'password' => 'testpass',
10+
}
911
}
10-
}
1112

12-
it 'should contain mongodb_database with mongodb::server requirement' do
13-
is_expected.to contain_mongodb_database('testdb')
14-
end
13+
it 'should contain mongodb_database with mongodb::server requirement' do
14+
is_expected.to contain_mongodb_database('testdb')
15+
end
1516

16-
it 'should contain mongodb_user with mongodb_database requirement' do
17-
is_expected.to contain_mongodb_user('User testuser on db testdb').with({
18-
'username' => 'testuser',
19-
'database' => 'testdb',
20-
'require' => 'Mongodb_database[testdb]',
21-
})
22-
end
17+
it 'should contain mongodb_user with mongodb_database requirement' do
18+
is_expected.to contain_mongodb_user('User testuser on db testdb').with({
19+
'username' => 'testuser',
20+
'database' => 'testdb',
21+
'require' => 'Mongodb_database[testdb]',
22+
})
23+
end
2324

24-
it 'should contain mongodb_user with proper roles' do
25-
params.merge!({'roles' => ['testrole1', 'testrole2']})
26-
is_expected.to contain_mongodb_user('User testuser on db testdb')\
27-
.with_roles(["testrole1", "testrole2"])
28-
end
25+
it 'should contain mongodb_user with proper roles' do
26+
params.merge!({'roles' => ['testrole1', 'testrole2']})
27+
is_expected.to contain_mongodb_user('User testuser on db testdb')\
28+
.with_roles(["testrole1", "testrole2"])
29+
end
2930

30-
it 'should prefer password_hash instead of password' do
31-
params.merge!({'password_hash' => 'securehash'})
32-
is_expected.to contain_mongodb_user('User testuser on db testdb')\
33-
.with_password_hash('securehash')
31+
it 'should prefer password_hash instead of password' do
32+
params.merge!({'password_hash' => 'securehash'})
33+
is_expected.to contain_mongodb_user('User testuser on db testdb')\
34+
.with_password_hash('securehash')
35+
end
36+
37+
it 'should contain mongodb_database with proper tries param' do
38+
params.merge!({'tries' => 5})
39+
is_expected.to contain_mongodb_database('testdb').with_tries(5)
40+
end
3441
end
3542

36-
it 'should contain mongodb_database with proper tries param' do
37-
params.merge!({'tries' => 5})
38-
is_expected.to contain_mongodb_database('testdb').with_tries(5)
43+
context 'with a db_name value' do
44+
let(:title) { 'testdb-title' }
45+
46+
let(:params) {
47+
{
48+
'db_name' => 'testdb',
49+
'user' => 'testuser',
50+
'password' => 'testpass',
51+
}
52+
}
53+
54+
it 'should contain mongodb_database with mongodb::server requirement' do
55+
is_expected.to contain_mongodb_database('testdb')
56+
end
57+
58+
it 'should contain mongodb_user with mongodb_database requirement' do
59+
is_expected.to contain_mongodb_user('User testuser on db testdb').with({
60+
'username' => 'testuser',
61+
'database' => 'testdb',
62+
'require' => 'Mongodb_database[testdb]',
63+
})
64+
end
65+
66+
it 'should contain mongodb_user with proper roles' do
67+
params.merge!({'roles' => ['testrole1', 'testrole2']})
68+
is_expected.to contain_mongodb_user('User testuser on db testdb')\
69+
.with_roles(["testrole1", "testrole2"])
70+
end
71+
72+
it 'should prefer password_hash instead of password' do
73+
params.merge!({'password_hash' => 'securehash'})
74+
is_expected.to contain_mongodb_user('User testuser on db testdb')\
75+
.with_password_hash('securehash')
76+
end
77+
78+
it 'should contain mongodb_database with proper tries param' do
79+
params.merge!({'tries' => 5})
80+
is_expected.to contain_mongodb_database('testdb').with_tries(5)
81+
end
3982
end
4083
end

0 commit comments

Comments
 (0)