Skip to content

Commit

Permalink
Add insert_type and subsetting_key_val_separator
Browse files Browse the repository at this point in the history
* Insert_type and imsert value can define where
  a new subsetting element will be placed.
* subsetting_key_val_separator allows you to have
  a separator character between a subsetting name
  and its value.
* Refactor ini_subsetting and its spec.
  • Loading branch information
Dmitry Ilyin committed Jun 29, 2016
1 parent 95a8003 commit ab78c38
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 74 deletions.
18 changes: 18 additions & 0 deletions README.markdown
Expand Up @@ -409,6 +409,10 @@ Global show_diff configuraton takes priority over this one -

*Optional.* Specifies a string to use between subsettings. Valid options: a string. Default value: " ".

##### `subsetting_key_val_separator`

*Optional.* Specifies a string to use between subsetting name and value (if there is a separator between the subsetting name and its value). Valid options: a string. Default value: empty string.

##### `use_exact_match`

*Optional.* Whether to use partial or exact matching for subsetting. Should be set to true if the subsettings do not have values. Valid options: true, false. Default value: false.
Expand All @@ -417,6 +421,20 @@ Global show_diff configuraton takes priority over this one -

*Optional.* Supplies a value for the specified subsetting. Valid options: a string. Default value: undefined.

##### `insert_type`

*Optional.* Selects where a new subsetting item should be inserted.

* *start* - insert at the beginning of the line.
* *end* - insert at the end of the line (default).
* *before* - insert before the specified element if possible.
* *after* - insert after the specified element if possible.
* *index* - insert at the specified index number.

##### `insert_value`

*Optional.* The value for the insert type if the value if required.

### Function: create_ini_settings

Manages multiple `ini_setting` resources from a hash. Note that this cannot be used with ini_subsettings.
Expand Down
17 changes: 15 additions & 2 deletions examples/ini_subsetting.pp
@@ -1,5 +1,5 @@
ini_subsetting { 'sample subsetting':
ensure => present,
ensure => 'present',
section => '',
key_val_separator => '=',
path => '/etc/default/pe-puppetdb',
Expand All @@ -9,10 +9,23 @@
}

ini_subsetting { 'sample subsetting2':
ensure => absent,
ensure => 'absent',
section => '',
key_val_separator => '=',
path => '/etc/default/pe-puppetdb',
setting => 'JAVA_ARGS',
subsetting => '-Xms',
}

ini_subsetting { 'sample subsetting3':
ensure => 'present',
section => '',
key_val_separator => '=',
subsetting_key_val_separator => ':',
path => '/etc/default/pe-puppetdb',
setting => 'JAVA_ARGS',
subsetting => '-XX',
value => '+HeapDumpOnOutOfMemoryError',
insert_type => 'after',
insert_value => '-Xmx',
}
23 changes: 18 additions & 5 deletions lib/puppet/provider/ini_subsetting/ruby.rb
Expand Up @@ -8,7 +8,10 @@ def exists?
end

def create
setting_value.add_subsetting(subsetting, resource[:value], resource[:use_exact_match])
setting_value.add_subsetting(
subsetting, resource[:value], resource[:use_exact_match],
resource[:insert_type], resource[:insert_value]
)
ini_file.set_value(section, setting, setting_value.get_value)
ini_file.save
@ini_file = nil
Expand All @@ -28,7 +31,10 @@ def value
end

def value=(value)
setting_value.add_subsetting(subsetting, resource[:value], resource[:use_exact_match])
setting_value.add_subsetting(
subsetting, value, resource[:use_exact_match],
resource[:insert_type], resource[:insert_value]
)
ini_file.set_value(section, setting, setting_value.get_value)
ini_file.save
end
Expand All @@ -53,21 +59,28 @@ def file_path
resource[:path]
end

def separator
def key_val_separator
resource[:key_val_separator] || '='
end

def subsetting_key_val_separator
resource[:subsetting_key_val_separator] || ''
end

def quote_char
resource[:quote_char]
end

private
def ini_file
@ini_file ||= Puppet::Util::IniFile.new(file_path, separator)
@ini_file ||= Puppet::Util::IniFile.new(file_path, key_val_separator)
end

def setting_value
@setting_value ||= Puppet::Util::SettingValue.new(ini_file.get_value(section, setting), subsetting_separator, quote_char)
@setting_value ||= Puppet::Util::SettingValue.new(
ini_file.get_value(section, setting),
subsetting_separator, quote_char, subsetting_key_val_separator
)
end

end
24 changes: 24 additions & 0 deletions lib/puppet/type/ini_subsetting.rb
Expand Up @@ -43,6 +43,11 @@ def munge_boolean_md5(value)
defaultto(" ")
end

newparam(:subsetting_key_val_separator) do
desc 'The separator string between the subsetting name and its value. Defaults to the empty string.'
defaultto('')
end

newparam(:path) do
desc 'The ini file Puppet will ensure contains the specified setting.'
validate do |value|
Expand Down Expand Up @@ -109,4 +114,23 @@ def is_to_s(value)
end
end

newparam(:insert_type) do
desc <<-eof
Where the new subsetting item should be inserted?
* :start - insert at the beginning of the line.
* :end - insert at the end of the line (default).
* :before - insert before the specified element if possible.
* :after - insert after the specified element if possible.
* :index - insert at the specified index number.
eof

newvalues(:start, :end, :before, :after, :index)
defaultto(:end)
end

newparam(:insert_value) do
desc 'The value for the insert types which require one.'
end

end

0 comments on commit ab78c38

Please sign in to comment.