Skip to content

Commit ef00677

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

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
@@ -244,19 +244,36 @@ def to_resource_shim(resource)
244244
type_definition.create_attribute_in(self, name, param_or_property, parent, options)
245245
end
246246

247+
def self.rsapi_provider_get(names = [])
248+
# If the cache has been marked as having all instances, then just return the
249+
# full contents:
250+
return rsapi_provider_get_cache.all if rsapi_provider_get_cache.cached_all? && names.empty?
251+
252+
fetched = if type_definition.feature?('simple_get_filter')
253+
my_provider.get(context, names)
254+
else
255+
my_provider.get(context)
256+
end
257+
258+
fetched.each do |resource_hash|
259+
type_definition.check_schema(resource_hash)
260+
rsapi_provider_get_cache.add(build_title(type_definition, resource_hash), resource_hash)
261+
end
262+
263+
if !type_definition.feature?('simple_get_filter') || names.empty?
264+
# Mark the cache as having all possible instances:
265+
rsapi_provider_get_cache.cached_all
266+
end
267+
268+
fetched
269+
end
270+
247271
def self.instances
248272
# puts 'instances'
249273
# force autoloading of the provider
250274
provider(type_definition.name)
251275

252-
initial_fetch = if type_definition.feature?('simple_get_filter')
253-
my_provider.get(context, [])
254-
else
255-
my_provider.get(context)
256-
end
257-
258-
initial_fetch.map do |resource_hash|
259-
type_definition.check_schema(resource_hash)
276+
rsapi_provider_get.map do |resource_hash|
260277
# allow a :title from the provider to override the default
261278
result = if resource_hash.key? :title
262279
new(title: resource_hash[:title])
@@ -271,14 +288,9 @@ def self.instances
271288
end
272289

273290
def refresh_current_state
274-
current_state = if type_definition.feature?('simple_get_filter')
275-
my_provider.get(context, [rsapi_title]).find { |h| namevar_match?(h) }
276-
else
277-
my_provider.get(context).find { |h| namevar_match?(h) }
278-
end
291+
current_state = self.class.rsapi_provider_get([rsapi_title]).find { |h| namevar_match?(h) }
279292

280293
if current_state
281-
type_definition.check_schema(current_state)
282294
strict_check(current_state)
283295
else
284296
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
@@ -20,8 +20,7 @@
2020
# rubocop:disable Metrics/LineLength
2121
stdout_str, _status = Open3.capture2e("puppet apply #{common_args} -e \"test_get_calls_basic { foo: }\ntest_get_calls_basic { bar: }\"")
2222
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 1 times}
23-
expect(stdout_str).to match %r{Notice: test_get_calls_basic: Provider get called 2 times}
24-
expect(stdout_str).not_to match %r{Notice: test_get_calls_basic: Provider get called 3 times}
23+
expect(stdout_str).not_to match %r{Notice: test_get_calls_basic: Provider get called 2 times}
2524
expect(stdout_str).not_to match %r{Creating}
2625
# rubocop:enable Metrics/LineLength
2726
end

spec/puppet/resource_api_spec.rb

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

21442144
context 'with the type registered' do
2145+
before(:each) do
2146+
type.rsapi_provider_get_cache.clear
2147+
end
2148+
21452149
it 'is seen as a supported feature' do
21462150
expect(Puppet).not_to receive(:warning).with(%r{Unknown feature detected:.*simple_test_filter_2})
21472151
expect { described_class.register_type(definition) }.not_to raise_error

0 commit comments

Comments
 (0)