Merge pull request #376 from w32-blaster/master

Type mysql_grant fixed, spec test created
This commit is contained in:
Igor Galić 2013-11-17 07:47:50 -08:00
commit 6ae9bc61d4
6 changed files with 67 additions and 56 deletions

View file

@ -1,4 +1,5 @@
Puppet::Type.type(:database).provide(:mysql) do
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'mysql'))
Puppet::Type.type(:database).provide(:mysql, :parent => Puppet::Provider::Mysql) do
desc 'Manages MySQL database.'
defaultfor :kernel => 'Linux'
@ -6,18 +7,6 @@ Puppet::Type.type(:database).provide(:mysql) do
optional_commands :mysql => 'mysql'
optional_commands :mysqladmin => 'mysqladmin'
def self.defaults_file
if File.file?("#{Facter.value(:root_home)}/.my.cnf")
"--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf"
else
nil
end
end
def defaults_file
self.class.defaults_file
end
def self.instances
mysql([defaults_file, '-NBe', 'show databases'].compact).split("\n").collect do |name|
new(:name => name)

View file

@ -3,7 +3,8 @@
# user@host => global
# user@host/db => per-db
Puppet::Type.type(:database_grant).provide(:mysql) do
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'mysql'))
Puppet::Type.type(:database_grant).provide(:mysql, :parent => Puppet::Provider::Mysql) do
desc 'Uses mysql as database.'
@ -195,16 +196,4 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
end
end
# Optional defaults file
def self.defaults_file
if File.file?("#{Facter.value(:root_home)}/.my.cnf")
"--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf"
else
nil
end
end
def defaults_file
self.class.defaults_file
end
end

View file

