Skip to content

Commit 89a1612

Browse files
committed
Add caching to rsapi_current_state accessors
Adding the cache layer allows the provider.get() results to be re-used across multiple Puppet::Resource instantiations for a single actual provider resource within a given Puppet run.
1 parent 90accb4 commit 89a1612

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

lib/puppet/resource_api.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'puppet/resource_api/glue'
66
require 'puppet/resource_api/parameter'
77
require 'puppet/resource_api/property'
8+
require 'puppet/resource_api/provider_get_cache'
89
require 'puppet/resource_api/puppet_context' unless RUBY_PLATFORM == 'java'
910
require 'puppet/resource_api/read_only_parameter'
1011
require 'puppet/resource_api/transport'
@@ -69,6 +70,15 @@ def type_definition
6970
apply_to_device
7071
end
7172

73+
define_singleton_method(:rsapi_provider_get_cache) do
74+
# This gives a new cache per resource provider on each Puppet run:
75+
@rsapi_provider_get_cache ||= Puppet::ResourceApi::ProviderGetCache.new
76+
end
77+
78+
def rsapi_provider_get_cache
79+
self.class.rsapi_provider_get_cache
80+
end
81+
7282
def initialize(attributes)
7383
# $stderr.puts "A: #{attributes.inspect}"
7484
if attributes.is_a? Puppet::Resource
@@ -154,10 +164,16 @@ def generate
154164
end
155165

156166
def rsapi_current_state
157-
@rsapi_current_state
167+
return @rsapi_current_state if @rsapi_current_state
168+
# If the current state is not set, then check the cache and, if a value is
169+
# found, ensure it passes strict_check before allowing it to be used:
170+
cached_value = rsapi_provider_get_cache.get(rsapi_title)
171+
strict_check(cached_value) if cached_value
172+
@rsapi_current_state = cached_value
158173
end
159174

160175
def rsapi_current_state=(value)
176+
rsapi_provider_get_cache.add(rsapi_title, value)
161177
@rsapi_current_state = value
162178
end
163179

@@ -299,7 +315,7 @@ def refresh_current_state
299315
# does not help with that:
300316
def cache_current_state(resource_hash)
301317
self.rsapi_current_state = resource_hash
302-
strict_check(rsapi_current_state)
318+
strict_check(resource_hash)
303319
end
304320

305321
def retrieve

spec/acceptance/composite_namevar_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
stdout_str, status = Open3.capture2e("puppet resource #{common_args} composite_namevar php/gem")
3434
expect(stdout_str.strip).to match %r{^composite_namevar \{ \'php/gem\'}
3535
expect(stdout_str.strip).to match %r{ensure\s*=> \'present\'}
36-
expect(stdout_str.strip).to match %r{Looking for \[\{:package=>"php", :manager=>"gem"\}\]}
36+
# "Looking for" will return nil as puppet resource will have already fetched
37+
# the resource in instances():
38+
expect(stdout_str.strip).to match %r{Looking for nil}
3739
expect(status.exitstatus).to eq 0
3840
end
3941
it 'properly identifies an absent resource if only the title is provided' do

spec/acceptance/get_calls_spec.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@
2424
expect(stdout_str).not_to match %r{Creating}
2525
end
2626

27-
it 'calls get 3 times with resource purging' do
27+
it 'calls get 1 time with resource purging' do
2828
stdout_str, _status = Open3.capture2e("puppet apply #{common_args} -e \"test_get_calls_basic { foo: } test_get_calls_basic { bar: } resources { test_get_calls_basic: purge => true }\"")
2929
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 1 times}
30-
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 2 times}
31-
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 3 times}
32-
expect(stdout_str).not_to match %r{Notice: test_get_calls_basic: Provider get called 4 times}
30+
expect(stdout_str).not_to match %r{Notice: test_get_calls_basic: Provider get called 2 times}
3331
expect(stdout_str).not_to match %r{Creating}
3432
end
3533
end
@@ -52,12 +50,10 @@
5250
expect(stdout_str).not_to match %r{Creating}
5351
end
5452

55-
it 'calls get 3 times when resource purging' do
53+
it 'calls get 1 time when resource purging' do
5654
stdout_str, _status = Open3.capture2e("puppet apply #{common_args} -e \"test_get_calls_sgf { foo: } test_get_calls_sgf { bar: } resources { test_get_calls_sgf: purge => true }\"")
5755
expect(stdout_str).to match %r{Notice: test_get_calls_sgf: Provider get called 1 times}
58-
expect(stdout_str).to match %r{Notice: test_get_calls_sgf: Provider get called 2 times}
59-
expect(stdout_str).to match %r{Notice: test_get_calls_sgf: Provider get called 3 times}
60-
expect(stdout_str).not_to match %r{Notice: test_get_calls_sgf: Provider get called 4 times}
56+
expect(stdout_str).not_to match %r{Notice: test_get_calls_sgf: Provider get called 2 times}
6157
expect(stdout_str).not_to match %r{Creating}
6258
end
6359
end

spec/puppet/resource_api_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,10 @@ def set(_context, _changes); end
865865
context 'when a manifest wants to change the value of an init_only attribute' do
866866
let(:instance) { Puppet::Type.type(:init_behaviour).new(name: 'init', ensure: 'present', something_init_only: 'lies', mutable: 'overdraft') }
867867

868+
before(:each) do
869+
instance.rsapi_provider_get_cache.clear
870+
end
871+
868872
context 'when Puppet strict setting is :error' do
869873
let(:strict_level) { :error }
870874

@@ -1630,6 +1634,8 @@ def set(_context, changes)
16301634
subject(:type) { Puppet::Type.type(:canonicalizer) }
16311635

16321636
before(:each) do
1637+
type.rsapi_provider_get_cache.clear
1638+
16331639
allow(type.my_provider).to receive(:get)
16341640
.with(kind_of(Puppet::ResourceApi::BaseContext))
16351641
.and_return([{ name: 'somename', test_string: 'canonfoo' },
@@ -1818,6 +1824,7 @@ def set(_context, changes)
18181824
.with(kind_of(Puppet::ResourceApi::BaseContext))
18191825
.and_return([{ name: 'somename', test_string: 'foo' },
18201826
{ name: 'other', test_string: 'bar' }])
1827+
type.rsapi_provider_get_cache.clear
18211828
end
18221829

18231830
it { is_expected.not_to be_nil }

0 commit comments

Comments
 (0)