Skip to content

Commit be96084

Browse files
committed
Ensure all provider get() data is cached
Ensure that remaining data fetched with the provider get() method is cached.
1 parent 0f12dba commit be96084

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

lib/puppet/resource_api.rb

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -261,19 +261,36 @@ def to_resource_shim(resource)
261261
type_definition.create_attribute_in(self, name, param_or_property, parent, options)
262262
end
263263

264+
def self.rsapi_provider_get(names = nil)
265+
# If the cache has been marked as having all instances, then just return the
266+
# full contents:
267+
return rsapi_provider_get_cache.all if rsapi_provider_get_cache.cached_all? && names.nil?
268+
269+
fetched = if type_definition.feature?('simple_get_filter')
270+
my_provider.get(context, names)
271+
else
272+
my_provider.get(context)
273+
end
274+
275+
fetched.each do |resource_hash|
276+
type_definition.check_schema(resource_hash)
277+
rsapi_provider_get_cache.add(build_title(type_definition, resource_hash), resource_hash)
278+
end
279+
280+
if names.nil? && !type_definition.feature?('simple_get_filter')
281+
# Mark the cache as having all possible instances:
282+
rsapi_provider_get_cache.cached_all
283+
end
284+
285+
fetched
286+
end
287+
264288
def self.instances
265289
# puts 'instances'
266290
# force autoloading of the provider
267291
provider(type_definition.name)
268292

269-
initial_fetch = if type_definition.feature?('simple_get_filter')
270-
my_provider.get(context, nil)
271-
else
272-
my_provider.get(context)
273-
end
274-
275-
initial_fetch.map do |resource_hash|
276-
type_definition.check_schema(resource_hash)
293+
rsapi_provider_get.map do |resource_hash|
277294
# allow a :title from the provider to override the default
278295
result = if resource_hash.key? :title
279296
new(title: resource_hash[:title])
@@ -288,14 +305,9 @@ def self.instances
288305
end
289306

290307
def refresh_current_state
291-
current_state = if type_definition.feature?('simple_get_filter')
292-
my_provider.get(context, [rsapi_title]).find { |h| namevar_match?(h) }
293-
else
294-
my_provider.get(context).find { |h| namevar_match?(h) }
295-
end
308+
current_state = self.class.rsapi_provider_get([rsapi_title]).find { |h| namevar_match?(h) }
296309

297310
if current_state
298-
type_definition.check_schema(current_state)
299311
strict_check(current_state)
300312
else
301313
current_state = if rsapi_title.is_a? Hash

spec/acceptance/get_calls_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
it 'calls get 2 times' do
2020
stdout_str, _status = Open3.capture2e("puppet apply #{common_args} -e \"test_get_calls_basic { foo: }\ntest_get_calls_basic { bar: }\"")
2121
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 1 times}
22-
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 2 times}
23-
expect(stdout_str).not_to match %r{Notice: test_get_calls_basic: Provider get called 3 times}
22+
expect(stdout_str).not_to match %r{Notice: test_get_calls_basic: Provider get called 2 times}
2423
expect(stdout_str).not_to match %r{Creating}
2524
end
2625

spec/puppet/resource_api_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,10 @@ def set(_context, changes) end
21972197
it { expect { described_class.register_type(definition) }.not_to raise_error }
21982198

21992199
context 'with the type registered' do
2200+
before(:each) do
2201+
type.rsapi_provider_get_cache.clear
2202+
end
2203+
22002204
it 'is seen as a supported feature' do
22012205
expect(Puppet).not_to receive(:warning).with(%r{Unknown feature detected:.*simple_test_filter_2})
22022206
expect { described_class.register_type(definition) }.not_to raise_error

0 commit comments

Comments
 (0)