Making immutable config setting when using <_IMMUTABLE_>.

Similar to what is done with os_service_default, but here we have the
possibility to tell puppet to not change the existing value, whatever
this is.

The associated fact for syntactic sugar in the module is
`::os_immutable`.

Partial-Bug: #1763322
Change-Id: Iaea44309db9b1b075425fa15890ba592d8bc9b7e
This commit is contained in:
Sofer Athlan-Guyot 2018-06-29 16:47:56 +02:00
parent 0db6725c25
commit 124daec7bb
5 changed files with 84 additions and 0 deletions

1
facts.d/os_immutable.txt Normal file
View File

@ -0,0 +1 @@
os_immutable=<_IMMUTABLE_>

View File

@ -1,11 +1,20 @@
require 'facter'
Puppet::Type.type(:openstack_config).provide(
:ini_setting,
:parent => Puppet::Type.type(:ini_setting).provider(:ruby)
) do
def exists?
immutable_string = Facter.value(:os_immutable) || '<_IMMUTABLE_>'
if resource[:value] == ensure_absent_val
resource[:ensure] = :absent
elsif resource[:value] == immutable_string or resource[:value] == [immutable_string]
resource[:value] = value
# when the value is undefined, we keep it that way.
if value.nil? or (value.kind_of?(Array) and value[0].nil?)
resource[:ensure] = :absent
end
end
super
end

View File

@ -1,3 +1,4 @@
require 'facter'
require File.expand_path('../../../util/openstackconfig', __FILE__)
@ -33,8 +34,15 @@ Puppet::Type.type(:openstack_config).provide(:ruby) do
end
def exists?
immutable_string = Facter.value(:os_immutable) || '<_IMMUTABLE_>'
if resource[:value] == ensure_absent_val
resource[:ensure] = :absent
elsif resource[:value] == immutable_string or resource[:value] == [immutable_string]
resource[:value] = value
# when the value is undefined, we keep it that way.
if value.nil? or (value.kind_of?(Array) and value[0].nil?)
resource[:ensure] = :absent
end
end
!config.get_value(section, setting).nil?
end

View File

@ -0,0 +1,8 @@
---
features:
- |
This add `os_immutable` fact that can be used in the puppet
openstack module as value for all types that inherit
openstack_config. Using ::os_immutable as a value of the
configuration parameter will tell puppet to not change the current
value, whatever it is.

View File

@ -37,6 +37,14 @@ describe provider_class do
}
end
let(:immutable_properties) do
{
:name => 'DEFAULT/foo',
:value => '<_IMMUTABLE_>',
:ensure => :present,
}
end
let(:type) do
Puppet::Type.newtype(:test_config) do
newparam(:name, :namevar => true)
@ -56,6 +64,15 @@ describe provider_class do
end
end
let(:immutable_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
@ -66,6 +83,11 @@ describe provider_class do
resource
end
let(:immutable_resource) do
resource = immutable_type.new(immutable_properties)
resource
end
context '#exists?' do
it 'ensure to present' do
child_conf = Class.new(provider_class) do
@ -108,6 +130,42 @@ describe provider_class do
expect(transform_resource[:value]).to eq 'BAR'
end
context 'immutable' do
# could not set fact using the classic let(:facts) idiom.
it 'ensure to no change when value set' do
child_conf = Class.new(provider_class) do
def self.file_path
'/some/file/path'
end
# current value
def value
'foo'
end
end
provider = child_conf.new(immutable_resource)
provider.exists?
expect(immutable_resource[:value]).to eq 'foo'
expect(immutable_resource[:ensure]).to eq :present
end
it 'ensure to no change when value unset' do
child_conf = Class.new(provider_class) do
def self.file_path
'/some/file/path'
end
# current value
def value
[nil]
end
end
provider = child_conf.new(immutable_resource)
provider.exists?
expect(immutable_resource[:value]).to eq nil
expect(immutable_resource[:ensure]).to eq :absent
end
end
end
end