diff --git a/manifests/server/tablespace.pp b/manifests/server/tablespace.pp index 37972e0..cf0b65d 100644 --- a/manifests/server/tablespace.pp +++ b/manifests/server/tablespace.pp @@ -1,55 +1,56 @@ # This module creates tablespace. See README.md for more details. define postgresql::server::tablespace( $location, $owner = undef, $spcname = $title, $connect_settings = $postgresql::server::default_connect_settings, ) { $user = $postgresql::server::user $group = $postgresql::server::group $psql_path = $postgresql::server::psql_path $module_workdir = $postgresql::server::module_workdir # If the connection settings do not contain a port, then use the local server port if $connect_settings != undef and has_key( $connect_settings, 'PGPORT') { $port = undef } else { $port = $postgresql::server::port } Postgresql_psql { psql_user => $user, psql_group => $group, psql_path => $psql_path, port => $port, connect_settings => $connect_settings, cwd => $module_workdir, } - if $owner == undef { - $owner_section = '' - } else { - $owner_section = "OWNER \"${owner}\"" - } - file { $location: ensure => directory, owner => $user, group => $group, mode => '0700', seluser => 'system_u', selrole => 'object_r', seltype => 'postgresql_db_t', require => Class['postgresql::server'], } postgresql_psql { "CREATE TABLESPACE \"${spcname}\"": - command => "CREATE TABLESPACE \"${spcname}\" ${owner_section} LOCATION '${location}'", + command => "CREATE TABLESPACE \"${spcname}\" LOCATION '${location}'", unless => "SELECT 1 FROM pg_tablespace WHERE spcname = '${spcname}'", require => [Class['postgresql::server'], File[$location]], } - if $owner != undef and defined(Postgresql::Server::Role[$owner]) { - Postgresql::Server::Role[$owner]->Postgresql_psql["CREATE TABLESPACE \"${spcname}\""] + if $owner { + postgresql_psql { "ALTER TABLESPACE \"${spcname}\" OWNER TO \"${owner}\"": + unless => "SELECT 1 FROM pg_tablespace JOIN pg_roles rol ON spcowner = rol.oid WHERE spcname = '${spcname}' AND rolname = '${owner}'", + require => Postgresql_psql["CREATE TABLESPACE \"${spcname}\""], + } + + if defined(Postgresql::Server::Role[$owner]) { + Postgresql::Server::Role[$owner]->Postgresql_psql["ALTER TABLESPACE \"${spcname}\" OWNER TO \"${owner}\""] + } } } diff --git a/spec/unit/defines/server/tablespace_spec.rb b/spec/unit/defines/server/tablespace_spec.rb index 560a50e..50a93e2 100644 --- a/spec/unit/defines/server/tablespace_spec.rb +++ b/spec/unit/defines/server/tablespace_spec.rb @@ -1,31 +1,42 @@ require 'spec_helper' describe 'postgresql::server::tablespace', :type => :define do let :facts do { :osfamily => 'Debian', :operatingsystem => 'Debian', :operatingsystemrelease => '6.0', :kernel => 'Linux', :concat_basedir => tmpfilename('tablespace'), :id => 'root', :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', } end let :title do 'test' end let :params do { :location => '/srv/data/foo', } end let :pre_condition do "class {'postgresql::server':}" end it { is_expected.to contain_postgresql__server__tablespace('test') } + + context "with different owner" do + let :params do + { + :location => '/srv/data/foo', + :owner => 'test_owner', + } + end + + it { is_expected.to contain_postgresql_psql('ALTER TABLESPACE "test" OWNER TO "test_owner"') } + end end