Skip to content

Commit 0cf84e5

Browse files
authored
Merge pull request #174 from DavidS/maint-fix-composite-simple_get_filter
(MODULES-9428) make the composite namevar implementation usable
2 parents 9e55ce3 + 781083f commit 0cf84e5

File tree

13 files changed

+270
-105
lines changed

13 files changed

+270
-105
lines changed

Rakefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,30 @@ end
1414
require 'rspec/core/rake_task'
1515

1616
RSpec::Core::RakeTask.new(:spec) do |t|
17-
Rake::Task[:spec_prep].invoke
1817
# thanks to the fixtures/modules/ symlinks this needs to exclude fixture modules explicitely
1918
excludes = ['fixtures/**/*.rb,fixtures/modules/*/**/*.rb']
2019
if RUBY_PLATFORM == 'java'
2120
excludes += ['acceptance/**/*.rb', 'integration/**/*.rb', 'puppet/resource_api/*_context_spec.rb', 'puppet/util/network_device/simple/device_spec.rb']
2221
t.rspec_opts = '--tag ~agent_test'
22+
t.rspec_opts << ' --tag ~j17_exclude' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.0.0')
2323
end
2424
t.exclude_pattern = "spec/{#{excludes.join ','}}"
2525
end
2626

27+
task :spec => :spec_prep
28+
2729
namespace :spec do
2830
desc 'Run RSpec code examples with coverage collection'
2931
task :coverage do
3032
ENV['SIMPLECOV'] = 'yes'
3133
Rake::Task['spec'].execute
3234
end
35+
36+
RSpec::Core::RakeTask.new(:unit) do |t|
37+
t.pattern = "spec/puppet/**/*_spec.rb,spec/integration/**/*_spec.rb"
38+
end
39+
40+
task :unit => :spec_prep
3341
end
3442

3543
#### LICENSE_FINDER ####

lib/puppet/resource_api.rb

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def type_definition
9191
apply_to_device
9292
end
9393

94-
define_method(:initialize) do |attributes|
94+
def initialize(attributes)
9595
# $stderr.puts "A: #{attributes.inspect}"
9696
if attributes.is_a? Puppet::Resource
9797
@title = attributes.title
@@ -119,7 +119,7 @@ def type_definition
119119
# the `Puppet::Resource::Ral.find` method, when `instances` does not return a match, uses a Hash with a `:name` key to create
120120
# an "absent" resource. This is often hit by `puppet resource`. This needs to work, even if the namevar is not called `name`.
121121
# This bit here relies on the default `title_patterns` (see below) to match the title back to the first (and often only) namevar
122-
if definition[:attributes][:name].nil? && attributes[:title].nil?
122+
if type_definition.attributes[:name].nil? && attributes[:title].nil?
123123
attributes[:title] = attributes.delete(:name)
124124
if attributes[:title].nil? && !type_definition.namevars.empty?
125125
attributes[:title] = @title
@@ -133,11 +133,25 @@ def name
133133
title
134134
end
135135

136+
def self.build_title(type_definition, resource_hash)
137+
if type_definition.namevars.size > 1
138+
# use a MonkeyHash to allow searching in Puppet's RAL
139+
Puppet::ResourceApi::MonkeyHash[type_definition.namevars.map { |attr| [attr, resource_hash[attr]] }]
140+
else
141+
resource_hash[type_definition.namevars[0]]
142+
end
143+
end
144+
145+
def rsapi_title
146+
@rsapi_title ||= self.class.build_title(type_definition, self)
147+
@rsapi_title
148+
end
149+
136150
def to_resource
137151
to_resource_shim(super)
138152
end
139153

140-
define_method(:to_resource_shim) do |resource|
154+
def to_resource_shim(resource)
141155
resource_hash = Hash[resource.keys.map { |k| [k, resource[k]] }]
142156
resource_hash[:title] = resource.title
143157
ResourceShim.new(resource_hash, type_definition.name, type_definition.namevars, type_definition.attributes, catalog)
@@ -244,7 +258,7 @@ def to_resource
244258
end
245259
end
246260

