|
1 | 1 | # |
2 | 2 | # create_ini_settings.rb |
3 | 3 | # |
4 | | - |
5 | 4 | 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: |
| 5 | + newfunction(:create_ini_settings, type: :statement, doc: <<-EOS |
| 6 | + Uses create_resources to create a set of ini_setting resources from a hash: |
8 | 7 |
|
9 | | - $settings = { section1 => { |
10 | | - setting1 => val1 |
11 | | - }, |
12 | | - section2 => { |
13 | | - setting2 => val2, |
14 | | - setting3 => { |
15 | | - ensure => absent |
| 8 | + $settings = { section1 => { |
| 9 | + setting1 => val1 |
| 10 | + }, |
| 11 | + section2 => { |
| 12 | + setting2 => val2, |
| 13 | + setting3 => { |
| 14 | + ensure => absent |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + $defaults = { |
| 19 | + path => '/tmp/foo.ini' |
16 | 20 | } |
17 | | - } |
18 | | - } |
19 | | - $defaults = { |
20 | | - path => '/tmp/foo.ini' |
21 | | - } |
22 | | - create_ini_settings($settings,$defaults) |
| 21 | + create_ini_settings($settings,$defaults) |
23 | 22 |
|
24 | 23 |
|
25 | | -Will create the following resources |
| 24 | + Will create the following resources |
26 | 25 |
|
27 | | - ini_setting{'/tmp/foo.ini [section1] setting1': |
28 | | - ensure => present, |
29 | | - section => 'section1', |
30 | | - setting => 'setting1', |
31 | | - value => 'val1', |
32 | | - path => '/tmp/foo.ini', |
33 | | - } |
34 | | - ini_setting{'/tmp/foo.ini [section2] setting2': |
35 | | - ensure => present, |
36 | | - section => 'section2', |
37 | | - setting => 'setting2', |
38 | | - value => 'val2', |
39 | | - path => '/tmp/foo.ini', |
40 | | - } |
41 | | - ini_setting{'/tmp/foo.ini [section2] setting3': |
42 | | - ensure => absent, |
43 | | - section => 'section2', |
44 | | - setting => 'setting3', |
45 | | - path => '/tmp/foo.ini', |
46 | | - } |
| 26 | + ini_setting{'/tmp/foo.ini [section1] setting1': |
| 27 | + ensure => present, |
| 28 | + section => 'section1', |
| 29 | + setting => 'setting1', |
| 30 | + value => 'val1', |
| 31 | + path => '/tmp/foo.ini', |
| 32 | + } |
| 33 | + ini_setting{'/tmp/foo.ini [section2] setting2': |
| 34 | + ensure => present, |
| 35 | + section => 'section2', |
| 36 | + setting => 'setting2', |
| 37 | + value => 'val2', |
| 38 | + path => '/tmp/foo.ini', |
| 39 | + } |
| 40 | + ini_setting{'/tmp/foo.ini [section2] setting3': |
| 41 | + ensure => absent, |
| 42 | + section => 'section2', |
| 43 | + setting => 'setting3', |
| 44 | + path => '/tmp/foo.ini', |
| 45 | + } |
47 | 46 |
|
48 | | -EOS |
49 | | - ) do |arguments| |
| 47 | + EOS |
| 48 | + ) do |arguments| |
50 | 49 |
|
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) |
| 50 | + unless arguments.size.between?(1, 2) |
| 51 | + raise(Puppet::ParseError, 'create_ini_settings(): Wrong number of arguments ' \ |
| 52 | + "given (#{arguments.size} for 1 or 2)") |
| 53 | + end |
53 | 54 |
|
54 | 55 | settings = arguments[0] |
55 | 56 | defaults = arguments[1] || {} |
56 | 57 |
|
57 | | - if [settings,defaults].any?{|i| !i.is_a?(Hash) } |
| 58 | + if [settings, defaults].any? { |i| !i.is_a?(Hash) } |
58 | 59 | raise(Puppet::ParseError, |
59 | | - 'create_ini_settings(): Requires all arguments to be a Hash') |
| 60 | + 'create_ini_settings(): Requires all arguments to be a Hash') |
60 | 61 | end |
61 | 62 |
|
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 | | - unless path = defaults.merge(settings)['path'] |
68 | | - raise Puppet::ParseError, 'create_ini_settings(): must pass the path parameter to the Ini_setting resource!' |
| 63 | + resources = settings.keys.each_with_object({}) do |section, res| |
| 64 | + unless settings[section].is_a?(Hash) |
| 65 | + raise(Puppet::ParseError, |
| 66 | + "create_ini_settings(): Section #{section} must contain a Hash") |
69 | 67 | end |
70 | 68 |
|
| 69 | + path = defaults.merge(settings)['path'] |
| 70 | + raise Puppet::ParseError, 'create_ini_settings(): must pass the path parameter to the Ini_setting resource!' unless path |
| 71 | + |
71 | 72 | settings[section].each do |setting, value| |
72 | 73 | res["#{path} [#{section}] #{setting}"] = { |
73 | 74 | 'ensure' => 'present', |
74 | 75 | 'section' => section, |
75 | 76 | 'setting' => setting, |
76 | 77 | }.merge(if value.is_a?(Hash) |
77 | | - value |
78 | | - else |
79 | | - { 'value' => value, } |
80 | | - end) |
| 78 | + value |
| 79 | + else |
| 80 | + { 'value' => value } |
| 81 | + end) |
81 | 82 | end |
82 | | - res |
83 | 83 | end |
84 | 84 |
|
85 | 85 | Puppet::Parser::Functions.function('create_resources') |
86 | | - function_create_resources(['ini_setting',resources,defaults]) |
| 86 | + function_create_resources(['ini_setting', resources, defaults]) |
87 | 87 | end |
88 | 88 | end |
89 | 89 |
|
|
0 commit comments