|
| 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 : |
0 commit comments