Skip to content

Commit

Permalink
(MODULES-9428) Improve handling of composite namevars in SimpleProvider
Browse files Browse the repository at this point in the history
Before this change, edgecases where `is` and `should` were not
provided would lead to failures due to `name:` being filled wrong.
  • Loading branch information
DavidS committed Jun 28, 2019
1 parent 7ab1d9e commit 781083f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
18 changes: 15 additions & 3 deletions lib/puppet/resource_api/simple_provider.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Puppet; end # rubocop:disable Style/Documentation

module Puppet::ResourceApi
# This class provides a default implementation for set(), when your resource does not benefit from batching.
# Instead of processing changes yourself, the `create`, `update`, and `delete` functions, are called for you,
Expand All @@ -19,12 +20,12 @@ def set(context, changes)

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

is = { name: name, ensure: 'absent' } if is.nil?
should = { name: name, ensure: 'absent' } if should.nil?
is = SimpleProvider.create_absent(:name, name) if is.nil?
should = SimpleProvider.create_absent(:name, name) if should.nil?

name_hash = if context.type.namevars.length > 1
# pass a name_hash containing the values of all namevars
name_hash = { title: name }
name_hash = {}
context.type.namevars.each do |namevar|
name_hash[namevar] = change[:should][namevar]
end
Expand Down Expand Up @@ -60,5 +61,16 @@ def update(_context, _name, _should)
def delete(_context, _name)
raise "#{self.class} has not implemented `delete`"
end

# @api private
def self.create_absent(namevar, title)
result = if title.is_a? Hash
title.dup
else
{ namevar => title }
end
result[:ensure] = 'absent'
result
end
end
end
29 changes: 11 additions & 18 deletions spec/puppet/resource_api/simple_provider_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,33 +214,26 @@ def delete(context, _name); end
it { expect { provider.set(context, changes) }.to raise_error %r{SimpleProvider cannot be used with a Type that is not ensurable} }
end

context 'with a type with multiple namevars' do
let(:should_values) { { name: 'title', parent: 'foo', wibble: 'wub', ensure: 'present' } }
let(:title) { 'foo#wub' }
context 'with changes from a composite namevar type' do
let(:changes) do
{ title =>
{
should: should_values,
} }
end
let(:name_hash) do
{
title: title,
parent: 'foo',
wibble: 'wub',
{ name1: 'value1', name2: 'value2' } =>
{
should: { name1: 'value1', name2: 'value2', ensure: 'present' },
},
}
end

before(:each) do
allow(context).to receive(:creating).with(title).and_yield
allow(context).to receive(:type).and_return(type_def)
allow(type_def).to receive(:feature?).with('simple_get_filter')
allow(context).to receive(:creating).with(name1: 'value1', name2: 'value2').and_yield
allow(type_def).to receive(:feature?).with('simple_get_filter').and_return(true)
allow(type_def).to receive(:namevars).and_return([:name1, :name2])
allow(type_def).to receive(:check_schema)
allow(type_def).to receive(:namevars).and_return([:parent, :wibble])
end

it 'calls create once' do
expect(provider).to receive(:create).with(context, name_hash, should_values).once
it 'calls the crud methods with the right title' do
expect(provider).to receive(:create).with(context, { name1: 'value1', name2: 'value2' }, hash_including(name1: 'value1'))

provider.set(context, changes)
end
end
Expand Down

0 comments on commit 781083f

Please sign in to comment.