From ede74d1a6717045c3e41e966c975e6853c1a8d6b Mon Sep 17 00:00:00 2001 From: Alan Petersen Date: Mon, 14 Mar 2016 14:43:54 +0800 Subject: [PATCH] Added refreshonly Parameter Working with a recent customer, they had a product whose ini files have timestamps in them associated with particular settings. When those settings are updated, it is expected that the timestamp be updated as well. To do this with ini_setting, we need it to respond to refresh events and have a way of only updating a value if a refresh event occurs. Hence the little modification to make this possible. --- README.markdown | 27 +++++++++++++++++++++++++++ lib/puppet/type/ini_setting.rb | 23 +++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/README.markdown b/README.markdown index dcc9523..80fba7b 100644 --- a/README.markdown +++ b/README.markdown @@ -331,6 +331,33 @@ Global show_diff configuraton takes priority over this one - *Optional.* Designates the string that will appear after the section's name. Default value: "]". +##### `refreshonly` + +*Optional.* A boolean to indicate whether or not the value associated with the setting should be updated if this resource is only part of a refresh event. Default value: **false**. + +For example, if we want a timestamp associated with the last time a setting's value was updated, we could do something like this: + +~~~ +ini_setting { 'foosetting': + ensure => present, + path => '/tmp/file.ini', + section => 'foo', + setting => 'foosetting', + value => 'bar', + notify => Ini_Setting['foosetting_timestamp'], +} + +$now = strftime('%Y-%m-%d %H:%M:%S') +ini_setting {'foosetting_timestamp': + ensure => present, + path => '/tmp/file.ini', + section => 'foo', + setting => 'foosetting_timestamp', + value => $now, + refreshonly => true, +} +~~~ + **NOTE:** This type finds all sections in the file by looking for lines like `${section_prefix}${title}${section_suffix}`. ### Type: ini_subsetting diff --git a/lib/puppet/type/ini_setting.rb b/lib/puppet/type/ini_setting.rb index 79fc934..850fddf 100644 --- a/lib/puppet/type/ini_setting.rb +++ b/lib/puppet/type/ini_setting.rb @@ -88,6 +88,15 @@ Puppet::Type.newtype(:ini_setting) do def is_to_s(value) should_to_s(value) end + + def insync?(current) + if (@resource[:refreshonly]) then + true + else + current == should + end + end + end newparam(:section_prefix) do @@ -102,4 +111,18 @@ Puppet::Type.newtype(:ini_setting) do defaultto(']') end + newparam(:refreshonly) do + desc 'A flag indicating whether or not the ini_setting should be updated '+ + 'only when called as part of a refresh event' + defaultto false + newvalues(true,false) + end + + def refresh + if self[:refreshonly] then + # update the value in the provider, which will save the value to the ini file + provider.value = self[:value] + end + end + end