247-
define_singleton_method(:instances) do
261+
def self.instances
248262
# puts 'instances'
249263
# force autoloading of the provider
250264
provider(type_definition.name)
@@ -261,16 +275,16 @@ def to_resource
261275
result = if resource_hash.key? :title
262276
new(title: resource_hash[:title])
263277
else
264-
new(title: resource_hash[type_definition.namevars.first])
278+
new(title: build_title(type_definition, resource_hash))
265279
end
266280
result.cache_current_state(resource_hash)
267281
result
268282
end
269283
end
270284

271-
define_method(:refresh_current_state) do
285+
def refresh_current_state
272286
@rsapi_current_state = if type_definition.feature?('simple_get_filter')
273-
my_provider.get(context, [title]).find { |h| namevar_match?(h) }
287+
my_provider.get(context, [rsapi_title]).find { |h| namevar_match?(h) }
274288
else
275289
my_provider.get(context).find { |h| namevar_match?(h) }
276290
end
@@ -279,7 +293,11 @@ def to_resource
279293
type_definition.check_schema(@rsapi_current_state)
280294
strict_check(@rsapi_current_state) if type_definition.feature?('canonicalize')
281295
else
282-
@rsapi_current_state = { title: title }
296+
@rsapi_current_state = if rsapi_title.is_a? Hash
297+
rsapi_title.dup
298+
else
299+
{ title: rsapi_title }
300+
end
283301
@rsapi_current_state[:ensure] = :absent if type_definition.ensurable?
284302
end
285303
end
@@ -290,7 +308,7 @@ def cache_current_state(resource_hash)
290308
strict_check(@rsapi_current_state) if type_definition.feature?('canonicalize')
291309
end
292310

293-
define_method(:retrieve) do
311+
def retrieve
294312
refresh_current_state unless @rsapi_current_state
295313

296314
Puppet.debug("Current State: #{@rsapi_current_state.inspect}")
@@ -304,13 +322,13 @@ def cache_current_state(resource_hash)
304322
result
305323
end
306324

307-
define_method(:namevar_match?) do |item|
325+
def namevar_match?(item)
308326
context.type.namevars.all? do |namevar|
309327
item[namevar] == @parameters[namevar].value if @parameters[namevar].respond_to? :value
310328
end
311329
end
312330

313-
define_method(:flush) do
331+
def flush
314332
raise_missing_attrs
315333

316334
# puts 'flush'
@@ -328,7 +346,7 @@ def cache_current_state(resource_hash)
328346
# enforce init_only attributes
329347
if Puppet.settings[:strict] != :off && @rsapi_current_state && (@rsapi_current_state[:ensure] == 'present' && target_state[:ensure] == 'present')
330348
target_state.each do |name, value|
331-
next unless definition[:attributes][name][:behaviour] == :init_only && value != @rsapi_current_state[name]
349+
next unless type_definition.attributes[name][:behaviour] == :init_only && value != @rsapi_current_state[name]
332350
message = "Attempting to change `#{name}` init_only attribute value from `#{@rsapi_current_state[name]}` to `#{value}`"
333351
case Puppet.settings[:strict]
334352
when :warning
@@ -340,27 +358,27 @@ def cache_current_state(resource_hash)
340358
end
341359

342360
if type_definition.feature?('supports_noop')
343-
my_provider.set(context, { title => { is: @rsapi_current_state, should: target_state } }, noop: noop?)
361+
my_provider.set(context, { rsapi_title => { is: @rsapi_current_state, should: target_state } }, noop: noop?)
344362
else
345-
my_provider.set(context, title => { is: @rsapi_current_state, should: target_state }) unless noop?
363+
my_provider.set(context, rsapi_title => { is: @rsapi_current_state, should: target_state }) unless noop?
346364
end
347365
raise 'Execution encountered an error' if context.failed?
348366

349367
# remember that we have successfully reached our desired state
350368
@rsapi_current_state = target_state
351369
end
352370

353-
define_method(:raise_missing_attrs) do
371+
def raise_missing_attrs
354372
error_msg = "The following mandatory attributes were not provided:\n * " + @missing_attrs.join(", \n * ")
355373
raise Puppet::ResourceError, error_msg if @missing_attrs.any? && (value(:ensure) != :absent && !value(:ensure).nil?)
356374
end
357375

