From c1f45306343cdd28385df3578520e83fb7a4e0ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Conil?= Date: Wed, 2 Sep 2015 10:33:46 +1000 Subject: [PATCH] Fixes MODULES_2059 - adds extension argument This adds the ability to define the extension name separately from the "title" of the resource, which allows you to add the extension to more than one database. As per the original ticket, extensions in postgresql can be defined on a per database basis. By using the same name for both the extension and the instance of postgresql::server::extension, you're getting duplicates errors if you try to assign an extension to more than one database --- README.md | 5 ++- manifests/server/extension.pp | 19 +++++----- spec/unit/defines/server/extension_spec.rb | 43 ++++++++++++++++++++-- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f6ee241..584970e 100644 --- a/README.md +++ b/README.md @@ -679,7 +679,10 @@ OS user for running `psql`. Defaults to the default user for the module, usually Hash of environment variable used when connecting to a remote server. Defaults to connecting to the local Postgres instance. ###Resource: postgresql::server::extension -Manages a postgresql extension. +This defined type manages a postgresql extension for a given database. + +####`extension` +The extension to activate. If left blank, will use the name of the resource. ####`database` The database on which to activate the extension. diff --git a/manifests/server/extension.pp b/manifests/server/extension.pp index fda2b39..14b51f2 100644 --- a/manifests/server/extension.pp +++ b/manifests/server/extension.pp @@ -1,6 +1,7 @@ # Activate an extension on a postgresql database define postgresql::server::extension ( $database, + $extension = $name, $ensure = 'present', $package_name = undef, $package_ensure = undef, @@ -12,16 +13,16 @@ define postgresql::server::extension ( case $ensure { 'present': { - $command = "CREATE EXTENSION \"${name}\"" + $command = "CREATE EXTENSION \"${extension}\"" $unless_comp = '=' $package_require = undef - $package_before = Postgresql_psql["Add ${title} extension to ${database}"] + $package_before = Postgresql_psql["Add ${extension} extension to ${database}"] } 'absent': { - $command = "DROP EXTENSION \"${name}\"" + $command = "DROP EXTENSION \"${extension}\"" $unless_comp = '!=' - $package_require = Postgresql_psql["Add ${title} extension to ${database}"] + $package_require = Postgresql_psql["Add ${extension} extension to ${database}"] $package_before = undef } @@ -30,7 +31,8 @@ define postgresql::server::extension ( } } - postgresql_psql {"Add ${title} extension to ${database}": + + postgresql_psql {"Add ${extension} extension to ${database}": psql_user => $user, psql_group => $group, @@ -39,7 +41,7 @@ define postgresql::server::extension ( db => $database, command => $command, - unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = '${name}') as t WHERE t.count ${unless_comp} 1", + unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = '${extension}') as t WHERE t.count ${unless_comp} 1", require => Postgresql::Server::Database[$database], } @@ -49,12 +51,11 @@ define postgresql::server::extension ( default => $package_ensure, } - package { "Postgresql extension ${title}": + ensure_packages($package_name, { ensure => $_package_ensure, - name => $package_name, tag => 'postgresql', require => $package_require, before => $package_before, - } + }) } } diff --git a/spec/unit/defines/server/extension_spec.rb b/spec/unit/defines/server/extension_spec.rb index 306326d..54386a1 100644 --- a/spec/unit/defines/server/extension_spec.rb +++ b/spec/unit/defines/server/extension_spec.rb @@ -41,7 +41,7 @@ describe 'postgresql::server::extension', :type => :define do }) } it { - is_expected.to contain_package('Postgresql extension postgis').with({ + is_expected.to contain_package('postgis').with({ :ensure => 'present', :name => 'postgis', }).that_comes_before('Postgresql_psql[Add postgis extension to template_postgis]') @@ -63,7 +63,7 @@ describe 'postgresql::server::extension', :type => :define do } it { - is_expected.to contain_package('Postgresql extension postgis').with({ + is_expected.to contain_package('postgis').with({ :ensure => 'absent', :name => 'postgis', }) @@ -83,7 +83,7 @@ describe 'postgresql::server::extension', :type => :define do } it { - is_expected.to contain_package('Postgresql extension postgis').with({ + is_expected.to contain_package('postgis').with({ :ensure => 'present', :name => 'postgis', }).that_requires('Postgresql_psql[Add postgis extension to template_postgis]') @@ -91,3 +91,40 @@ describe 'postgresql::server::extension', :type => :define do end end end + +describe 'postgresql::server::extension', :type => :define do + let :pre_condition do + "class { 'postgresql::server': } + postgresql::server::database { 'template_postgis2': + template => 'template1', + }" + end + + let :facts do + { + :osfamily => 'Debian', + :operatingsystem => 'Debian', + :operatingsystemrelease => '6.0', + :kernel => 'Linux', + :concat_basedir => tmpfilename('postgis'), + :id => 'root', + :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', + } + end + + let (:title) { 'postgis_db2' } + let (:params) { { + :database => 'template_postgis2', + :extension => 'postgis', + } } + + context "with mandatory arguments only" do + it { + is_expected.to contain_postgresql_psql('Add postgis extension to template_postgis2').with({ + :db => 'template_postgis2', + :command => 'CREATE EXTENSION "postgis"', + :unless => "SELECT t.count FROM (SELECT count(extname) FROM pg_extension WHERE extname = 'postgis') as t WHERE t.count = 1", + }).that_requires('Postgresql::Server::Database[template_postgis2]') + } + end +end