vs_config: Fix broken idempotency with nil/empty value

If the value is nil or empty then the option should NOT exist. This
change fixes the logic to determine whether the option already exists
which is causing broken idempotency.

Closes-Bug: #1987419
Change-Id: I56f85ca35f8e9ee4e5e8d07c714b65f655b1e9ae
This commit is contained in:
Takashi Kajinami
2022-08-24 01:14:17 +09:00
parent b66ab255fb
commit 10202ad22f
2 changed files with 36 additions and 3 deletions

View File

@@ -85,6 +85,8 @@ Puppet::Type.type(:vs_config).provide(:ovs) do
# if skip_if_version matches ovs_version(), then skip the configuration by faking exists # if skip_if_version matches ovs_version(), then skip the configuration by faking exists
if @resource[:skip_if_version].eql? ovs_version() if @resource[:skip_if_version].eql? ovs_version()
return true return true
elsif ensure_absent?
@property_hash[:ensure] != :present
else else
@property_hash[:ensure] == :present @property_hash[:ensure] == :present
end end
@@ -108,7 +110,7 @@ Puppet::Type.type(:vs_config).provide(:ovs) do
end end
def create def create
if @resource[:value].nil? or @resource[:value].empty? if ensure_absent?
destroy destroy
else else
_set _set
@@ -119,6 +121,8 @@ Puppet::Type.type(:vs_config).provide(:ovs) do
# if skip_if_version matches ovs_version(), then skip the configuration by returning the same value # if skip_if_version matches ovs_version(), then skip the configuration by returning the same value
if @resource[:skip_if_version].eql? ovs_version() if @resource[:skip_if_version].eql? ovs_version()
@resource[:value] @resource[:value]
elsif ensure_absent?
@resource[:value]
else else
@property_hash[:value] @property_hash[:value]
end end
@@ -129,10 +133,16 @@ Puppet::Type.type(:vs_config).provide(:ovs) do
end end
def value=(value) def value=(value)
if @resource[:value].nil? or @resource[:value].empty? if ensure_absent?
destroy destroy
else else
_set _set
end end
end end
private
def ensure_absent?
(@resource[:value].nil? or @resource[:value].empty?) and @resource[:ensure] == :present
end
end end

View File

@@ -44,7 +44,24 @@ describe 'basic vswitch' do
vs_config { 'external_ids:ovn-remote': vs_config { 'external_ids:ovn-remote':
ensure => present, ensure => present,
value => 'tcp:127.0.0.1:2300', value => 'tcp:127.0.0.1:2300',
}
vs_config { 'other_config:thisshouldexist':
ensure => present,
value => 'customvalue',
}
vs_config { 'other_config:thisshouldnotexist':
ensure => present,
value => undef,
}
vs_config { 'other_config:thisshouldnotexist2':
ensure => present,
value => '',
}
vs_config { 'other_config:thisshouldnotexist3':
ensure => present,
value => [],
} }
EOS EOS
@@ -107,5 +124,11 @@ describe 'basic vswitch' do
expect(r.stdout).to match(/\"tcp:127.0.0.1:2300\"/) expect(r.stdout).to match(/\"tcp:127.0.0.1:2300\"/)
end end
end end
it 'should get other config' do
command('sudo ovs-vsctl get Open_Vswitch . other_config') do |r|
expect(r.stdout).to match(/\"{thishshouldexist=customvalue}"/)
end
end
end end
end end