Skip to content

Commit b0de1ff

Browse files
committed
Merge pull request puppetlabs#950 from ekohl/hiera-defined-types
Allow adding roles, config entires and hba rules via hiera
2 parents 5c2bc6e + 27a4813 commit b0de1ff

File tree

12 files changed

+153
-19
lines changed

12 files changed

+153
-19
lines changed

lib/puppet/type/postgresql_psql.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ def matches(value)
120120
newvalues(:true, :false)
121121
end
122122

123+
autorequire(:class) { ['Postgresql::Server::Service'] }
124+
123125
def should_run_sql(refreshing = false)
124126
onlyif_param = @parameters[:onlyif]
125127
unless_param = @parameters[:unless]

manifests/server.pp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
$manage_pg_ident_conf = $postgresql::params::manage_pg_ident_conf,
5656
$manage_recovery_conf = $postgresql::params::manage_recovery_conf,
5757
$module_workdir = $postgresql::params::module_workdir,
58+
59+
Hash[String, Hash] $roles = {},
60+
Hash[String, Any] $config_entries = {},
61+
Hash[String, Hash] $pg_hba_rules = {},
62+
5863
#Deprecated
5964
$version = undef,
6065
) inherits postgresql::params {
@@ -74,11 +79,33 @@
7479
# Reload has its own ordering, specified by other defines
7580
class { "${pg}::reload": require => Class["${pg}::install"] }
7681

77-
anchor { "${pg}::start": }
78-
-> class { "${pg}::install": }
79-
-> class { "${pg}::initdb": }
80-
-> class { "${pg}::config": }
81-
-> class { "${pg}::service": }
82-
-> class { "${pg}::passwd": }
83-
-> anchor { "${pg}::end": }
82+
contain postgresql::server::install
83+
contain postgresql::server::initdb
84+
contain postgresql::server::config
85+
contain postgresql::server::service
86+
contain postgresql::server::passwd
87+
88+
Class['postgresql::server::install']
89+
-> Class['postgresql::server::initdb']
90+
-> Class['postgresql::server::config']
91+
-> Class['postgresql::server::service']
92+
-> Class['postgresql::server::passwd']
93+
94+
$roles.each |$rolename, $role| {
95+
postgresql::server::role { $rolename:
96+
* => $role,
97+
}
98+
}
99+
100+
$config_entries.each |$entry, $value| {
101+
postgresql::server::config_entry { $entry:
102+
value => $value,
103+
}
104+
}
105+
106+
$pg_hba_rules.each |$rule_name, $rule| {
107+
postgresql::server::pg_hba_rule { $rule_name:
108+
* => $rule,
109+
}
110+
}
84111
}

manifests/server/grant.pp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@
413413
psql_path => $psql_path,
414414
unless => $_unless,
415415
onlyif => $_onlyif,
416-
require => Class['postgresql::server']
417416
}
418417

419418
if($role != undef and defined(Postgresql::Server::Role[$role])) {

manifests/server/reassign_owned_by.pp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
psql_group => $group,
4646
psql_path => $psql_path,
4747
onlyif => $onlyif,
48-
require => Class['postgresql::server']
4948
}
5049

5150
if($old_role != undef and defined(Postgresql::Server::Role[$old_role])) {

manifests/server/role.pp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@
4747
psql_path => $psql_path,
4848
connect_settings => $connect_settings,
4949
cwd => $module_workdir,
50-
require => [
51-
Postgresql_psql["CREATE ROLE ${username} ENCRYPTED PASSWORD ****"],
52-
Class['postgresql::server'],
53-
],
50+
require => Postgresql_psql["CREATE ROLE ${username} ENCRYPTED PASSWORD ****"],
5451
}
5552

5653
if $ensure == 'present' {
@@ -72,7 +69,7 @@
7269
command => "CREATE ROLE \"${username}\" ${password_sql} ${login_sql} ${createrole_sql} ${createdb_sql} ${superuser_sql} ${replication_sql} CONNECTION LIMIT ${connection_limit}",
7370
unless => "SELECT 1 FROM pg_roles WHERE rolname = '${username}'",
7471
environment => $environment,
75-
require => Class['Postgresql::Server'],
72+
require => undef,
7673
}
7774

7875
postgresql_psql {"ALTER ROLE \"${username}\" ${superuser_sql}":
@@ -128,7 +125,7 @@
128125
# ensure == absent
129126
postgresql_psql { "DROP ROLE \"${username}\"":
130127
onlyif => "SELECT 1 FROM pg_roles WHERE rolname = '${username}'",
131-
require => Class['Postgresql::Server'],
128+
require => undef,
132129
}
133130
}
134131
}

manifests/server/tablespace.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
postgresql_psql { "CREATE TABLESPACE \"${spcname}\"":
4141
command => "CREATE TABLESPACE \"${spcname}\" LOCATION '${location}'",
4242
unless => "SELECT 1 FROM pg_tablespace WHERE spcname = '${spcname}'",
43-
require => [Class['postgresql::server'], File[$location]],
43+
require => File[$location],
4444
}
4545

