Skip to content

Commit 781083f

Browse files
committed
(MODULES-9428) Improve handling of composite namevars in SimpleProvider
Before this change, edgecases where `is` and `should` were not provided would lead to failures due to `name:` being filled wrong.
1 parent 7ab1d9e commit 781083f

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

lib/puppet/resource_api/simple_provider.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module Puppet; end # rubocop:disable Style/Documentation
2+
23
module Puppet::ResourceApi
34
# This class provides a default implementation for set(), when your resource does not benefit from batching.
45
# Instead of processing changes yourself, the `create`, `update`, and `delete` functions, are called for you,
@@ -19,12 +20,12 @@ def set(context, changes)
1920

2021
raise 'SimpleProvider cannot be used with a Type that is not ensurable' unless context.type.ensurable?
2122

22-
is = { name: name, ensure: 'absent' } if is.nil?
23-
should = { name: name, ensure: 'absent' } if should.nil?
23+
is = SimpleProvider.create_absent(:name, name) if is.nil?
24+
should = SimpleProvider.create_absent(:name, name) if should.nil?
2425

2526
name_hash = if context.type.namevars.length > 1
2627
# pass a name_hash containing the values of all namevars
27-
name_hash = { title: name }
28+
name_hash = {}
2829
context.type.namevars.each do |namevar|
2930
name_hash[namevar] = change[:should][namevar]
3031
end
@@ -60,5 +61,16 @@ def update(_context, _name, _should)
6061
def delete(_context, _name)
6162
raise "#{self.class} has not implemented `delete`"
6263
end
64+
65+
# @api private
66+
def self.create_absent(namevar, title)
67+
result = if title.is_a? Hash
68+
title.dup
69+
else
70+
{ namevar => title }
71+
end
72+
result[:ensure] = 'absent'
73+
result
74+
end
6375
end
6476
end

spec/puppet/resource_api/simple_provider_spec.rb

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -214,33 +214,26 @@ def delete(context, _name); end
214214
it { expect { provider.set(context, changes) }.to raise_error %r{SimpleProvider cannot be used with a Type that is not ensurable} }
215215
end
216216

217-
context 'with a type with multiple namevars' do
218-
let(:should_values) { { name: 'title', parent: 'foo', wibble: 'wub', ensure: 'present' } }
219-
let(:title) { 'foo#wub' }
217+
context 'with changes from a composite namevar type' do
220218
let(:changes) do
221-
{ title =>
222-
{
223-
should: should_values,
224-
} }
225-
end
226-
let(:name_hash) do
227219
{
228-
title: title,
229-
parent: 'foo',
230-
wibble: 'wub',
220+
{ name1: 'value1', name2: 'value2' } =>
221+
{
222+
should: { name1: 'value1', name2: 'value2', ensure: 'present' },
223+
},
231224
}
232225
end
233226

234227
before(:each) do
235-
allow(context).to receive(:creating).with(title).and_yield
236-
allow(context).to receive(:type).and_return(type_def)
237-
allow(type_def).to receive(:feature?).with('simple_get_filter')
228+
allow(context).to receive(:creating).with(name1: 'value1', name2: 'value2').and_yield
229+
allow(type_def).to receive(:feature?).with('simple_get_filter').and_return(true)
230+
allow(type_def).to receive(:namevars).and_return([:name1, :name2])
238231
allow(type_def).to receive(:check_schema)
239-
allow(type_def).to receive(:namevars).and_return([:parent, :wibble])
240232
end
241233

242-
it 'calls create once' do
243-
expect(provider).to receive(:create).with(context, name_hash, should_values).once
234+
it 'calls the crud methods with the right title' do
235+
expect(provider).to receive(:create).with(context, { name1: 'value1', name2: 'value2' }, hash_including(name1: 'value1'))
236+
244237
provider.set(context, changes)
245238
end
246239
end

0 commit comments

Comments
 (0)