]> git.openstreetmap.org Git - chef.git/blob - cookbooks/postgresql/resources/table.rb
Merge remote-tracking branch 'tigerfell/pr257'
[chef.git] / cookbooks / postgresql / resources / table.rb
1 #
2 # Cookbook:: postgresql
3 # Resource:: 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 # https://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 unified_mode true
21
22 default_action :create
23
24 property :table, :kind_of => String, :name_property => true
25 property :cluster, :kind_of => String, :required => true
26 property :database, :kind_of => String, :required => true
27 property :schema, :kind_of => String, :default => "public"
28 property :owner, :kind_of => String, :required => [:create]
29 property :permissions, :kind_of => Hash, :default => {}
30
31 action :create do
32   if tables.include?(qualified_name)
33     if new_resource.owner != tables[qualified_name][:owner]
34       converge_by("set owner for #{new_resource} to #{new_resource.owner}") do
35         Chef::Log.info("Setting owner for #{new_resource} to #{new_resource.owner}")
36         cluster.execute(:command => "ALTER TABLE #{qualified_name} OWNER TO \"#{new_resource.owner}\"", :database => new_resource.database)
37       end
38     end
39
40     tables[qualified_name][:permissions].each_key do |user|
41       next if new_resource.permissions[user]
42
43       converge_by("revoke all for #{user} on #{new_resource}") do
44         Chef::Log.info("Revoking all for #{user} on #{new_resource}")
45         cluster.execute(:command => "REVOKE ALL ON #{qualified_name} FROM \"#{user}\"", :database => new_resource.database)
46       end
47     end
48
49     new_resource.permissions.each do |user, new_privileges|
50       current_privileges = tables[qualified_name][:permissions][user] || {}
51       new_privileges = Array(new_privileges)
52
53       if new_privileges.include?(:all)
54         new_privileges |= OpenStreetMap::PostgreSQL::TABLE_PRIVILEGES
55       end
56
57       OpenStreetMap::PostgreSQL::TABLE_PRIVILEGES.each do |privilege|
58         if new_privileges.include?(privilege)
59           unless current_privileges.include?(privilege)
60             converge_by("grant #{privilege} for #{user} on #{new_resource}") do
61               Chef::Log.info("Granting #{privilege} for #{user} on #{new_resource}")
62               cluster.execute(:command => "GRANT #{privilege.to_s.upcase} ON #{qualified_name} TO \"#{user}\"", :database => new_resource.database)
63             end
64           end
65         elsif current_privileges.include?(privilege)
66           converge_by("revoke #{privilege} for #{user} on #{new_resource}") do
67             Chef::Log.info("Revoking #{privilege} for #{user} on #{new_resource}")
68             cluster.execute(:command => "REVOKE #{privilege.to_s.upcase} ON #{qualified_name} FROM \"#{user}\"", :database => new_resource.database)
69           end
70         end
71       end
72     end
73   end
74 end
75
76 action :drop do
77   if tables.include?(qualified_name)
78     converge_by("drop #{new_resource}") do
79       Chef::Log.info("Dropping #{new_resource}")
80       cluster.execute(:command => "DROP TABLE #{qualified_name}", :database => new_resource.database)
81     end
82   end
83 end
84
85 action_class do
86   def cluster
87     @cluster ||= OpenStreetMap::PostgreSQL.new(new_resource.cluster)
88   end
89
90   def tables
91     @tables ||= cluster.tables(new_resource.database)
92   end
93
94   def qualified_name
95     "#{new_resource.schema}.#{new_resource.name}"
96   end
97 end