Skip to content

Commit 7746890

Browse files
author
jordanbreen28
committed
(CAT-1724) - Fix Provider returned data not matching Type Schema
Prior to this commit, we would see the above error returned when dsc would return a nil value for a required string in its data hash. As puppet is not expecting a nil value, but rather an empty string, the error is raised on each puppet run where this occurs. We can fix this by converting all required string attributes values in the data hash from nil to an empty string ('').
1 parent 9203b8d commit 7746890

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lib/puppet/provider/dsc_base_provider/dsc_base_provider.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def insync?(context, name, _property_name, _is_hash, should_hash)
351351
# @param context [Object] the Puppet runtime context to operate in and send feedback to
352352
# @param name_hash [Hash] the hash of namevars to be passed as properties to `Invoke-DscResource`
353353
# @return [Hash] returns a hash representing the DSC resource munged to the representation the Puppet Type expects
354-
def invoke_get_method(context, name_hash)
354+
def invoke_get_method(context, name_hash) # rubocop:disable Metrics/AbcSize
355355
context.debug("retrieving #{name_hash.inspect}")
356356

357357
query_props = name_hash.select { |k, v| mandatory_get_attributes(context).include?(k) || (k == :dsc_psdscrunascredential && !v.nil?) }
@@ -394,6 +394,8 @@ def invoke_get_method(context, name_hash)
394394
# If a resource is found, it's present, so refill this Puppet-only key
395395
data[:name] = name_hash[:name]
396396

397+
data = stringify_nil_attributes(context, data)
398+
397399
# Have to check for this to avoid a weird canonicalization warning
398400
# The Resource API calls canonicalize against the current state which
399401
# will lead to dsc_ensure being set to absent in the name_hash even if
@@ -663,6 +665,20 @@ def mandatory_set_attributes(context)
663665
context.type.attributes.select { |_attribute, properties| properties[:mandatory_for_set] }.keys
664666
end
665667

668+
# Parses the DSC resource type definition to retrieve the names of any attributes which are specifed as required strings
669+
# This is used to ensure that any nil values are converted to empty strings to match the DSC behaviour
670+
# @param context [Object] the Puppet runtime context to operate in and send feedback to
671+
# @param data [Hash] the hash of properties returned from the DSC resource
672+
# @return [Hash] returns a data hash with any nil values converted to empty strings
673+
def stringify_nil_attributes(context, data)
674+
nil_strings = data.select { |_name, value| value.nil? }.keys
675+
string_attrs = context.type.attributes.select { |_name, properties| properties[:type] == 'String' }.keys
676+
string_attrs.each do |attribute|
677+
data[attribute] = '' if nil_strings.include?(attribute)
678+
end
679+
data
680+
end
681+
666682
# Parses the DSC resource type definition to retrieve the names of any attributes which are specified as namevars
667683
#
668684
# @param context [Object] the Puppet runtime context to operate in and send feedback to

0 commit comments

Comments
 (0)