newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / manifests / server / tablespace.pp
1 # This module creates tablespace. See README.md for more details.
2 define postgresql::server::tablespace(
3   $location,
4   $owner   = undef,
5   $spcname = $title,
6   $connect_settings = $postgresql::server::default_connect_settings,
7 ) {
8   $user           = $postgresql::server::user
9   $group          = $postgresql::server::group
10   $psql_path      = $postgresql::server::psql_path
11   $module_workdir = $postgresql::server::module_workdir
12
13   # If the connection settings do not contain a port, then use the local server port
14   if $connect_settings != undef and has_key( $connect_settings, 'PGPORT') {
15     $port = undef
16   } else {
17     $port = $postgresql::server::port
18   }
19
20   Postgresql_psql {
21     psql_user        => $user,
22     psql_group       => $group,
23     psql_path        => $psql_path,
24     port             => $port,
25     connect_settings => $connect_settings,
26     cwd              => $module_workdir,
27   }
28
29   file { $location:
30     ensure  => directory,
31     owner   => $user,
32     group   => $group,
33     mode    => '0700',
34     seluser => 'system_u',
35     selrole => 'object_r',
36     seltype => 'postgresql_db_t',
37     require => Class['postgresql::server'],
38   }
39
40   postgresql_psql { "CREATE TABLESPACE \"${spcname}\"":
41     command => "CREATE TABLESPACE \"${spcname}\" LOCATION '${location}'",
42     unless  => "SELECT 1 FROM pg_tablespace WHERE spcname = '${spcname}'",
43     require => [Class['postgresql::server'], File[$location]],
44   }
45
46   if $owner {
47     postgresql_psql { "ALTER TABLESPACE \"${spcname}\" OWNER TO \"${owner}\"":
48       unless  => "SELECT 1 FROM pg_tablespace JOIN pg_roles rol ON spcowner = rol.oid WHERE spcname = '${spcname}' AND rolname = '${owner}'",
49       require => Postgresql_psql["CREATE TABLESPACE \"${spcname}\""],
50     }
51
52     if defined(Postgresql::Server::Role[$owner]) {
53       Postgresql::Server::Role[$owner]->Postgresql_psql["ALTER TABLESPACE \"${spcname}\" OWNER TO \"${owner}\""]
54     }
55   }
56 }