]> git.openstreetmap.org Git - chef.git/blob - cookbooks/postgresql/providers/user.rb
Use ruby 2.3 on Ubuntu 16.04 database servers
[chef.git] / cookbooks / postgresql / providers / user.rb
1 #
2 # Cookbook Name:: postgresql
3 # Provider:: postgresql_user
4 #
5 # Copyright 2012, OpenStreetMap Foundation
6 #
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
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18 #
19
20 use_inline_resources
21
22 def load_current_resource
23   @pg = Chef::PostgreSQL.new(new_resource.cluster)
24
25   @current_resource = Chef::Resource::PostgresqlUser.new(new_resource.name)
26   @current_resource.user(new_resource.user)
27   @current_resource.cluster(new_resource.cluster)
28   if (pg_user = @pg.users[@current_resource.user])
29     @current_resource.superuser(pg_user[:superuser])
30     @current_resource.createdb(pg_user[:createdb])
31     @current_resource.createrole(pg_user[:createrole])
32     @current_resource.replication(pg_user[:replication])
33   end
34   @current_resource
35 end
36
37 action :create do
38   password = new_resource.password ? "ENCRYPTED PASSWORD '#{new_resource.password}'" : ""
39   superuser = new_resource.superuser ? "SUPERUSER" : "NOSUPERUSER"
40   createdb = new_resource.createdb ? "CREATEDB" : "NOCREATEDB"
41   createrole = new_resource.createrole ? "CREATEROLE" : "NOCREATEROLE"
42   replication = new_resource.replication ? "REPLICATION" : "NOREPLICATION"
43
44   if !@pg.users.include?(new_resource.user)
45     @pg.execute(:command => "CREATE ROLE \"#{new_resource.user}\" LOGIN #{password} #{superuser} #{createdb} #{createrole}")
46     new_resource.updated_by_last_action(true)
47   else
48     if new_resource.superuser != @current_resource.superuser
49       @pg.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{superuser}")
50       new_resource.updated_by_last_action(true)
51     end
52
53     unless new_resource.superuser
54       if new_resource.createdb != @current_resource.createdb
55         @pg.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{createdb}")
56         new_resource.updated_by_last_action(true)
57       end
58
59       if new_resource.createrole != @current_resource.createrole
60         @pg.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{createrole}")
61         new_resource.updated_by_last_action(true)
62       end
63
64       if new_resource.replication != @current_resource.replication
65         @pg.execute(:command => "ALTER ROLE \"#{new_resource.user}\" #{replication}")
66         new_resource.updated_by_last_action(true)
67       end
68     end
69   end
70 end
71
72 action :drop do
73   if @pg.users.include?(new_resource.user)
74     @pg.execute(:command => "DROP ROLE \"#{new_resource.user}\"")
75     new_resource.updated_by_last_action(true)
76   end
77 end