4646
if $owner {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'spec_helper_acceptance'
2+
3+
# These tests are designed to ensure that the module, when ran overrides,
4+
# sets up everything correctly and allows us to connect to Postgres.
5+
describe 'postgresql::server', unless: UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do
6+
pp = <<-MANIFEST
7+
class { 'postgresql::server':
8+
roles => {
9+
'testusername' => {
10+
password_hash => postgresql_password('testusername', 'supersecret'),
11+
createdb => true,
12+
},
13+
},
14+
config_entries => {
15+
max_connections => 200,
16+
},
17+
pg_hba_rules => {
18+
'from_remote_host' => {
19+
type => 'host',
20+
database => 'mydb',
21+
user => 'myuser',
22+
auth_method => 'md5',
23+
address => '192.0.2.100/32',
24+
},
25+
},
26+
}
27+
28+
postgresql::server::database { 'testusername':
29+
owner => 'testusername',
30+
}
31+
MANIFEST
32+
33+
it 'with additional hiera entries' do
34+
apply_manifest(pp, catch_failures: true)
35+
apply_manifest(pp, catch_changes: true)
36+
end
37+
38+
describe port(5432) do
39+
it { is_expected.to be_listening }
40+
end
41+
42+
it 'can connect with psql' do
43+
psql('--command="\l" postgres', 'postgres') do |r|
44+
expect(r.stdout).to match(%r{List of databases})
45+
end
46+
end
47+
48+
it 'can connect with psql as testusername' do
49+
shell('PGPASSWORD=supersecret psql -U testusername -h localhost --command="\l"') do |r|
50+
expect(r.stdout).to match(%r{List of databases})
51+
end
52+
end
53+
end

spec/unit/classes/server_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,58 @@ class { 'postgresql::globals':
163163
is_expected.to contain_class('postgresql::repo').with_version('99.5')
164164
end
165165
end
166+
167+
describe 'additional roles' do
168+
let(:params) do
169+
{
170+
roles: {
171+
username: { createdb: true },
172+
},
173+
}
174+
end
175+
176+
it { is_expected.to compile.with_all_deps }
177+
it { is_expected.to contain_postgresql__server__role('username').with_createdb(true) }
178+
end
179+
180+
describe 'additional config_entries' do
181+
let(:params) do
182+
{
183+
config_entries: {
184+
fsync: 'off',
185+
checkpoint_segments: '20',
186+
},
187+
}
188+
end
189+
190+
it { is_expected.to compile.with_all_deps }
191+
it { is_expected.to contain_postgresql__server__config_entry('fsync').with_value('off') }
192+
it { is_expected.to contain_postgresql__server__config_entry('checkpoint_segments').with_value('20') }
193+
end
194+
195+
describe 'additional pg_hba_rules' do
196+
let(:params) do
197+
{
198+
pg_hba_rules: {
199+
from_remote_host: {
200+
type: 'host',
201+
database: 'mydb',
202+
user: 'myuser',
203+
auth_method: 'md5',
204+
address: '192.0.2.100',
205+
},
206+
},
207+
}
208+
end
209+
210+
it { is_expected.to compile.with_all_deps }
211+
it do
212+
is_expected.to contain_postgresql__server__pg_hba_rule('from_remote_host')
213+
.with_type('host')
214+
.with_database('mydb')
215+
.with_user('myuser')
216+
.with_auth_method('md5')
217+
.with_address('192.0.2.100')
218+
end
219+
end
166220
end

spec/unit/defines/server/grant_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class {'postgresql::server':}
210210
it { is_expected.to contain_postgresql__server__role('test') }
211211
it do
212212
is_expected.to contain_postgresql_psql('grant:test') \
213-
.that_requires('Postgresql::Server::Role[test]')
213+
.that_requires(['Class[postgresql::server::service]', 'Postgresql::Server::Role[test]'])
214214
end
215215
end
216216

spec/unit/defines/server/reassign_owned_by_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ class {'postgresql::server':}
3232
MANIFEST
3333
end
3434

35+
it { is_expected.to compile.with_all_deps }
3536
it { is_expected.to contain_postgresql__server__reassign_owned_by('test') }
3637

3738
it {
3839
is_expected.to contain_postgresql_psql('reassign_owned_by:test:REASSIGN OWNED BY "test_old_role" TO "test_new_role"')
3940
.with_command('REASSIGN OWNED BY "test_old_role" TO "test_new_role"')
4041
.with_onlyif(%r{SELECT tablename FROM pg_catalog.pg_tables WHERE\s*schemaname NOT IN \('pg_catalog', 'information_schema'\) AND\s*tableowner = 'test_old_role'.*}m)
41-
.that_requires('Class[postgresql::server]')
42+
.that_requires('Class[Postgresql::Server::Service]')
4243
}
4344
end

spec/unit/defines/server/role_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
.with_unless("SELECT 1 FROM pg_roles WHERE rolname = 'test'")
6767
.with_port(5432)
6868
.with_connect_settings('PGHOST' => 'postgres-db-server', 'DBVERSION' => '9.1', 'PGUSER' => 'login-user', 'PGPASSWORD' => 'login-pass')
69+
.that_requires('Class[postgresql::server::service]')
6970
end
7071
it 'has alter role for "test" user with password as ****' do
7172
is_expected.to contain_postgresql_psql('ALTER ROLE test ENCRYPTED PASSWORD ****')
@@ -138,7 +139,7 @@
138139
end
139140

140141
it 'has drop role for "test" user if ensure absent' do
141-
is_expected.to contain_postgresql_psql('DROP ROLE "test"')
142+
is_expected.to contain_postgresql_psql('DROP ROLE "test"').that_requires('Class[postgresql::server::service]')
142143
end
143144
end
144145
end

spec/unit/defines/server/tablespace_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
end
2929

3030
it { is_expected.to contain_postgresql__server__tablespace('test') }
31+
it { is_expected.to contain_postgresql_psql('CREATE TABLESPACE "test"').that_requires('Class[postgresql::server::service]') }
3132

3233
context 'with different owner' do
3334
let :params do

0 commit comments

Comments
 (0)