Skip to content

Commit abfc23e

Browse files
author
jordanbreen28
committed
(bug) - Fixes missing mandatory ID
1 parent 76bacc7 commit abfc23e

File tree

2 files changed

+66
-69
lines changed

2 files changed

+66
-69
lines changed

lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,25 @@
66
require 'json'
77

88
class Puppet::Provider::DscBaseProvider
9-
# Initializes the provider, preparing the class variables which cache:
9+
# Initializes the provider, preparing the instance variables which cache:
1010
# - the canonicalized resources across calls
1111
# - query results
1212
# - logon failures
1313
def initialize
14-
@@cached_canonicalized_resource ||= []
15-
@@cached_query_results ||= []
16-
@@cached_test_results ||= []
17-
@@logon_failures ||= []
14+
@cached_canonicalized_resource = []
15+
@cached_query_results = []
16+
@cached_test_results = []
17+
@logon_failures = []
1818
super
1919
end
2020

21-
def cached_test_results
22-
@@cached_test_results
23-
end
21+
attr_reader :cached_test_results
2422

2523
# Look through a cache to retrieve the hashes specified, if they have been cached.
2624
# Does so by seeing if each of the specified hashes is a subset of any of the hashes
2725
# in the cache, so {foo: 1, bar: 2} would return if {foo: 1} was the search hash.
2826
#
29-
# @param cache [Array] the class variable containing cached hashes to search through
27+
# @param cache [Array] the instance variable containing cached hashes to search through
3028
# @param hashes [Array] the list of hashes to search the cache for
3129
# @return [Array] an array containing the matching hashes for the search condition, if any
3230
def fetch_cached_hashes(cache, hashes)
@@ -52,22 +50,22 @@ def canonicalize(context, resources)
5250
# During RSAPI refresh runs mandatory parameters are stripped and not available;
5351
# Instead of checking again and failing, search the cache for a namevar match.
5452
namevarized_r = r.select { |k, _v| namevar_attributes(context).include?(k) }
55-
cached_result = fetch_cached_hashes(@@cached_canonicalized_resource, [namevarized_r]).first
53+
cached_result = fetch_cached_hashes(@cached_canonicalized_resource, [namevarized_r]).first
5654
if cached_result.nil?
5755
# If the resource is meant to be absent, skip canonicalization and rely on the manifest
5856
# value; there's no reason to compare system state to desired state for casing if the
5957
# resource is being removed.
6058
if r[:dsc_ensure] == 'absent'
6159
canonicalized = r.dup
62-
@@cached_canonicalized_resource << r.dup
60+
@cached_canonicalized_resource << r.dup
6361
else
6462
canonicalized = invoke_get_method(context, r)
6563
# If the resource could not be found or was returned as absent, skip case munging and
6664
# treat the manifest values as canonical since the resource is being created.
6765
# rubocop:disable Metrics/BlockNesting
6866
if canonicalized.nil? || canonicalized[:dsc_ensure] == 'absent'
6967
canonicalized = r.dup
70-
@@cached_canonicalized_resource << r.dup
68+
@cached_canonicalized_resource << r.dup
7169
else
7270
parameters = r.select { |name, _properties| parameter_attributes(context).include?(name) }
7371
canonicalized.merge!(parameters)
@@ -91,7 +89,7 @@ def canonicalize(context, resources)
9189
canonicalized.delete(key) unless downcased_resource.key?(key)
9290
end
9391
# Cache the actually canonicalized resource separately
94-
@@cached_canonicalized_resource << canonicalized.dup
92+
@cached_canonicalized_resource << canonicalized.dup
9593
end
9694
# rubocop:enable Metrics/BlockNesting
9795
end
@@ -123,13 +121,13 @@ def get(context, names = nil)
123121
context.debug('Collecting data from the DSC Resource')
124122

125123
# If the resource has already been queried, do not bother querying for it again
126-
cached_results = fetch_cached_hashes(@@cached_query_results, names)
124+
cached_results = fetch_cached_hashes(@cached_query_results, names)
127125
return cached_results unless cached_results.empty?
128126

