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.
 
  22 default_action :create
 
  24 property :user, :kind_of => String, :name_property => true
 
  25 property :password, :kind_of => String
 
  27 OpenStreetMap::MySQL::USER_PRIVILEGES.each do |privilege|
 
  28   property privilege, :default => false
 
  32   user = mysql_canonicalise_user(new_resource.user)
 
  33   password = new_resource.password ? "IDENTIFIED BY '#{new_resource.password}'" : ""
 
  35   unless mysql_users.include?(user)
 
  36     converge_by("create #{new_resource}") do
 
  37       Chef::Log.info("Creating #{new_resource}")
 
  38       mysql_execute(:command => "CREATE USER #{user} #{password}")
 
  42   current_privileges = mysql_users.fetch(new_resource.user, {})
 
  44   new_privileges = Hash[OpenStreetMap::MySQL::USER_PRIVILEGES.collect do |privilege|
 
  45     [privilege, new_resource.send(privilege)]
 
  48   new_privileges.each do |privilege, new_enabled|
 
  49     old_enabled = current_privileges.fetch(privilege, false)
 
  51     if new_enabled && !old_enabled
 
  52       converge_by("grant #{privilege} for #{new_resource}") do
 
  53         Chef::Log.info("Granting #{privilege} for #{new_resource}")
 
  54         mysql_execute(:command => "GRANT #{mysql_privilege_name(privilege)} ON *.* TO #{user}")
 
  56     elsif old_enabled && !new_enabled
 
  57       converge_by("revoke #{privilege} for #{new_resource}") do
 
  58         Chef::Log.info("Revoking #{privilege} for #{new_resource}")
 
  59         mysql_execute(:command => "REVOKE #{mysql_privilege_name(privilege)} ON *.* FROM #{user}")
 
  66   user = mysql_canonicalise_user(new_resource.user)
 
  68   if mysql_users.include?(user)
 
  69     converge_by("drop #{new_resource}") do
 
  70       Chef::Log.info("Dropping #{new_resource}")
 
  71       mysql_execute(:command => "DROP USER #{user}")
 
  77   include OpenStreetMap::MySQL