From a94f51e68dca6f2eef877d9703da1e83eacfc74d Mon Sep 17 00:00:00 2001 From: David Danzilio Date: Wed, 12 Aug 2015 18:00:57 -0400 Subject: [PATCH] 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. --- README.markdown | 10 +++++----- .../parser/functions/create_ini_settings.rb | 12 ++++++++---- spec/classes/create_ini_settings_test_spec.rb | 6 +++--- spec/functions/create_ini_settings_spec.rb | 15 ++++++++------- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/README.markdown b/README.markdown index ece5330..bdaa517 100644 --- a/README.markdown +++ b/README.markdown @@ -284,7 +284,7 @@ create_ini_settings($example, $defaults) ~~~ results in a resource ~~~ -ini_setting { '[section1] setting1': +ini_setting { '/tmp/foo.ini [section1] setting1': ensure => present, section => 'section1', setting => 'setting1', @@ -307,14 +307,14 @@ create_ini_settings($example, $defaults) ~~~ results in resources ~~~ -ini_setting { '[section1] setting1': +ini_setting { '/tmp/foo.ini [section1] setting1': ensure => present, section => 'section1', setting => 'setting1', value => 'value1', path => '/tmp/foo.ini', } -ini_setting { '[section1] setting2': +ini_setting { '/tmp/foo.ini [section1] setting2': ensure => absent, section => 'section1', setting => 'setting2', @@ -324,12 +324,12 @@ ini_setting { '[section1] setting2': ##### `$defaults` -*Optional, but recommended.* +*Optional, but recommended.* This works exactly like `create_resources` defaults parameter. Use it to not repeat yourself to often and write settings more densely. Example usage see parameter `$settings` above. -If you omit this parameter, you will need to add the `path` and `value` attribute to every single setting as a hash +If you omit this parameter, you will need to add the `path` and `value` attribute to every single setting as a hash (`$example = { 'section1' => { 'setting1' => { 'value' => 'value1', 'path' => '/tmp/foo.ini' } } }`). This most certainly is not what you want, but if you need it it's there. diff --git a/lib/puppet/parser/functions/create_ini_settings.rb b/lib/puppet/parser/functions/create_ini_settings.rb index 8e14591..f0eff6d 100644 --- a/lib/puppet/parser/functions/create_ini_settings.rb +++ b/lib/puppet/parser/functions/create_ini_settings.rb @@ -24,21 +24,21 @@ Uses create_resources to create a set of ini_setting resources from a hash: Will create the following resources - ini_setting{'[section1] setting1': + ini_setting{'/tmp/foo.ini [section1] setting1': ensure => present, section => 'section1', setting => 'setting1', value => 'val1', path => '/tmp/foo.ini', } - ini_setting{'[section2] setting2': + ini_setting{'/tmp/foo.ini [section2] setting2': ensure => present, section => 'section2', setting => 'setting2', value => 'val2', path => '/tmp/foo.ini', } - ini_setting{'[section2] setting3': + ini_setting{'/tmp/foo.ini [section2] setting3': ensure => absent, section => 'section2', setting => 'setting3', @@ -64,8 +64,12 @@ EOS "create_ini_settings(): Section #{section} must contain a Hash") \ unless settings[section].is_a?(Hash) + unless path = defaults.merge(settings)['path'] + raise Puppet::ParseError, 'create_ini_settings(): must pass the path parameter to the Ini_setting resource!' + end + settings[section].each do |setting, value| - res["[#{section}] #{setting}"] = { + res["#{path} [#{section}] #{setting}"] = { 'ensure' => 'present', 'section' => section, 'setting' => setting, diff --git a/spec/classes/create_ini_settings_test_spec.rb b/spec/classes/create_ini_settings_test_spec.rb index 4e6683a..c81f93f 100644 --- a/spec/classes/create_ini_settings_test_spec.rb +++ b/spec/classes/create_ini_settings_test_spec.rb @@ -2,21 +2,21 @@ require 'spec_helper' # end-to-end test of the create_init_settings function describe 'create_ini_settings_test' do it { should have_ini_setting_resource_count(3) } - it { should contain_ini_setting('[section1] setting1').with( + it { should contain_ini_setting('/tmp/foo.ini [section1] setting1').with( :ensure => 'present', :section => 'section1', :setting => 'setting1', :value => 'val1', :path => '/tmp/foo.ini' )} - it { should contain_ini_setting('[section2] setting2').with( + it { should contain_ini_setting('/tmp/foo.ini [section2] setting2').with( :ensure => 'present', :section => 'section2', :setting => 'setting2', :value => 'val2', :path => '/tmp/foo.ini' )} - it { should contain_ini_setting('[section2] setting3').with( + it { should contain_ini_setting('/tmp/foo.ini [section2] setting3').with( :ensure => 'absent', :section => 'section2', :setting => 'setting3', diff --git a/spec/functions/create_ini_settings_spec.rb b/spec/functions/create_ini_settings_spec.rb index e40fb09..4fe817f 100644 --- a/spec/functions/create_ini_settings_spec.rb +++ b/spec/functions/create_ini_settings_spec.rb @@ -10,14 +10,15 @@ describe 'create_ini_settings' do end describe 'argument handling' do - it { should run.with_params.and_raise_error(Puppet::ParseError, /0 for 1 or 2/) } - it { should run.with_params(1,2,3).and_raise_error(Puppet::ParseError, /3 for 1 or 2/) } - it { should run.with_params('foo').and_raise_error(Puppet::ParseError, /Requires all arguments/) } - it { should run.with_params({},'foo').and_raise_error(Puppet::ParseError, /Requires all arguments/) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, /0 for 1 or 2/) } + it { is_expected.to run.with_params(1,2,3).and_raise_error(Puppet::ParseError, /3 for 1 or 2/) } + it { is_expected.to run.with_params('foo').and_raise_error(Puppet::ParseError, /Requires all arguments/) } + it { is_expected.to run.with_params({},'foo').and_raise_error(Puppet::ParseError, /Requires all arguments/) } - it { should run.with_params({}) } - it { should run.with_params({},{}) } + it { is_expected.to run.with_params({}) } + it { is_expected.to run.with_params({},{}) } - it { should run.with_params({ 1 => 2 }).and_raise_error(Puppet::ParseError, /Section 1 must contain a Hash/) } + it { is_expected.to run.with_params({ 'section' => { 'setting' => 'value' }}).and_raise_error(Puppet::ParseError, /must pass the path parameter/) } + it { is_expected.to run.with_params({ 1 => 2 }).and_raise_error(Puppet::ParseError, /Section 1 must contain a Hash/) } end end