129-
if @@cached_canonicalized_resource.empty?
127+
if @cached_canonicalized_resource.empty?
130128
mandatory_properties = {}
131129
else
132-
canonicalized_resource = @@cached_canonicalized_resource[0].dup
130+
canonicalized_resource = @cached_canonicalized_resource[0].dup
133131
mandatory_properties = canonicalized_resource.select do |attribute, _value|
134132
(mandatory_get_attributes(context) - namevar_attributes(context)).include?(attribute)
135133
end
@@ -266,9 +264,9 @@ def invoke_dsc_resource(context, name_hash, props, method)
266264
if error.include?('Logon failure: the user has not been granted the requested logon type at this computer')
267265
logon_error = "PSDscRunAsCredential account specified (#{name_hash[:dsc_psdscrunascredential]['user']}) does not have appropriate logon rights; are they an administrator?"
268266
name_hash[:name].nil? ? context.err(logon_error) : context.err(name_hash[:name], logon_error)
269-
@@logon_failures << name_hash[:dsc_psdscrunascredential].dup
267+
@logon_failures << name_hash[:dsc_psdscrunascredential].dup
270268
# This is a hack to handle the query cache to prevent a second lookup
271-
@@cached_query_results << name_hash # if fetch_cached_hashes(@@cached_query_results, [data]).empty?
269+
@cached_query_results << name_hash # if fetch_cached_hashes(@cached_query_results, [data]).empty?
272270
else
273271
context.err(error)
274272
end
@@ -292,7 +290,7 @@ def invoke_dsc_resource(context, name_hash, props, method)
292290
def insync?(context, name, _property_name, _is_hash, should_hash)
293291
return nil if should_hash[:validation_mode] != 'resource'
294292

295-
prior_result = fetch_cached_hashes(@@cached_test_results, [name])
293+
prior_result = fetch_cached_hashes(@cached_test_results, [name])
296294
prior_result.empty? ? invoke_test_method(context, name, should_hash) : prior_result.first[:in_desired_state]
297295
end
298296

@@ -361,7 +359,7 @@ def invoke_get_method(context, name_hash)
361359
data = recursively_sort(data)
362360

363361
# Cache the query to prevent a second lookup
364-
@@cached_query_results << data.dup if fetch_cached_hashes(@@cached_query_results, [data]).empty?
362+
@cached_query_results << data.dup if fetch_cached_hashes(@cached_query_results, [data]).empty?
365363
context.debug("Returned to Puppet as #{data}")
366364
data
367365
end
@@ -400,7 +398,7 @@ def invoke_test_method(context, name, should)
400398
return nil if data.nil?
401399

402400
in_desired_state = data['indesiredstate']
403-
@@cached_test_results << name.merge({ in_desired_state: in_desired_state })
401+
@cached_test_results << name.merge({ in_desired_state: in_desired_state })
404402

405403
return in_desired_state if in_desired_state
406404

@@ -451,7 +449,7 @@ def vendored_modules_path(module_name)
451449
# path to allow multiple modules to with shared dsc_resources to be installed side by side
452450
# The old vendored_modules_path: puppet_x/dsc_resources
453451
# The new vendored_modules_path: puppet_x/<module_name>/dsc_resources
454-
root_module_path = load_path.find { |path| path.match?(%r{#{puppetize_name(module_name)}/lib}) }
452+
root_module_path = load_path.grep(%r{#{puppetize_name(module_name)}/lib}).first
455453
vendored_path = if root_module_path.nil?
456454
File.expand_path(Pathname.new(__FILE__).dirname + '../../../' + "puppet_x/#{puppetize_name(module_name)}/dsc_resources") # rubocop:disable Style/StringConcatenation
457455
else
@@ -509,12 +507,12 @@ def random_variable_name
509507
#
510508
# @return [Hash] containing all instantiated variables and the properties that they define
511509
def instantiated_variables
512-
@@instantiated_variables ||= {}
510+
@instantiated_variables ||= {}
513511
end
514512

515513
# Clear the instantiated variables hash to be ready for the next run
516514
def clear_instantiated_variables!
517-
@@instantiated_variables = {}
515+
@instantiated_variables = {}
518516
end
519517

520518
# Return true if the specified credential hash has already failed to execute a DSC resource due to
@@ -523,7 +521,7 @@ def clear_instantiated_variables!
523521
# @param [Hash] a credential hash with a user and password keys where the password is a sensitive string
524522
# @return [Bool] true if the credential_hash has already failed logon, false otherwise
525523
def logon_failed_already?(credential_hash)
526-
@@logon_failures.any? do |failure_hash|
524+
@logon_failures.any? do |failure_hash|
527525
failure_hash['user'] == credential_hash['user'] && failure_hash['password'].unwrap == credential_hash['password'].unwrap
528526
end
529527
end

0 commit comments

Comments
 (0)