3 # Resource:: mysql_database
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 # 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.
22 default_action :create
24 property :database, :kind_of => String, :name_property => true
25 property :permissions, :kind_of => Hash, :default => {}
28 unless mysql_databases.include?(new_resource.database)
29 converge_by("create #{new_resource}") do
30 Chef::Log.info("Creating #{new_resource}")
31 mysql_execute(:command => "CREATE DATABASE `#{new_resource.database}`")
35 current_permissions = mysql_databases[new_resource.database]&.dig(:permissions) || {}
37 new_permissions = Hash[new_resource.permissions.collect do |user, privileges|
38 [mysql_canonicalise_user(user), privileges]
41 current_permissions.each_key do |user|
42 next if new_permissions[user]
44 converge_by("revoke all for #{user} on #{new_resource}") do
45 Chef::Log.info("Revoking all for #{user} on #{new_resource}")
46 mysql_execute(:command => "REVOKE ALL ON `#{new_resource.database}`.* FROM #{user}")
50 new_permissions.each do |user, new_privileges|
51 current_privileges = current_permissions[user] || {}
52 new_privileges = Array(new_privileges)
54 if new_privileges.include?(:all)
55 new_privileges |= (OpenStreetMap::MySQL::DATABASE_PRIVILEGES - [:grant])
58 OpenStreetMap::MySQL::DATABASE_PRIVILEGES.each do |privilege|
59 if new_privileges.include?(privilege)
60 unless current_privileges.include?(privilege)
61 converge_by("grant #{privilege} for #{user} on mysql database #{new_resource}") do
62 Chef::Log.info("Granting #{privilege} for #{user} on mysql database #{new_resource}")
63 mysql_execute(:command => "GRANT #{mysql_privilege_name(privilege)} ON `#{new_resource.database}`.* TO #{user}")
66 elsif current_privileges.include?(privilege)
67 converge_by("revoke #{privilege} for #{user} on #{new_resource}") do
68 Chef::Log.info("Revoking #{privilege} for #{user} on #{new_resource}")
69 mysql_execute(:command => "REVOKE #{mysql_privilege_name(privilege)} ON `#{new_resource.database}`.* FROM #{user}")
77 if mysql_databases.include?(new_resource.database)
78 converge_by("drop #{new_resource}") do
79 Chef::Log.info("Dropping #{new_resource}")
80 mysql_execute(:command => "DROP DATABASE `#{new_resource.database}`")
86 include OpenStreetMap::MySQL