2 # Cookbook Name:: 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.
22 default_action :create
24 property :user, :kind_of => String, :name_attribute => true
25 property :cluster, :kind_of => String, :required => true
26 property :password, :kind_of => String
27 property :superuser, :default => false
28 property :createdb, :default => false
29 property :createrole, :default => false
30 property :replication, :default => false
33 password = new_resource.password ? "ENCRYPTED PASSWORD '#{new_resource.password.shellescape}'" : ""
34 superuser = new_resource.superuser ? "SUPERUSER" : "NOSUPERUSER"
35 createdb = new_resource.createdb ? "CREATEDB" : "NOCREATEDB"
36 createrole = new_resource.createrole ? "CREATEROLE" : "NOCREATEROLE"
37 replication = new_resource.replication ? "REPLICATION" : "NOREPLICATION"
39 if !cluster.users.include?(new_resource.user)
40 converge_by "create role #{new_resource.user}" do
41 cluster.execute(:command => "CREATE ROLE \"#{new_resource.user}\" LOGIN #{password} #{superuser} #{createdb} #{createrole}")
44 current_user = cluster.users[new_resource.user]
46 if new_resource.superuser != current_user[:superuser]
47 converge_by "alter role #{new_resource.user}" do
48 cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{superuser}")
52 unless new_resource.superuser
53 if new_resource.createdb != current_user[:createdb]
54 converge_by "alter role #{new_resource.user}" do
55 cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{createdb}")
59 if new_resource.createrole != current_user[:createrole]
60 converge_by "alter role #{new_resource.user}" do
61 cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{createrole}")
65 if new_resource.replication != current_user[:replication]
66 converge_by "alter role #{new_resource.user}" do
67 cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{replication}")
75 if cluster.users.include?(new_resource.user)
76 converge_by "drop role #{new_resource.user}" do
77 cluster.execute(:command => "DROP ROLE \"#{new_resource.user}\"")
84 @cluster ||= OpenStreetMap::PostgreSQL.new(new_resource.cluster)