2 # Cookbook Name:: postgresql
3 # Provider:: 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 # http://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 def load_current_resource
21 @pg = Chef::PostgreSQL.new(new_resource.cluster)
22 @tables = @pg.tables(new_resource.database)
23 @name = "#{new_resource.schema}.#{new_resource.name}"
25 @current_resource = Chef::Resource::PostgresqlTable.new(new_resource.name)
26 @current_resource.cluster(new_resource.cluster)
27 @current_resource.database(new_resource.database)
28 @current_resource.schema(new_resource.schema)
29 if pg_table = @tables[@name]
30 @current_resource.owner(pg_table[:owner])
31 @current_resource.permissions(pg_table[:permissions])
37 if @tables.include?(@name)
38 if new_resource.owner != @current_resource.owner
39 converge_by("set owner for #{new_resource} to #{new_resource.owner}") do
40 Chef::Log.info("Setting owner for #{new_resource} to #{new_resource.owner}")
41 @pg.execute(:command => "ALTER TABLE #{@name} OWNER TO \"#{new_resource.owner}\"", :database => new_resource.database)
45 @current_resource.permissions.each_key do |user|
46 next if new_resource.permissions[user]
48 converge_by("revoke all for #{user} on #{new_resource}") do
49 Chef::Log.info("Revoking all for #{user} on #{new_resource}")
50 @pg.execute(:command => "REVOKE ALL ON #{@name} FROM \"#{user}\"", :database => new_resource.database)
54 new_resource.permissions.each do |user, new_privileges|
55 current_privileges = @current_resource.permissions[user] || {}
56 new_privileges = Array(new_privileges)
58 if new_privileges.include?(:all)
59 new_privileges |= Chef::PostgreSQL::TABLE_PRIVILEGES
62 Chef::PostgreSQL::TABLE_PRIVILEGES.each do |privilege|
63 if new_privileges.include?(privilege)
64 unless current_privileges.include?(privilege)
65 converge_by("grant #{privilege} for #{user} on #{new_resource}") do
66 Chef::Log.info("Granting #{privilege} for #{user} on #{new_resource}")
67 @pg.execute(:command => "GRANT #{privilege.to_s.upcase} ON #{@name} TO \"#{user}\"", :database => new_resource.database)
71 if current_privileges.include?(privilege)
72 converge_by("revoke #{privilege} for #{user} on #{new_resource}") do
73 Chef::Log.info("Revoking #{privilege} for #{user} on #{new_resource}")
74 @pg.execute(:command => "REVOKE #{privilege.to_s.upcase} ON #{@name} FROM \"#{user}\"", :database => new_resource.database)
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)