Skip to content

Commit c72bfbb

Browse files
committed
Merge pull request redhat-openstack#129 from duritong/create_ini_settings
introduce create_ini_settings
2 parents 4da2f6e + 9f03e8a commit c72bfbb

4 files changed

Lines changed: 151 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#
2+
# create_ini_settings.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:create_ini_settings, :type => :statement, :doc => <<-EOS
7+
Uses create_resources to create a set of ini_setting resources from a hash:
8+
9+
$settings = { section1 => {
10+
setting1 => val1
11+
},
12+
section2 => {
13+
setting2 => val2,
14+
setting3 => {
15+
ensure => absent
16+
}
17+
}
18+
}
19+
$defaults = {
20+
path => '/tmp/foo.ini'
21+
}
22+
create_ini_settings($settings,$defaults)
23+
24+
25+
Will create the following resources
26+
27+
ini_setting{'[section1] setting1':
28+
ensure => present,
29+
section => 'section1',
30+
setting => 'setting1',
31+
value => 'val1',
32+
path => '/tmp/foo.ini',
33+
}
34+
ini_setting{'[section2] setting2':
35+
ensure => present,
36+
section => 'section2',
37+
setting => 'setting2',
38+
value => 'val2',
39+
path => '/tmp/foo.ini',
40+
}
41+
ini_setting{'[section2] setting3':
42+
ensure => absent,
43+
section => 'section2',
44+
setting => 'setting3',
45+
path => '/tmp/foo.ini',
46+
}
47+
48+
EOS
49+
) do |arguments|
50+
51+
raise(Puppet::ParseError, "create_ini_settings(): Wrong number of arguments " +
52+
"given (#{arguments.size} for 1 or 2)") unless arguments.size.between?(1,2)
53+
54+
settings = arguments[0]
55+
defaults = arguments[1] || {}
56+
57+
if [settings,defaults].any?{|i| !i.is_a?(Hash) }
58+
raise(Puppet::ParseError,
59+
'create_ini_settings(): Requires all arguments to be a Hash')
60+
end
61+
62+
resources = settings.keys.inject({}) do |res, section|
63+
raise(Puppet::ParseError,
64+
"create_ini_settings(): Section #{section} must contain a Hash") \
65+
unless settings[section].is_a?(Hash)
66+
67+
settings[section].each do |setting, value|
68+
res["[#{section}] #{setting}"] = {
69+
'ensure' => 'present',
70+
'section' => section,
71+
'setting' => setting,
72+
}.merge(if value.is_a?(Hash)
73+
value
74+
else
75+
{ 'value' => value, }
76+
end)
77+
end
78+
res
79+
end
80+
81+
Puppet::Parser::Functions.function('create_resources')
82+
function_create_resources(['ini_setting',resources,defaults])
83+
end
84+
end
85+
86+
# vim: set ts=2 sw=2 et :
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'spec_helper'
2+
# end-to-end test of the create_init_settings function
3+
describe 'create_ini_settings_test' do
4+
it { should have_ini_setting_resource_count(3) }
5+
it { should contain_ini_setting('[section1] setting1').with(
6+
:ensure => 'present',
7+
:section => 'section1',
8+
:setting => 'setting1',
9+
:value => 'val1',
10+
:path => '/tmp/foo.ini'
11+
)}
12+
it { should contain_ini_setting('[section2] setting2').with(
13+
:ensure => 'present',
14+
:section => 'section2',
15+
:setting => 'setting2',
16+
:value => 'val2',
17+
:path => '/tmp/foo.ini'
18+
)}
19+
it { should contain_ini_setting('[section2] setting3').with(
20+
:ensure => 'absent',
21+
:section => 'section2',
22+
:setting => 'setting3',
23+
:path => '/tmp/foo.ini'
24+
)}
25+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# simple test class
2+
class create_ini_settings_test {
3+
$settings = { section1 => {
4+
setting1 => val1
5+
},
6+
section2 => {
7+
setting2 => val2,
8+
setting3 => {
9+
ensure => absent
10+
}
11+
}
12+
}
13+
$defaults = {
14+
path => '/tmp/foo.ini'
15+
}
16+
create_ini_settings($settings,$defaults)
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#! /usr/bin/env ruby
2+
3+
require 'spec_helper'
4+
require 'rspec-puppet'
5+
6+
describe 'create_ini_settings' do
7+
before :each do
8+
Puppet::Parser::Functions.autoloader.loadall
9+
Puppet::Parser::Functions.function(:create_resources)
10+
end
11+
12+
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/) }
17+
18+
it { should run.with_params({}) }
19+
it { should run.with_params({},{}) }
20+
21+
it { should run.with_params({ 1 => 2 }).and_raise_error(Puppet::ParseError, /Section 1 must contain a Hash/) }
22+
end
23+
end

0 commit comments

Comments
 (0)