@ -1,4 +1,5 @@
Puppet::Type.type(:database_user).provide(:mysql) do
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'mysql'))
Puppet::Type.type(:database_user).provide(:mysql, :parent => Puppet::Provider::Mysql) do
desc 'manage users for a mysql database.'
@ -15,19 +16,19 @@ Puppet::Type.type(:database_user).provide(:mysql) do
end
def create
merged_name = @resource[:name].sub('@', "'@'")
merged_name = self.class.cmd_user(@resource[:name])
password_hash = @resource.value(:password_hash)
max_user_connections = @resource.value(:max_user_connections) || 0
mysql([defaults_file, 'mysql', '-e', "grant usage on *.* to '#{merged_name}' identified by PASSWORD
mysql([defaults_file, 'mysql', '-e', "grant usage on *.* to #{merged_name} identified by PASSWORD
'#{password_hash}' with max_user_connections #{max_user_connections}"].compact)
exists? ? (return true) : (return false)
end
def destroy
merged_name = @resource[:name].sub('@', "'@'")
mysql([defaults_file, 'mysql', '-e', "drop user '#{merged_name}'"].compact)
merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, 'mysql', '-e', "drop user #{merged_name}"].compact)
exists? ? (return false) : (return true)
end
@ -37,7 +38,7 @@ Puppet::Type.type(:database_user).provide(:mysql) do
end
def password_hash=(string)
mysql([defaults_file, 'mysql', '-e', "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub('@', "'@'"), string ] ].compact)
mysql([defaults_file, 'mysql', '-e', "SET PASSWORD FOR #{self.class.cmd_user(@resource[:name])} = '#{string}'"].compact)
password_hash == string ? (return true) : (return false)
end
@ -47,7 +48,7 @@ Puppet::Type.type(:database_user).provide(:mysql) do
end
def max_user_connections=(int)
mysql([defaults_file, "mysql", "-e", "grant usage on *.* to '%s' with max_user_connections #{int}" % [ @resource[:name].sub("@", "'@'")] ].compact).chomp
mysql([defaults_file, "mysql", "-e", "grant usage on *.* to %s with max_user_connections #{int}" % [ self.class.cmd_user(@resource[:name])] ].compact).chomp
max_user_connections == int ? (return true) : (return false)
end
@ -61,16 +62,4 @@ Puppet::Type.type(:database_user).provide(:mysql) do
mysqladmin([defaults_file, 'flush-privileges'].compact)
end
# Optional defaults file
def self.defaults_file
if File.file?("#{Facter.value(:root_home)}/.my.cnf")
"--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf"
else
nil
end
end
def defaults_file
self.class.defaults_file
end
end

View file

@ -78,36 +78,36 @@ Puppet::Type.type(:mysql_user).provide(:mysql, :parent => Puppet::Provider::Mysq
mk_resource_methods
def password_hash=(string)
merged_name = @resource[:name].sub('@', "'@'")
mysql([defaults_file, '-e', "SET PASSWORD FOR '#{merged_name}' = '#{string}'"].compact)
merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, '-e', "SET PASSWORD FOR #{merged_name} = '#{string}'"].compact)
password_hash == string ? (return true) : (return false)
end
def max_user_connections=(int)
merged_name = @resource[:name].sub('@', "'@'")
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO '#{merged_name}' WITH MAX_USER_CONNECTIONS #{int}"].compact).chomp
merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO #{merged_name} WITH MAX_USER_CONNECTIONS #{int}"].compact).chomp
max_user_connections == int ? (return true) : (return false)
end
def max_connections_per_hour=(int)
merged_name = @resource[:name].sub('@', "'@'")
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO '#{merged_name}' WITH MAX_CONNECTIONS_PER_HOUR #{int}"].compact).chomp
merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO #{merged_name} WITH MAX_CONNECTIONS_PER_HOUR #{int}"].compact).chomp
max_connections_per_hour == int ? (return true) : (return false)
end
def max_queries_per_hour=(int)
merged_name = @resource[:name].sub('@', "'@'")
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO '#{merged_name}' WITH MAX_QUERIES_PER_HOUR #{int}"].compact).chomp
merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO #{merged_name} WITH MAX_QUERIES_PER_HOUR #{int}"].compact).chomp
max_queries_per_hour == int ? (return true) : (return false)
end
def max_updates_per_hour=(int)
merged_name = @resource[:name].sub('@', "'@'")
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO '#{merged_name}' WITH MAX_UPDATES_PER_HOUR #{int}"].compact).chomp
merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO #{merged_name} WITH MAX_UPDATES_PER_HOUR #{int}"].compact).chomp
max_updates_per_hour == int ? (return true) : (return false)
end

View file

@ -49,7 +49,7 @@ Puppet::Type.newtype(:mysql_grant) do
value.delete("`")
end
newvalues(/.*\..*/)
newvalues(/.*\..*/,/@/)
end
newproperty(:user) do

View file

@ -0,0 +1,44 @@
require 'puppet'
require 'puppet/type/mysql_grant'
describe Puppet::Type.type(:mysql_grant) do
before :each do
@user = Puppet::Type.type(:mysql_grant).new(:name => 'foo@localhost/*.*', :privileges => ['ALL', 'PROXY'], :table => ['*.*','@'], :user => 'foo@localhost')
end
it 'should accept a grant name' do
@user[:name].should == 'foo@localhost/*.*'
end
it 'should accept ALL privileges' do
@user[:privileges] = 'ALL'
@user[:privileges].should == ['ALL']
end
it 'should accept PROXY privilege' do
@user[:privileges] = 'PROXY'
@user[:privileges].should == ['PROXY']
end
it 'should accept a table' do
@user[:table] = '*.*'
@user[:table].should == '*.*'
end
it 'should accept @ for table' do
@user[:table] = '@'
@user[:table].should == '@'
end
it 'should accept a user' do
@user[:user] = 'foo@localhost'
@user[:user].should == 'foo@localhost'
end
it 'should require a name' do
expect {
Puppet::Type.type(:mysql_grant).new({})
}.to raise_error(Puppet::Error, 'Title or name must be provided')
end
end