Skip to content

Commit e9ad1cb

Browse files
committed
Allow adding roles, config entires and hba rules via hiera
1 parent c6aa480 commit e9ad1cb

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-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: 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 # rubocop:disable RSpec/ExampleLength
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

0 commit comments

Comments
 (0)