358-
define_method(:raise_missing_params) do
376+
def raise_missing_params
359377
error_msg = "The following mandatory parameters were not provided:\n * " + @missing_params.join(", \n * ")
360378
raise Puppet::ResourceError, error_msg
361379
end
362380

363-
define_method(:strict_check) do |current_state|
381+
def strict_check(current_state)
364382
return if Puppet.settings[:strict] == :off
365383

366384
# if strict checking is on we must notify if the values are changed by canonicalize
@@ -374,7 +392,7 @@ def cache_current_state(resource_hash)
374392
#:nocov:
375393
# codecov fails to register this multiline as covered, even though simplecov does.
376394
message = <<MESSAGE.strip
377-
#{definition[:name]}[#{@title}]#get has not provided canonicalized values.
395+
#{type_definition.name}[#{@title}]#get has not provided canonicalized values.
378396
Returned values: #{current_state.inspect}
379397
Canonicalized values: #{state_clone.inspect}
380398
MESSAGE
@@ -387,7 +405,7 @@ def cache_current_state(resource_hash)
387405
raise Puppet::DevError, message
388406
end
389407

390-
return nil
408+
nil
391409
end
392410

393411
define_singleton_method(:context) do
@@ -398,9 +416,9 @@ def context
398416
self.class.context
399417
end
400418

401-
define_singleton_method(:title_patterns) do
402-
@title_patterns ||= if definition.key? :title_patterns
403-
parse_title_patterns(definition[:title_patterns])
419+
def self.title_patterns
420+
@title_patterns ||= if type_definition.definition.key? :title_patterns
421+
parse_title_patterns(type_definition.definition[:title_patterns])
404422
else
405423
[[%r{(.*)}m, [[type_definition.namevars.first]]]]
406424
end

lib/puppet/resource_api/glue.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,19 @@ def filtered_keys
5757
values.keys.reject { |k| k == :title || !attr_def[k] || (attr_def[k][:behaviour] == :namevar && @namevars.size == 1) }
5858
end
5959
end
60+
61+
# this hash allows key-value based ordering comparisons between instances of this and instances of this and other classes
62+
# this is required for `lib/puppet/indirector/resource/ral.rb`'s `search` method which expects all titles to be comparable
63+
class MonkeyHash < Hash
64+
def <=>(other)
65+
result = self.class.name <=> other.class.name
66+
if result.zero?
67+
result = keys.sort <=> other.keys.sort
68+
end
69+
if result.zero?
70+
result = keys.sort.map { |k| self[k] } <=> other.keys.sort.map { |k| other[k] }
71+
end
72+
result
73+
end
74+
end
6075
end

lib/puppet/resource_api/simple_provider.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module Puppet; end # rubocop:disable Style/Documentation
2+
23
module Puppet::ResourceApi
34
# This class provides a default implementation for set(), when your resource does not benefit from batching.
45
# Instead of processing changes yourself, the `create`, `update`, and `delete` functions, are called for you,
@@ -19,12 +20,12 @@ def set(context, changes)
1920

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

22-
is = { name: name, ensure: 'absent' } if is.nil?
23-
should = { name: name, ensure: 'absent' } if should.nil?
23+
is = SimpleProvider.create_absent(:name, name) if is.nil?
24+
should = SimpleProvider.create_absent(:name, name) if should.nil?
2425

2526
name_hash = if context.type.namevars.length > 1
2627
# pass a name_hash containing the values of all namevars
27-
name_hash = { title: name }
28+
name_hash = {}
2829
context.type.namevars.each do |namevar|
2930
name_hash[namevar] = change[:should][namevar]
3031
end
@@ -60,5 +61,16 @@ def update(_context, _name, _should)
6061
def delete(_context, _name)
6162
raise "#{self.class} has not implemented `delete`"
6263
end
64+
65+
# @api private
66+
def self.create_absent(namevar, title)
67+
result = if title.is_a? Hash
68+
title.dup
69+
else
70+
{ namevar => title }
71+
end
72+
result[:ensure] = 'absent'
73+
result
74+
end
6375
end
6476
end

0 commit comments

Comments
 (0)