Fix issues reported by new rubocop
[chef.git] / cookbooks / mysql / providers / database.rb
1 #
2 # Cookbook Name:: mysql
3 # Provider:: mysql_database
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::MysqlDatabase.new(new_resource.name)
24   @current_resource.database(new_resource.database)
25   if (mysql_database = @mysql.databases[@current_resource.database])
26     @current_resource.permissions(mysql_database[:permissions])
27   end
28   @current_resource
29 end
30
31 action :create do
32   unless @mysql.databases.include?(new_resource.database)
33     converge_by("create #{new_resource}") do
34       Chef::Log.info("Creating #{new_resource}")
35       @mysql.execute(:command => "CREATE DATABASE `#{new_resource.database}`")
36     end
37   end
38
39   new_permissions = Hash[new_resource.permissions.collect do |user, privileges|
40     [@mysql.canonicalise_user(user), privileges]
41   end]
42
43   @current_resource.permissions.each_key do |user|
44     next if new_permissions[user]
45
46     converge_by("revoke all for #{user} on #{new_resource}") do
47       Chef::Log.info("Revoking all for #{user} on #{new_resource}")
48       @mysql.execute(:command => "REVOKE ALL ON `#{new_resource.database}`.* FROM #{user}")
49     end
50   end
51
52   new_permissions.each do |user, new_privileges|
53     current_privileges = @current_resource.permissions[user] || {}
54     new_privileges = Array(new_privileges)
55
56     if new_privileges.include?(:all)
57       new_privileges |= (Chef::MySQL::DATABASE_PRIVILEGES - [:grant])
58     end
59
60     Chef::MySQL::DATABASE_PRIVILEGES.each do |privilege|
61       if new_privileges.include?(privilege)
62         unless current_privileges.include?(privilege)
63           converge_by("grant #{privilege} for #{user} on mysql database #{new_resource}") do
64             Chef::Log.info("Granting #{privilege} for #{user} on mysql database #{new_resource}")
65             @mysql.execute(:command => "GRANT #{@mysql.privilege_name(privilege)} ON `#{new_resource.database}`.* TO #{user}")
66           end
67         end
68       elsif current_privileges.include?(privilege)
69         converge_by("revoke #{privilege} for #{user} on #{new_resource}") do
70           Chef::Log.info("Revoking #{privilege} for #{user} on #{new_resource}")
71           @mysql.execute(:command => "REVOKE #{@mysql.privilege_name(privilege)} ON `#{new_resource.database}`.* FROM #{user}")
72         end
73       end
74     end
75   end
76 end
77
78 action :drop do
79   if @mysql.databases.include?(new_resource.database)
80     converge_by("drop #{new_resource}") do
81       Chef::Log.info("Dropping #{new_resource}")
82       @mysql.execute(:command => "DROP DATABASE `#{new_resource.database}`")
83     end
84   end
85 end