]> git.openstreetmap.org Git - chef.git/blob - cookbooks/mysql/providers/user.rb
05fd01eb85caf8cad5ec4973826a463814a8faf1
[chef.git] / cookbooks / mysql / providers / user.rb
1 #
2 # Cookbook Name:: mysql
3 # Provider:: mysql_user
4 #
5 # Copyright 2013, OpenStreetMap Foundation
6 #
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
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19
20 def load_current_resource
21   @mysql = Chef::MySQL.new
22
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])
28     end
29   end
30   @current_resource
31 end
32
33 action :create do
34   user = @mysql.canonicalise_user(new_resource.user)
35   password = new_resource.password ? "IDENTIFIED BY '#{new_resource.password}'" : ""
36
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}")
41     end
42   end
43
44   Chef::MySQL::USER_PRIVILEGES.each do |privilege|
45     next if new_resource.send(privilege) == @current_resource.send(privilege)
46
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}")
51       end
52     else
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}")
56       end
57     end
58   end
59 end
60
61 action :drop do
62   user = @mysql.canonicalise_user(new_resource.user)
63
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}")
68     end
69   end
70 end