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.
20 def load_current_resource
21 @mysql = Chef::MySQL.new
23 @current_resource = Chef::Resource::MysqlUser.new(new_resource.name)
24 @current_resource.user(new_resource.user)
25 if (mysql_user = @mysql.users[@current_resource.user])
26 Chef::MySQL::USER_PRIVILEGES.each do |privilege|
27 @current_resource.send(privilege, mysql_user[privilege])
34 user = @mysql.canonicalise_user(new_resource.user)
35 password = new_resource.password ? "IDENTIFIED BY '#{new_resource.password}'" : ""
37 unless @mysql.users.include?(user)
38 converge_by("create #{new_resource}") do
39 Chef::Log.info("Creating #{new_resource}")
40 @mysql.execute(:command => "CREATE USER #{user} #{password}")
44 Chef::MySQL::USER_PRIVILEGES.each do |privilege|
45 next if new_resource.send(privilege) == @current_resource.send(privilege)
47 if new_resource.send(privilege)
48 converge_by("grant #{privilege} for #{new_resource}") do
49 Chef::Log.info("Granting #{privilege} for #{new_resource}")
50 @mysql.execute(:command => "GRANT #{@mysql.privilege_name(privilege)} ON *.* TO #{user}")
53 converge_by("revoke #{privilege} for #{new_resource}") do
54 Chef::Log.info("Revoking #{privilege} for #{new_resource}")
55 @mysql.execute(:command => "REVOKE #{@mysql.privilege_name(privilege)} ON *.* FROM #{user}")
62 user = @mysql.canonicalise_user(new_resource.user)
64 if @mysql.users.include?(user)
65 converge_by("drop #{new_resource}") do
66 Chef::Log.info("Dropping #{new_resource}")
67 @mysql.execute(:command => "DROP USER #{user}")