2 # Cookbook Name:: postgresql
3 # Resource:: postgresql_table
5 # Copyright 2013, 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.
20 default_action :create
22 property :table, :kind_of => String, :name_attribute => true
23 property :cluster, :kind_of => String, :required => true
24 property :database, :kind_of => String, :required => true
25 property :schema, :kind_of => String, :default => "public"
26 property :owner, :kind_of => String, :required => true
27 property :permissions, :kind_of => Hash, :default => {}
30 if tables.include?(qualified_name)
31 if new_resource.owner != tables[qualified_name][:owner]
32 converge_by("set owner for #{new_resource} to #{new_resource.owner}") do
33 Chef::Log.info("Setting owner for #{new_resource} to #{new_resource.owner}")
34 cluster.execute(:command => "ALTER TABLE #{qualified_name} OWNER TO \"#{new_resource.owner}\"", :database => new_resource.database)
38 tables[qualified_name][:permissions].each_key do |user|
39 next if new_resource.permissions[user]
41 converge_by("revoke all for #{user} on #{new_resource}") do
42 Chef::Log.info("Revoking all for #{user} on #{new_resource}")
43 cluster.execute(:command => "REVOKE ALL ON #{qualified_name} FROM \"#{user}\"", :database => new_resource.database)
47 new_resource.permissions.each do |user, new_privileges|
48 current_privileges = tables[qualified_name][:permissions][user] || {}
49 new_privileges = Array(new_privileges)
51 if new_privileges.include?(:all)
52 new_privileges |= OpenStreetMap::PostgreSQL::TABLE_PRIVILEGES
55 OpenStreetMap::PostgreSQL::TABLE_PRIVILEGES.each do |privilege|
56 if new_privileges.include?(privilege)
57 unless current_privileges.include?(privilege)
58 converge_by("grant #{privilege} for #{user} on #{new_resource}") do
59 Chef::Log.info("Granting #{privilege} for #{user} on #{new_resource}")
60 cluster.execute(:command => "GRANT #{privilege.to_s.upcase} ON #{qualified_name} TO \"#{user}\"", :database => new_resource.database)
63 elsif current_privileges.include?(privilege)
64 converge_by("revoke #{privilege} for #{user} on #{new_resource}") do
65 Chef::Log.info("Revoking #{privilege} for #{user} on #{new_resource}")
66 cluster.execute(:command => "REVOKE #{privilege.to_s.upcase} ON #{qualified_name} FROM \"#{user}\"", :database => new_resource.database)
75 if tables.include?(qualified_name)
76 converge_by("drop #{new_resource}") do
77 Chef::Log.info("Dropping #{new_resource}")
78 cluster.execute(:command => "DROP TABLE #{qualified_name}", :database => new_resource.database)
85 @cluster ||= OpenStreetMap::PostgreSQL.new(new_resource.cluster)
89 @tables ||= cluster.tables(new_resource.database)
93 "#{new_resource.schema}.#{new_resource.name}"