Skip to content

Commit cecd90b

Browse files
authored
Merge pull request #125 from da-ar/simpleprovider_composite
(PDK-1143) Allow SimpleProvider to handle multiple namevars
2 parents 1b08a12 + e66fbda commit cecd90b

File tree

4 files changed

+70
-21
lines changed

4 files changed

+70
-21
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ group :tests do
2424
# the test gems required for module testing
2525
gem 'puppetlabs_spec_helper', '~> 2.7'
2626
gem 'rspec-puppet'
27+
28+
gem 'CFPropertyList'
2729
end
2830

2931
group :development do

lib/puppet/resource_api/simple_provider.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,28 @@ def set(context, changes)
2222
is = { name: name, ensure: 'absent' } if is.nil?
2323
should = { name: name, ensure: 'absent' } if should.nil?
2424

25+
name_hash = if context.type.namevars.length > 1
26+
# pass a name_hash containing the values of all namevars
27+
name_hash = { title: name }
28+
context.type.namevars.each do |namevar|
29+
name_hash[namevar] = change[:should][namevar]
30+
end
31+
name_hash
32+
else
33+
name
34+
end
35+
2536
if is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'present'
2637
context.creating(name) do
27-
create(context, name, should)
38+
create(context, name_hash, should)
2839
end
2940
elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'present'
3041
context.updating(name) do
31-
update(context, name, should)
42+
update(context, name_hash, should)
3243
end
3344
elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'absent'
3445
context.deleting(name) do
35-
delete(context, name)
46+
delete(context, name_hash)
3647
end
3748
end
3849
end
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
require 'puppet/resource_api'
2+
require 'puppet/resource_api/simple_provider'
23

34
# Implementation for the title_provider type using the Resource API.
4-
class Puppet::Provider::CompositeNamevar::CompositeNamevar
5+
class Puppet::Provider::CompositeNamevar::CompositeNamevar < Puppet::ResourceApi::SimpleProvider
56
def initialize
67
@current_values ||= [
78
{ title: 'php-yum', package: 'php', manager: 'yum', ensure: 'present', value: 'a' },
@@ -13,25 +14,25 @@ def initialize
1314
]
1415
end
1516

16-
def set(context, changes)
17-
changes.each do |name, change|
18-
next unless change[:is] != change[:should]
17+
def get(_context)
18+
@current_values
19+
end
1920

20-
match = @current_values.find do |item|
21-
context.type.namevars.all? do |namevar|
22-
item[namevar] == change[:should][namevar]
23-
end
24-
end
25-
if match
26-
match[:ensure] = change[:should][:ensure]
27-
else
28-
context.created([name], message: 'Adding new record')
29-
@current_values << change[:should].dup
30-
end
31-
end
21+
def create(context, name, should)
22+
context.notice("Creating '#{name[:title]}' with #{should.inspect}")
23+
context.notice("namevar :package value `#{name[:package]}`")
24+
context.notice("namevar :manager value `#{name[:manager]}`")
3225
end
3326

34-
def get(_context)
35-
@current_values
27+
def update(context, name, should)
28+
context.notice("Updating '#{name[:title]}' with #{should.inspect}")
29+
context.notice("namevar :package value `#{name[:package]}`")
30+
context.notice("namevar :manager value `#{name[:manager]}`")
31+
end
32+
33+
def delete(context, name)
34+
context.notice("Deleting '#{name[:title]}'")
35+
context.notice("namevar :package value `#{name[:package]}`")
36+
context.notice("namevar :manager value `#{name[:manager]}`")
3637
end
3738
end

spec/puppet/resource_api/simple_provider_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def delete(context, _name); end
6666
allow(context).to receive(:type).and_return(type_def)
6767
allow(type_def).to receive(:feature?).with('simple_get_filter')
6868
allow(type_def).to receive(:check_schema)
69+
allow(type_def).to receive(:namevars).and_return([:name])
6970
end
7071

7172
it 'calls create once' do
@@ -109,6 +110,7 @@ def delete(context, _name); end
109110
allow(context).to receive(:updating).with('title').and_yield
110111
allow(context).to receive(:type).and_return(type_def)
111112
allow(type_def).to receive(:feature?).with('simple_get_filter')
113+
allow(type_def).to receive(:namevars).and_return([:name])
112114
end
113115

114116
it 'does not call create' do
@@ -140,6 +142,7 @@ def delete(context, _name); end
140142
allow(context).to receive(:deleting).with('title').and_yield
141143
allow(context).to receive(:type).and_return(type_def)
142144
allow(type_def).to receive(:feature?).with('simple_get_filter')
145+
allow(type_def).to receive(:namevars).and_return([:name])
143146
end
144147

145148
it 'does not call create' do
@@ -179,6 +182,7 @@ def delete(context, _name); end
179182
allow(context).to receive(:updating).with('to update').and_yield
180183
allow(context).to receive(:deleting).with('to delete').and_yield
181184
allow(type_def).to receive(:feature?).with('simple_get_filter').exactly(3).times
185+
allow(type_def).to receive(:namevars).and_return([:name])
182186
end
183187

184188
it 'calls the crud methods' do
@@ -209,4 +213,35 @@ def delete(context, _name); end
209213

210214
it { expect { provider.set(context, changes) }.to raise_error %r{SimpleProvider cannot be used with a Type that is not ensurable} }
211215
end
216+
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' }
220+
let(:changes) do
221+
{ title =>
222+
{
223+
should: should_values,
224+
} }
225+
end
226+
let(:name_hash) do
227+
{
228+
title: title,
229+
parent: 'foo',
230+
wibble: 'wub',
231+
}
232+
end
233+
234+
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')
238+
allow(type_def).to receive(:check_schema)
239+
allow(type_def).to receive(:namevars).and_return([:parent, :wibble])
240+
end
241+
242+
it 'calls create once' do
243+
expect(provider).to receive(:create).with(context, name_hash, should_values).once
244+
provider.set(context, changes)
245+
end
246+
end
212247
end

0 commit comments

Comments
 (0)