diff options
author | Chris Price <chris@puppetlabs.com> | 2013-03-25 11:00:41 -0700 |
---|---|---|
committer | Chris Price <chris@puppetlabs.com> | 2013-03-25 11:00:41 -0700 |
commit | e824ab2abae16e97f9a19d21580d6825c4bf705e (patch) | |
tree | 0a6655cc785d224cd186988101beb68c0556036b /lib | |
parent | 8064df6d004b7e24104c7899053a5c8627c35e1b (diff) | |
parent | 4351d8b9c8dac40f0f733fd7622d655241f113ba (diff) | |
download | puppet-inifile-e824ab2abae16e97f9a19d21580d6825c4bf705e.tar.gz puppet-inifile-e824ab2abae16e97f9a19d21580d6825c4bf705e.tar.bz2 |
Merge pull request #29 from kbrezina/subsetting
Added 'ini_subsetting' custom resource type
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/provider/ini_subsetting/ruby.rb | 70 | ||||
-rw-r--r-- | lib/puppet/type/ini_subsetting.rb | 55 | ||||
-rw-r--r-- | lib/puppet/util/setting_value.rb | 74 |
3 files changed, 199 insertions, 0 deletions
diff --git a/lib/puppet/provider/ini_subsetting/ruby.rb b/lib/puppet/provider/ini_subsetting/ruby.rb new file mode 100644 index 0000000..49c0e49 --- /dev/null +++ b/lib/puppet/provider/ini_subsetting/ruby.rb @@ -0,0 +1,70 @@ +require File.expand_path('../../../util/ini_file', __FILE__) +require File.expand_path('../../../util/setting_value', __FILE__) + +Puppet::Type.type(:ini_subsetting).provide(:ruby) do + + def exists? + setting_value.get_subsetting_value(subsetting) + end + + def create + setting_value.add_subsetting(subsetting, resource[:value]) + ini_file.set_value(section, setting, setting_value.get_value) + ini_file.save + @ini_file = nil + @setting_value = nil + end + + def destroy + setting_value.remove_subsetting(subsetting) + ini_file.set_value(section, setting, setting_value.get_value) + ini_file.save + @ini_file = nil + @setting_value = nil + end + + def value + setting_value.get_subsetting_value(subsetting) + end + + def value=(value) + setting_value.add_subsetting(subsetting, resource[:value]) + ini_file.set_value(section, setting, setting_value.get_value) + ini_file.save + end + + def section + resource[:section] + end + + def setting + resource[:setting] + end + + def subsetting + resource[:subsetting] + end + + def subsetting_separator + resource[:subsetting_separator] + end + + def file_path + resource[:path] + end + + def separator + resource[:key_val_separator] || '=' + end + + private + def ini_file + @ini_file ||= Puppet::Util::IniFile.new(file_path, separator) + end + + private + def setting_value + @setting_value ||= Puppet::Util::SettingValue.new(ini_file.get_value(section, setting), subsetting_separator) + end + +end diff --git a/lib/puppet/type/ini_subsetting.rb b/lib/puppet/type/ini_subsetting.rb new file mode 100644 index 0000000..dd146c2 --- /dev/null +++ b/lib/puppet/type/ini_subsetting.rb @@ -0,0 +1,55 @@ +Puppet::Type.newtype(:ini_subsetting) do + + ensurable do + defaultvalues + defaultto :present + end + + newparam(:name, :namevar => true) do + desc 'An arbitrary name used as the identity of the resource.' + end + + newparam(:section) do + desc 'The name of the section in the ini file in which the setting should be defined.' + end + + newparam(:setting) do + desc 'The name of the setting to be defined.' + end + + newparam(:subsetting) do + desc 'The name of the subsetting to be defined.' + end + + newparam(:subsetting_separator) do + desc 'The separator string between subsettings. Defaults to " "' + defaultto(" ") + end + + newparam(:path) do + desc 'The ini file Puppet will ensure contains the specified setting.' + validate do |value| + unless (Puppet.features.posix? and value =~ /^\//) or (Puppet.features.microsoft_windows? and (value =~ /^.:\// or value =~ /^\/\/[^\/]+\/[^\/]+/)) + raise(Puppet::Error, "File paths must be fully qualified, not '#{value}'") + end + end + end + + newparam(:key_val_separator) do + desc 'The separator string to use between each setting name and value. ' + + 'Defaults to " = ", but you could use this to override e.g. whether ' + + 'or not the separator should include whitespace.' + defaultto(" = ") + + validate do |value| + unless value.scan('=').size == 1 + raise Puppet::Error, ":key_val_separator must contain exactly one = character." + end + end + end + + newproperty(:value) do + desc 'The value of the subsetting to be defined.' + end + +end diff --git a/lib/puppet/util/setting_value.rb b/lib/puppet/util/setting_value.rb new file mode 100644 index 0000000..2112e56 --- /dev/null +++ b/lib/puppet/util/setting_value.rb @@ -0,0 +1,74 @@ +module Puppet +module Util + + class SettingValue + + def initialize(setting_value, subsetting_separator = ' ') + @setting_value = setting_value + @subsetting_separator = subsetting_separator + + if @setting_value + unquoted = setting_value[1, setting_value.length - 2] + @subsetting_items = unquoted.scan(Regexp.new("(?:(?:[^\\#{@subsetting_separator}]|\\.)+)")) # an item can contain escaped separator + @subsetting_items.map! { |item| item.strip } + else + @subsetting_items = [] + end + end + + def get_value + + result = "" + first = true + + @subsetting_items.each { |item| + result << @subsetting_separator unless first + result << item + first = false + } + + "\"" + result + "\"" + end + + def get_subsetting_value(subsetting) + + value = nil + + @subsetting_items.each { |item| + if(item.start_with?(subsetting)) + value = item[subsetting.length, item.length - subsetting.length] + break + end + } + + value + end + + def add_subsetting(subsetting, subsetting_value) + + new_item = subsetting + (subsetting_value || '') + found = false + + @subsetting_items.map! { |item| + if item.start_with?(subsetting) + value = new_item + found = true + else + value = item + end + + value + } + + unless found + @subsetting_items.push(new_item) + end + end + + def remove_subsetting(subsetting) + @subsetting_items = @subsetting_items.map { |item| item.start_with?(subsetting) ? nil : item }.compact + end + + end +end +end
\ No newline at end of file |