Skip to content

Commit 4bba246

Browse files
committed
Allow adding roles, config entires and hba rules via hiera
1 parent 16ded4e commit 4bba246

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

manifests/server.pp

Lines changed: 23 additions & 0 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 {
@@ -85,4 +90,22 @@
8590
-> Class['postgresql::server::config']
8691
-> Class['postgresql::server::service']
8792
-> 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+
}
88111
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
it 'with defaults' do
7+
pp = <<-EOS
8+
class { 'postgresql::server':
9+
roles => {
10+
'testusername' => {
11+
password_hash => postgresql_password('testusername', 'supersecret'),
12+
createdb => true,
13+
},
14+
},
15+
config_entries => {
16+
max_connections => 200,
17+
},
18+
pg_hba_rules => {
19+
'from_remote_host' => {
20+
type => 'host',
21+
database => 'mydb',
22+
user => 'myuser',
23+
auth_method => 'md5',
24+
address => '192.0.2.100/32',
25+
},
26+
},
27+
}
28+
29+
postgresql::server::database { 'testusername':
30+
owner => 'testusername',
31+
}
32+
EOS
33+
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(/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(/List of databases/)
51+
end
52+
end
53+
54+
end

spec/unit/classes/server_spec.rb

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

0 commit comments

Comments
 (0)