2 # Cookbook:: postgresql
3 # Resource:: postgresql_user
5 # Copyright:: 2012, 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.
24 default_action :create
26 property :user, :kind_of => String, :name_property => true
27 property :cluster, :kind_of => String, :required => true
28 property :password, :kind_of => String
29 property :superuser, :kind_of => [TrueClass, FalseClass], :default => false
30 property :createdb, :kind_of => [TrueClass, FalseClass], :default => false
31 property :createrole, :kind_of => [TrueClass, FalseClass], :default => false
32 property :replication, :kind_of => [TrueClass, FalseClass], :default => false
33 property :roles, :kind_of => [String, Array]
36 password = new_resource.password ? "ENCRYPTED PASSWORD '#{new_resource.password.shellescape}'" : ""
37 superuser = new_resource.superuser ? "SUPERUSER" : "NOSUPERUSER"
38 createdb = new_resource.createdb ? "CREATEDB" : "NOCREATEDB"
39 createrole = new_resource.createrole ? "CREATEROLE" : "NOCREATEROLE"
40 replication = new_resource.replication ? "REPLICATION" : "NOREPLICATION"
42 if !cluster.users.include?(new_resource.user)
43 converge_by "create role #{new_resource.user}" do
44 cluster.execute(:command => "CREATE ROLE \"#{new_resource.user}\" LOGIN #{password} #{superuser} #{createdb} #{createrole}")
47 Array(new_resource.roles).each do |role|
48 converge_by "grant #{role} to #{new_resource.user}" do
49 cluster.execute(:command => "GRANT \"#{role}\" TO \"#{new_resource.user}\"")
53 current_user = cluster.users[new_resource.user]
55 if new_resource.superuser != current_user[:superuser]
56 converge_by "alter role #{new_resource.user}" do
57 cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{superuser}")
61 unless new_resource.superuser
62 if new_resource.createdb != current_user[:createdb]
63 converge_by "alter role #{new_resource.user}" do
64 cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{createdb}")
68 if new_resource.createrole != current_user[:createrole]
69 converge_by "alter role #{new_resource.user}" do
70 cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{createrole}")
74 if new_resource.replication != current_user[:replication]
75 converge_by "alter role #{new_resource.user}" do
76 cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{replication}")
81 roles = Array(new_resource.roles)
84 next if current_user[:roles].include?(role)
86 converge_by "grant #{role} to #{new_resource.user}" do
87 cluster.execute(:command => "GRANT \"#{role}\" TO \"#{new_resource.user}\"")
91 current_user[:roles].each do |role|
92 next if roles.include?(role)
94 converge_by "revoke #{role} from #{new_resource.user}" do
95 cluster.execute(:command => "REVOKE \"#{role}\" FROM \"#{new_resource.user}\"")
102 if cluster.users.include?(new_resource.user)
103 converge_by "drop role #{new_resource.user}" do
104 cluster.execute(:command => "DROP ROLE \"#{new_resource.user}\"")
111 @cluster ||= OpenStreetMap::PostgreSQL.new(new_resource.cluster)