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
 
  35   password = new_resource.password ? "ENCRYPTED PASSWORD '#{new_resource.password.shellescape}'" : ""
 
  36   superuser = new_resource.superuser ? "SUPERUSER" : "NOSUPERUSER"
 
  37   createdb = new_resource.createdb ? "CREATEDB" : "NOCREATEDB"
 
  38   createrole = new_resource.createrole ? "CREATEROLE" : "NOCREATEROLE"
 
  39   replication = new_resource.replication ? "REPLICATION" : "NOREPLICATION"
 
  41   if !cluster.users.include?(new_resource.user)
 
  42     converge_by "create role #{new_resource.user}" do
 
  43       cluster.execute(:command => "CREATE ROLE \"#{new_resource.user}\" LOGIN #{password} #{superuser} #{createdb} #{createrole}")
 
  46     current_user = cluster.users[new_resource.user]
 
  48     if new_resource.superuser != current_user[:superuser]
 
  49       converge_by "alter role #{new_resource.user}" do
 
  50         cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{superuser}")
 
  54     unless new_resource.superuser
 
  55       if new_resource.createdb != current_user[:createdb]
 
  56         converge_by "alter role #{new_resource.user}" do
 
  57           cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{createdb}")
 
  61       if new_resource.createrole != current_user[:createrole]
 
  62         converge_by "alter role #{new_resource.user}" do
 
  63           cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{createrole}")
 
  67       if new_resource.replication != current_user[:replication]
 
  68         converge_by "alter role #{new_resource.user}" do
 
  69           cluster.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{replication}")
 
  77   if cluster.users.include?(new_resource.user)
 
  78     converge_by "drop role #{new_resource.user}" do
 
  79       cluster.execute(:command => "DROP ROLE \"#{new_resource.user}\"")
 
  86     @cluster ||= OpenStreetMap::PostgreSQL.new(new_resource.cluster)