Skip to content

Commit efee0a1

Browse files
seanmilLukasAud
authored andcommitted
Fix SimpleProvider composite namevar matching
1 parent 1fd946c commit efee0a1

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/puppet/resource_api/simple_provider.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ module Puppet::ResourceApi
1010
# and absence of resources.
1111
class SimpleProvider
1212
def set(context, changes)
13+
namevars = context.type.namevars
14+
1315
changes.each do |name, change|
1416
is = if context.type.feature?('simple_get_filter')
15-
change.key?(:is) ? change[:is] : (get(context, [name]) || []).find { |r| r[:name] == name }
17+
change.key?(:is) ? change[:is] : (get(context, [name]) || []).find { |r| SimpleProvider.build_name(namevars, r) == name }
1618
else
17-
change.key?(:is) ? change[:is] : (get(context) || []).find { |r| r[:name] == name }
19+
change.key?(:is) ? change[:is] : (get(context) || []).find { |r| SimpleProvider.build_name(namevars, r) == name }
1820
end
1921
context.type.check_schema(is) unless change.key?(:is)
2022

@@ -25,10 +27,10 @@ def set(context, changes)
2527
is = SimpleProvider.create_absent(:name, name) if is.nil?
2628
should = SimpleProvider.create_absent(:name, name) if should.nil?
2729

28-
name_hash = if context.type.namevars.length > 1
30+
name_hash = if namevars.length > 1
2931
# pass a name_hash containing the values of all namevars
3032
name_hash = {}
31-
context.type.namevars.each do |namevar|
33+
namevars.each do |namevar|
3234
name_hash[namevar] = change[:should][namevar]
3335
end
3436
name_hash
@@ -74,5 +76,14 @@ def self.create_absent(namevar, title)
7476
result[:ensure] = 'absent'
7577
result
7678
end
79+
80+
# @api private
81+
def self.build_name(namevars, resource_hash)
82+
if namevars.size > 1
83+
Hash[namevars.map { |attr| [attr, resource_hash[attr]] }]
84+
else
85+
resource_hash[namevars[0]]
86+
end
87+
end
7788
end
7889
end

spec/puppet/resource_api/simple_provider_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def delete(context, _name); end
4040
context 'with no changes' do
4141
let(:changes) { {} }
4242

43+
before(:each) do
44+
allow(type_def).to receive(:namevars)
45+
end
46+
4347
it 'does not call create' do
4448
expect(provider).to receive(:create).never
4549
provider.set(context, changes)
@@ -362,6 +366,7 @@ def delete(context, _name); end
362366
allow(context).to receive(:updating).with('title').and_yield
363367
allow(type_def).to receive(:feature?).with('simple_get_filter')
364368
allow(type_def).to receive(:ensurable?).and_return(false)
369+
allow(type_def).to receive(:namevars).and_return([:name])
365370
end
366371

367372
it { expect { provider.set(context, changes) }.to raise_error %r{SimpleProvider cannot be used with a Type that is not ensurable} }

0 commit comments

Comments
 (0)