6
6
require 'json'
7
7
8
8
class Puppet ::Provider ::DscBaseProvider
9
- # Initializes the provider, preparing the class variables which cache:
9
+ # Initializes the provider, preparing the instance variables which cache:
10
10
# - the canonicalized resources across calls
11
11
# - query results
12
12
# - logon failures
13
13
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 = [ ]
18
18
super
19
19
end
20
20
21
- def cached_test_results
22
- @@cached_test_results
23
- end
21
+ attr_reader :cached_test_results
24
22
25
23
# Look through a cache to retrieve the hashes specified, if they have been cached.
26
24
# Does so by seeing if each of the specified hashes is a subset of any of the hashes
27
25
# in the cache, so {foo: 1, bar: 2} would return if {foo: 1} was the search hash.
28
26
#
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
30
28
# @param hashes [Array] the list of hashes to search the cache for
31
29
# @return [Array] an array containing the matching hashes for the search condition, if any
32
30
def fetch_cached_hashes ( cache , hashes )
@@ -52,22 +50,22 @@ def canonicalize(context, resources)
52
50
# During RSAPI refresh runs mandatory parameters are stripped and not available;
53
51
# Instead of checking again and failing, search the cache for a namevar match.
54
52
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
56
54
if cached_result . nil?
57
55
# If the resource is meant to be absent, skip canonicalization and rely on the manifest
58
56
# value; there's no reason to compare system state to desired state for casing if the
59
57
# resource is being removed.
60
58
if r [ :dsc_ensure ] == 'absent'
61
59
canonicalized = r . dup
62
- @@ cached_canonicalized_resource << r . dup
60
+ @cached_canonicalized_resource << r . dup
63
61
else
64
62
canonicalized = invoke_get_method ( context , r )
65
63
# If the resource could not be found or was returned as absent, skip case munging and
66
64
# treat the manifest values as canonical since the resource is being created.
67
65
# rubocop:disable Metrics/BlockNesting
68
66
if canonicalized . nil? || canonicalized [ :dsc_ensure ] == 'absent'
69
67
canonicalized = r . dup
70
- @@ cached_canonicalized_resource << r . dup
68
+ @cached_canonicalized_resource << r . dup
71
69
else
72
70
parameters = r . select { |name , _properties | parameter_attributes ( context ) . include? ( name ) }
73
71
canonicalized . merge! ( parameters )
@@ -91,7 +89,7 @@ def canonicalize(context, resources)
91
89
canonicalized . delete ( key ) unless downcased_resource . key? ( key )
92
90
end
93
91
# Cache the actually canonicalized resource separately
94
- @@ cached_canonicalized_resource << canonicalized . dup
92
+ @cached_canonicalized_resource << canonicalized . dup
95
93
end
96
94
# rubocop:enable Metrics/BlockNesting
97
95
end
@@ -123,13 +121,13 @@ def get(context, names = nil)
123
121
context . debug ( 'Collecting data from the DSC Resource' )
124
122
125
123
# 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 )
127
125
return cached_results unless cached_results . empty?
128
126
129
- if @@ cached_canonicalized_resource . empty?
127
+ if @cached_canonicalized_resource . empty?
130
128
mandatory_properties = { }
131
129
else
132
- canonicalized_resource = @@ cached_canonicalized_resource [ 0 ] . dup
130
+ canonicalized_resource = @cached_canonicalized_resource [ 0 ] . dup
133
131
mandatory_properties = canonicalized_resource . select do |attribute , _value |
134
132
( mandatory_get_attributes ( context ) - namevar_attributes ( context ) ) . include? ( attribute )
135
133
end
@@ -266,9 +264,9 @@ def invoke_dsc_resource(context, name_hash, props, method)
266
264
if error . include? ( 'Logon failure: the user has not been granted the requested logon type at this computer' )
267
265
logon_error = "PSDscRunAsCredential account specified (#{ name_hash [ :dsc_psdscrunascredential ] [ 'user' ] } ) does not have appropriate logon rights; are they an administrator?"
268
266
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
270
268
# 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?
272
270
else
273
271
context . err ( error )
274
272
end
@@ -292,7 +290,7 @@ def invoke_dsc_resource(context, name_hash, props, method)
292
290
def insync? ( context , name , _property_name , _is_hash , should_hash )
293
291
return nil if should_hash [ :validation_mode ] != 'resource'
294
292
295
- prior_result = fetch_cached_hashes ( @@ cached_test_results , [ name ] )
293
+ prior_result = fetch_cached_hashes ( @cached_test_results , [ name ] )
296
294
prior_result . empty? ? invoke_test_method ( context , name , should_hash ) : prior_result . first [ :in_desired_state ]
297
295
end
298
296
@@ -361,7 +359,7 @@ def invoke_get_method(context, name_hash)
361
359
data = recursively_sort ( data )
362
360
363
361
# 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?
365
363
context . debug ( "Returned to Puppet as #{ data } " )
366
364
data
367
365
end
@@ -400,7 +398,7 @@ def invoke_test_method(context, name, should)
400
398
return nil if data . nil?
401
399
402
400
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 } )
404
402
405
403
return in_desired_state if in_desired_state
406
404
@@ -451,7 +449,7 @@ def vendored_modules_path(module_name)
451
449
# path to allow multiple modules to with shared dsc_resources to be installed side by side
452
450
# The old vendored_modules_path: puppet_x/dsc_resources
453
451
# 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
455
453
vendored_path = if root_module_path . nil?
456
454
File . expand_path ( Pathname . new ( __FILE__ ) . dirname + '../../../' + "puppet_x/#{ puppetize_name ( module_name ) } /dsc_resources" ) # rubocop:disable Style/StringConcatenation
457
455
else
@@ -509,12 +507,12 @@ def random_variable_name
509
507
#
510
508
# @return [Hash] containing all instantiated variables and the properties that they define
511
509
def instantiated_variables
512
- @@ instantiated_variables ||= { }
510
+ @instantiated_variables ||= { }
513
511
end
514
512
515
513
# Clear the instantiated variables hash to be ready for the next run
516
514
def clear_instantiated_variables!
517
- @@ instantiated_variables = { }
515
+ @instantiated_variables = { }
518
516
end
519
517
520
518
# 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!
523
521
# @param [Hash] a credential hash with a user and password keys where the password is a sensitive string
524
522
# @return [Bool] true if the credential_hash has already failed logon, false otherwise
525
523
def logon_failed_already? ( credential_hash )
526
- @@ logon_failures . any? do |failure_hash |
524
+ @logon_failures . any? do |failure_hash |
527
525
failure_hash [ 'user' ] == credential_hash [ 'user' ] && failure_hash [ 'password' ] . unwrap == credential_hash [ 'password' ] . unwrap
528
526
end
529
527
end
0 commit comments