2 # Cookbook Name:: mysql
3 # Resource:: mysql_user
5 # Copyright 2012, 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 # https://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 default_action :create
22 property :user, :kind_of => String, :name_attribute => true
23 property :password, :kind_of => String
25 OpenStreetMap::MySQL::USER_PRIVILEGES.each do |privilege|
26 property privilege, :default => false
30 user = mysql_canonicalise_user(new_resource.user)
31 password = new_resource.password ? "IDENTIFIED BY '#{new_resource.password}'" : ""
33 unless mysql_users.include?(user)
34 converge_by("create #{new_resource}") do
35 Chef::Log.info("Creating #{new_resource}")
36 mysql_execute(:command => "CREATE USER #{user} #{password}")
40 current_privileges = mysql_users.fetch(new_resource.user, {})
42 new_privileges = Hash[OpenStreetMap::MySQL::USER_PRIVILEGES.collect do |privilege|
43 [privilege, new_resource.send(privilege)]
46 new_privileges.each do |privilege, new_enabled|
47 old_enabled = current_privileges.fetch(privilege, false)
49 if new_enabled && !old_enabled
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}")
54 elsif old_enabled && !new_enabled
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}")
75 include OpenStreetMap::MySQL