2 # Cookbook Name:: mysql
3 # Provider:: mysql_user
5 # Copyright 2013, OpenStreetMap Foundation
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
22 def load_current_resource
23 @mysql = Chef::MySQL.new
25 @current_resource = Chef::Resource::MysqlUser.new(new_resource.name)
26 @current_resource.user(new_resource.user)
27 if (mysql_user = @mysql.users[@current_resource.user])
28 Chef::MySQL::USER_PRIVILEGES.each do |privilege|
29 @current_resource.send(privilege, mysql_user[privilege])
36 user = @mysql.canonicalise_user(new_resource.user)
37 password = new_resource.password ? "IDENTIFIED BY '#{new_resource.password}'" : ""
39 unless @mysql.users.include?(user)
40 converge_by("create #{new_resource}") do
41 Chef::Log.info("Creating #{new_resource}")
42 @mysql.execute(:command => "CREATE USER #{user} #{password}")
46 Chef::MySQL::USER_PRIVILEGES.each do |privilege|
47 next if new_resource.send(privilege) == @current_resource.send(privilege)
49 if new_resource.send(privilege)
50 converge_by("grant #{privilege} for #{new_resource}") do
51 Chef::Log.info("Granting #{privilege} for #{new_resource}")
52 @mysql.execute(:command => "GRANT #{@mysql.privilege_name(privilege)} ON *.* TO #{user}")
55 converge_by("revoke #{privilege} for #{new_resource}") do
56 Chef::Log.info("Revoking #{privilege} for #{new_resource}")
57 @mysql.execute(:command => "REVOKE #{@mysql.privilege_name(privilege)} ON *.* FROM #{user}")
64 user = @mysql.canonicalise_user(new_resource.user)
66 if @mysql.users.include?(user)
67 converge_by("drop #{new_resource}") do
68 Chef::Log.info("Dropping #{new_resource}")
69 @mysql.execute(:command => "DROP USER #{user}")