Add actual postgresl module from puppetlabs
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / manifests / role.pp
1 # puppet-postgresql
2 # For all details and documentation:
3 # http://github.com/inkling/puppet-postgresql
4 #
5 # Copyright 2012- Inkling Systems, Inc.
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 define postgresql::role(
20     $password_hash    = false,
21     $createdb         = false,
22     $createrole       = false,
23     $db               = 'postgres',
24     $login            = false,
25     $superuser        = false,
26     $replication      = false,
27     $connection_limit = '-1',
28     $username         = $title
29 ) {
30   include postgresql::params
31
32   $login_sql       = $login       ? { true => 'LOGIN'       , default => 'NOLOGIN' }
33   $createrole_sql  = $createrole  ? { true => 'CREATEROLE'  , default => 'NOCREATEROLE' }
34   $createdb_sql    = $createdb    ? { true => 'CREATEDB'    , default => 'NOCREATEDB' }
35   $superuser_sql   = $superuser   ? { true => 'SUPERUSER'   , default => 'NOSUPERUSER' }
36   $replication_sql = $replication ? { true => 'REPLICATION' , default => '' }
37   if ($password_hash != false) {
38     $password_sql = "ENCRYPTED PASSWORD '${password_hash}'"
39   } else {
40     $password_sql = ""
41   }
42
43   Postgresql_psql {
44     db         => $db,
45     psql_user  => $postgresql::params::user,
46     psql_group => $postgresql::params::group,
47     psql_path  => $postgresql::params::psql_path,
48     require    => Postgresql_psql["CREATE ROLE \"${username}\" ${password_sql} ${login_sql} ${createrole_sql} ${createdb_sql} ${superuser_sql} ${replication_sql} CONNECTION LIMIT ${connection_limit}"],
49   }
50
51   postgresql_psql {"CREATE ROLE \"${username}\" ${password_sql} ${login_sql} ${createrole_sql} ${createdb_sql} ${superuser_sql} ${replication_sql} CONNECTION LIMIT ${connection_limit}":
52     unless  => "SELECT rolname FROM pg_roles WHERE rolname='${username}'",
53     require => undef,
54   }
55
56   postgresql_psql {"ALTER ROLE \"${username}\" ${superuser_sql}":
57     unless => "SELECT rolname FROM pg_roles WHERE rolname='${username}' and rolsuper=${superuser}",
58   }
59
60   postgresql_psql {"ALTER ROLE \"${username}\" ${createdb_sql}":
61     unless => "SELECT rolname FROM pg_roles WHERE rolname='${username}' and rolcreatedb=${createdb}",
62   }
63
64   postgresql_psql {"ALTER ROLE \"${username}\" ${createrole_sql}":
65     unless => "SELECT rolname FROM pg_roles WHERE rolname='${username}' and rolcreaterole=${createrole}",
66   }
67
68   postgresql_psql {"ALTER ROLE \"${username}\" ${login_sql}":
69     unless => "SELECT rolname FROM pg_roles WHERE rolname='${username}' and rolcanlogin=${login}",
70   }
71
72   if(versioncmp($postgresql::params::version, '9.1') >= 0) {
73     postgresql_psql {"ALTER ROLE \"${username}\" ${replication_sql}":
74       unless => "SELECT rolname FROM pg_roles WHERE rolname='${username}' and rolreplication=${replication}",
75     }
76   }
77
78   postgresql_psql {"ALTER ROLE \"${username}\" CONNECTION LIMIT ${connection_limit}":
79     unless => "SELECT rolname FROM pg_roles WHERE rolname='${username}' and rolconnlimit=${connection_limit}",
80   }
81
82   if $password_hash {
83     postgresql_psql {"ALTER ROLE \"${username}\" ${password_sql}":
84       unless => "SELECT usename FROM pg_shadow WHERE usename='${username}' and passwd='${password_hash}'",
85     }
86   }
87 }