Skip to content

Commit 1bd970d

Browse files
committed
Merge pull request #215 from mbakke/max_connections
Support max_user_connections in database_user
2 parents 588af2a + f3b3a7e commit 1bd970d

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

lib/puppet/provider/database_user/mysql.rb

+16-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ def self.instances
1515
end
1616

1717
def create
18-
merged_name = @resource[:name].sub("@", "'@'")
19-
password_hash = @resource.value(:password_hash)
20-
mysql([defaults_file, "mysql", "-e", "create user '#{merged_name}' identified by PASSWORD '#{password_hash}'"].compact)
18+
merged_name = @resource[:name].sub("@", "'@'")
19+
password_hash = @resource.value(:password_hash)
20+
max_user_connections = @resource.value(:max_user_connections) || 0
21+
22+
mysql([defaults_file, "mysql", "-e", "grant usage on *.* to '#{merged_name}' identified by PASSWORD
23+
'#{password_hash}' with max_user_connections #{max_user_connections}"].compact)
2124

2225
exists? ? (return true) : (return false)
2326
end
@@ -39,6 +42,16 @@ def password_hash=(string)
3942
password_hash == string ? (return true) : (return false)
4043
end
4144

45+
def max_user_connections
46+
mysql([defaults_file, "mysql", "-NBe", "select max_user_connections from mysql.user where CONCAT(user, '@', host) = '#{@resource[:name]}'"].compact).chomp
47+
end
48+
49+
def max_user_connections=(int)
50+
mysql([defaults_file, "mysql", "-e", "grant usage on *.* to '%s' with max_user_connections #{int}" % [ @resource[:name].sub("@", "'@'")] ].compact).chomp
51+
52+
max_user_connections == int ? (return true) : (return false)
53+
end
54+
4255
def exists?
4356
not mysql([defaults_file, "mysql", "-NBe", "select '1' from mysql.user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)].compact).empty?
4457
end

lib/puppet/type/database_user.rb

+5
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@
2222
newvalue(/\w+/)
2323
end
2424

25+
newproperty(:max_user_connections) do
26+
desc "Max concurrent connections for the user. 0 means no (or global) limit."
27+
newvalue(/\d+/)
28+
end
29+
2530
end

spec/unit/puppet/provider/database_user/mysql_spec.rb

+21-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
before :each do
2727
# password hash = mypass
2828
@resource = Puppet::Type::Database_user.new(
29-
{ :password_hash => '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4', :name => 'joe@localhost' }
29+
{ :password_hash => '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4',
30+
:name => 'joe@localhost',
31+
:max_user_connections => '10'
32+
}
3033
)
3134
@provider = provider_class.new(@resource)
3235
Facter.stubs(:value).with(:root_home).returns(root_home)
@@ -46,7 +49,8 @@
4649

4750
describe 'create' do
4851
it 'makes a user' do
49-
subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "create user 'joe'@'localhost' identified by PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4'"])
52+
subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "grant usage on *.* to 'joe'@'localhost' identified by PASSWORD
53+
'*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4' with max_user_connections 10"])
5054
@provider.expects(:exists?).returns(true)
5155
@provider.create.should be_true
5256
end
@@ -76,6 +80,21 @@
7680
end
7781
end
7882

83+
describe 'max_user_connections' do
84+
it 'returns max user connections' do
85+
subject.expects(:mysql).with([defaults_file, 'mysql', '-NBe', "select max_user_connections from mysql.user where CONCAT(user, '@', host) = 'joe@localhost'"]).returns('10')
86+
@provider.max_user_connections.should == '10'
87+
end
88+
end
89+
90+
describe 'max_user_connections=' do
91+
it 'changes max user connections' do
92+
subject.expects(:mysql).with([defaults_file, 'mysql', '-e', "grant usage on *.* to 'joe'@'localhost' with max_user_connections 42"]).returns('0')
93+
@provider.expects(:max_user_connections).returns('42')
94+
@provider.max_user_connections=('42')
95+
end
96+
end
97+
7998
describe 'exists?' do
8099
it 'checks if user exists' do
81100
subject.expects(:mysql).with([defaults_file, 'mysql', '-NBe', "select '1' from mysql.user where CONCAT(user, '@', host) = 'joe@localhost'"]).returns('1')

0 commit comments

Comments
 (0)