]> git.openstreetmap.org Git - chef.git/blob - cookbooks/postgresql/providers/table.rb
Make web frontends use backends in the same location
[chef.git] / cookbooks / postgresql / providers / table.rb
1 #
2 # Cookbook Name:: postgresql
3 # Provider:: postgresql_table
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 use_inline_resources
21
22 def load_current_resource
23   @pg = Chef::PostgreSQL.new(new_resource.cluster)
24   @tables = @pg.tables(new_resource.database)
25   @name = "#{new_resource.schema}.#{new_resource.name}"
26
27   @current_resource = Chef::Resource::PostgresqlTable.new(new_resource.name)
28   @current_resource.cluster(new_resource.cluster)
29   @current_resource.database(new_resource.database)
30   @current_resource.schema(new_resource.schema)
31   if (pg_table = @tables[@name])
32     @current_resource.owner(pg_table[:owner])
33     @current_resource.permissions(pg_table[:permissions])
34   end
35   @current_resource
36 end
37
38 action :create do
39   if @tables.include?(@name)
40     if new_resource.owner != @current_resource.owner
41       converge_by("set owner for #{new_resource} to #{new_resource.owner}") do
42         Chef::Log.info("Setting owner for #{new_resource} to #{new_resource.owner}")
43         @pg.execute(:command => "ALTER TABLE #{@name} OWNER TO \"#{new_resource.owner}\"", :database => new_resource.database)
44       end
45     end
46
47     @current_resource.permissions.each_key do |user|
48       next if new_resource.permissions[user]
49
50       converge_by("revoke all for #{user} on #{new_resource}") do
51         Chef::Log.info("Revoking all for #{user} on #{new_resource}")
52         @pg.execute(:command => "REVOKE ALL ON #{@name} FROM \"#{user}\"", :database => new_resource.database)
53       end
54     end
55
56     new_resource.permissions.each do |user, new_privileges|
57       current_privileges = @current_resource.permissions[user] || {}
58       new_privileges = Array(new_privileges)
59
60       if new_privileges.include?(:all)
61         new_privileges |= Chef::PostgreSQL::TABLE_PRIVILEGES
62       end
63
64       Chef::PostgreSQL::TABLE_PRIVILEGES.each do |privilege|
65         if new_privileges.include?(privilege)
66           unless current_privileges.include?(privilege)
67             converge_by("grant #{privilege} for #{user} on #{new_resource}") do
68               Chef::Log.info("Granting #{privilege} for #{user} on #{new_resource}")
69               @pg.execute(:command => "GRANT #{privilege.to_s.upcase} ON #{@name} TO \"#{user}\"", :database => new_resource.database)
70             end
71           end
72         elsif current_privileges.include?(privilege)
73           converge_by("revoke #{privilege} for #{user} on #{new_resource}") do
74             Chef::Log.info("Revoking #{privilege} for #{user} on #{new_resource}")
75             @pg.execute(:command => "REVOKE #{privilege.to_s.upcase} ON #{@name} FROM \"#{user}\"", :database => new_resource.database)
76           end
77         end
78       end
79     end
80   end
81 end
82
83 action :drop do
84   if @tables.include?(@name)
85     converge_by("drop #{new_resource}") do
86       Chef::Log.info("Dropping #{new_resource}")
87       @pg.execute(:command => "DROP TABLE #{@name}", :database => new_resource.database)
88     end
89   end
90 end