Skip to content

Commit a94f51e

Browse files
author
David Danzilio
committed
Adding path to create_ini_settings resources
Currently the create_ini_settings function creates an ini_setting with a title comprised of the section and setting names. This means that we run into resource conflicts when defining the same section/setting combinations but in different files. Since the path parameter is required, we can safely add this to the title of the ini_setting resource created by the create_ini_settings function. This commit does just that.
1 parent de45163 commit a94f51e

4 files changed

Lines changed: 24 additions & 19 deletions

File tree

README.markdown

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ create_ini_settings($example, $defaults)
284284
~~~
285285
results in a resource
286286
~~~
287-
ini_setting { '[section1] setting1':
287+
ini_setting { '/tmp/foo.ini [section1] setting1':
288288
ensure => present,
289289
section => 'section1',
290290
setting => 'setting1',
@@ -307,14 +307,14 @@ create_ini_settings($example, $defaults)
307307
~~~
308308
results in resources
309309
~~~
310-
ini_setting { '[section1] setting1':
310+
ini_setting { '/tmp/foo.ini [section1] setting1':
311311
ensure => present,
312312
section => 'section1',
313313
setting => 'setting1',
314314
value => 'value1',
315315
path => '/tmp/foo.ini',
316316
}
317-
ini_setting { '[section1] setting2':
317+
ini_setting { '/tmp/foo.ini [section1] setting2':
318318
ensure => absent,
319319
section => 'section1',
320320
setting => 'setting2',
@@ -324,12 +324,12 @@ ini_setting { '[section1] setting2':
324324

325325
##### `$defaults`
326326

327-
*Optional, but recommended.*
327+
*Optional, but recommended.*
328328

329329
This works exactly like `create_resources` defaults parameter. Use it to not repeat yourself to often
330330
and write settings more densely. Example usage see parameter `$settings` above.
331331

332-
If you omit this parameter, you will need to add the `path` and `value` attribute to every single setting as a hash
332+
If you omit this parameter, you will need to add the `path` and `value` attribute to every single setting as a hash
333333
(`$example = { 'section1' => { 'setting1' => { 'value' => 'value1', 'path' => '/tmp/foo.ini' } } }`).
334334
This most certainly is not what you want, but if you need it it's there.
335335

lib/puppet/parser/functions/create_ini_settings.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ module Puppet::Parser::Functions
2424
2525
Will create the following resources
2626
27-
ini_setting{'[section1] setting1':
27+
ini_setting{'/tmp/foo.ini [section1] setting1':
2828
ensure => present,
2929
section => 'section1',
3030
setting => 'setting1',
3131
value => 'val1',
3232
path => '/tmp/foo.ini',
3333
}
34-
ini_setting{'[section2] setting2':
34+
ini_setting{'/tmp/foo.ini [section2] setting2':
3535
ensure => present,
3636
section => 'section2',
3737
setting => 'setting2',
3838
value => 'val2',
3939
path => '/tmp/foo.ini',
4040
}
41-
ini_setting{'[section2] setting3':
41+
ini_setting{'/tmp/foo.ini [section2] setting3':
4242
ensure => absent,
4343
section => 'section2',
4444
setting => 'setting3',
@@ -64,8 +64,12 @@ module Puppet::Parser::Functions
6464
"create_ini_settings(): Section #{section} must contain a Hash") \
6565
unless settings[section].is_a?(Hash)
6666

67+
unless path = defaults.merge(settings)['path']
68+
raise Puppet::ParseError, 'create_ini_settings(): must pass the path parameter to the Ini_setting resource!'
69+
end
70+
6771
settings[section].each do |setting, value|
68-
res["[#{section}] #{setting}"] = {
72+
res["#{path} [#{section}] #{setting}"] = {
6973
'ensure' => 'present',
7074
'section' => section,
7175
'setting' => setting,

spec/classes/create_ini_settings_test_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
# end-to-end test of the create_init_settings function
33
describe 'create_ini_settings_test' do
44
it { should have_ini_setting_resource_count(3) }
5-
it { should contain_ini_setting('[section1] setting1').with(
5+
it { should contain_ini_setting('/tmp/foo.ini [section1] setting1').with(
66
:ensure => 'present',
77
:section => 'section1',
88
:setting => 'setting1',
99
:value => 'val1',
1010
:path => '/tmp/foo.ini'
1111
)}
12-
it { should contain_ini_setting('[section2] setting2').with(
12+
it { should contain_ini_setting('/tmp/foo.ini [section2] setting2').with(
1313
:ensure => 'present',
1414
:section => 'section2',
1515
:setting => 'setting2',
1616
:value => 'val2',
1717
:path => '/tmp/foo.ini'
1818
)}
19-
it { should contain_ini_setting('[section2] setting3').with(
19+
it { should contain_ini_setting('/tmp/foo.ini [section2] setting3').with(
2020
:ensure => 'absent',
2121
:section => 'section2',
2222
:setting => 'setting3',

spec/functions/create_ini_settings_spec.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
end
1111

1212
describe 'argument handling' do
13-
it { should run.with_params.and_raise_error(Puppet::ParseError, /0 for 1 or 2/) }
14-
it { should run.with_params(1,2,3).and_raise_error(Puppet::ParseError, /3 for 1 or 2/) }
15-
it { should run.with_params('foo').and_raise_error(Puppet::ParseError, /Requires all arguments/) }
16-
it { should run.with_params({},'foo').and_raise_error(Puppet::ParseError, /Requires all arguments/) }
13+
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, /0 for 1 or 2/) }
14+
it { is_expected.to run.with_params(1,2,3).and_raise_error(Puppet::ParseError, /3 for 1 or 2/) }
15+
it { is_expected.to run.with_params('foo').and_raise_error(Puppet::ParseError, /Requires all arguments/) }
16+
it { is_expected.to run.with_params({},'foo').and_raise_error(Puppet::ParseError, /Requires all arguments/) }
1717

18-
it { should run.with_params({}) }
19-
it { should run.with_params({},{}) }
18+
it { is_expected.to run.with_params({}) }
19+
it { is_expected.to run.with_params({},{}) }
2020

21-
it { should run.with_params({ 1 => 2 }).and_raise_error(Puppet::ParseError, /Section 1 must contain a Hash/) }
21+
it { is_expected.to run.with_params({ 'section' => { 'setting' => 'value' }}).and_raise_error(Puppet::ParseError, /must pass the path parameter/) }
22+
it { is_expected.to run.with_params({ 1 => 2 }).and_raise_error(Puppet::ParseError, /Section 1 must contain a Hash/) }
2223
end
2324
end

0 commit comments

Comments
 (0)