Allow the use of an ensure_absent_val param

This commit aims to add a new feature for the ini_setting provider, this
feature aims to simulate the ensure => absent behavior when a specific
keyword is specified.

Currently a pattern we have is

if $myvar {
  keystone_config { 'SECTION/setting' : value => $myvar }
} else {
  keystone_config { 'SECTION/setting' : ensure => absent }
}

If one has dozens or hundreds of parameters to handle then it can easily
make the manifest hard to read.

The solution offer here would turn the above example in something like

Keystone_config {
  ensure_absent_val = '<SERVICE DEFAULT>' # It is the default
}

keystone_config { 'SECTION/setting' : value => $myvar }

If `$myvar` is '<SERVICE DEFAULT>' then it will act as if `ensure => absent` would
have been specified.

Also added new tests for openstack_config provider

Co-Authored-By: Denis Egorenko <degorenko@mirantis.com>

Change-Id: I0eeebde3aac2662cc7e69bfad7f8d2481463a218
changes/74/202574/10
Yanis Guenane 7 years ago
parent 9ce94f1fa6
commit 3b85306d04
  1. 19
      lib/puppet/provider/openstack_config/ini_setting.rb
  2. 12
      lib/puppet_x/openstack/util/ini_file.rb
  3. 69
      spec/unit/provider/openstack_config/ini_setting_spec.rb

@ -1,11 +1,15 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),"..","..",".."))
require 'puppet_x/openstack/util/ini_file'
Puppet::Type.type(:openstack_config).provide(
:ini_setting,
:parent => Puppet::Type.type(:ini_setting).provider(:ruby)
) do
def exists?
if resource[:value] == ensure_absent_val
resource[:ensure] = :absent
end
super
end
def section
resource[:name].split('/', 2).first
end
@ -14,6 +18,10 @@ Puppet::Type.type(:openstack_config).provide(
resource[:name].split('/', 2).last
end
def ensure_absent_val
resource[:ensure_absent_val]
end
def separator
'='
end
@ -22,9 +30,4 @@ Puppet::Type.type(:openstack_config).provide(
self.class.file_path
end
private
def ini_file
@ini_file ||= PuppetX::Openstack::Util::IniFile.new(file_path, separator, section_prefix, section_suffix)
end
end

@ -1,12 +0,0 @@
require File.expand_path('../../../../../../inifile/lib/puppet/util/ini_file', __FILE__)
module PuppetX
module Openstack
module Util
class IniFile < Puppet::Util::IniFile
end
end
end
end

@ -0,0 +1,69 @@
#
# these tests are a little concerning b/c they are hacking around the
# modulepath, so these tests will not catch issues that may eventually arise
# related to loading these plugins.
# I could not, for the life of me, figure out how to programatcally set the modulepath
$LOAD_PATH.push(
File.join(
File.dirname(__FILE__),
'..',
'..',
'..',
'fixtures',
'modules',
'inifile',
'lib')
)
require 'spec_helper'
provider_class = Puppet::Type.type(:openstack_config).provider(:ini_setting)
describe provider_class do
let(:properties) do
{
:name => 'DEFAUL/foo',
:value => 'bar',
:ensure_absent_val => 'some_value',
:ensure => :present,
}
end
let(:type) do
Puppet::Type.newtype(:test_config) do
newparam(:name, :namevar => true)
newparam(:ensure)
newproperty(:value)
newparam(:ensure_absent_val)
end
end
let(:resource) do
resource = type.new(properties)
resource
end
context '#exists?' do
it 'ensure to present' do
child_conf = Class.new(provider_class) do
def self.file_path
'/some/file/path'
end
end
provider = child_conf.new(resource)
provider.exists?
expect(resource[:ensure]).to eq :present
end
it 'ensure to absent' do
child_conf = Class.new(provider_class) do
def self.file_path
'/some/file/path'
end
end
provider = child_conf.new(resource)
resource[:ensure_absent_val] = 'bar'
provider.exists?
expect(resource[:ensure]).to eq :absent
end
end
end
Loading…
Cancel
Save