-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(GH-55) Handle intentionally empty arrays #56
(GH-55) Handle intentionally empty arrays #56
Conversation
Prior to this commit the DSC base provider would occasionally need to send an empty array as a defined property to Invoke-DscResource. However, the CIM instance on the other end may not be able to appropriately cast from an empty array to, say a [String[]] type; this would cause the run to fail. Furthermore, the provider did not have a way to distinguish between an *absent* setting and an *empty* one. Ruby does not treat empty arrays and nil as the same, even though PowerShell can/does. This commit adds a special handler for munging the data returned from a get invocation, ensuring that if a propertys value is nil AND the property was used for the query AND the resource expects an array, THEN it will set the result to the manifest value or an empty array. It also strongly types any empty arrays being sent to Invoke-DscResource, as this prevents the casting problem by making the array type casting explicit instead of merely implicit. - Resolves #55
Lots of rubocop changes since last PR, will address those in an additional maint commit. |
The move from master -> main broke CI.
@@ -283,9 +285,9 @@ def should_to_resource(should, context, dsc_invoke_method) | |||
# During a Puppet agent run, the code lives in the cache so we can use the file expansion to discover the correct folder. | |||
root_module_path = $LOAD_PATH.select { |path| path.match?(%r{#{resource[:dscmeta_module_name].downcase}/lib}) }.first | |||
resource[:vendored_modules_path] = if root_module_path.nil? | |||
File.expand_path(Pathname.new(__FILE__).dirname + '../../../' + 'puppet_x/dsc_resources') | |||
File.expand_path(Pathname.new(__FILE__).dirname + '../../../' + 'puppet_x/dsc_resources') # rubocop:disable Style/StringConcatenation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rubocop: Hey man! You can't do that
Lombardi: STFU
Me: LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I felt like in this case, with going up-level, it was more readable/understandable with the + instead of interpolation - looks like steps to get to what you want, imo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey man, you had me at rubocop:disable
.
empty_array_parameters = resource[:parameters].select { |_k, v| v[:value].empty? } | ||
empty_array_parameters.each do |name, properties| | ||
param_block_name = name.to_s.gsub(/^dsc_/, '') | ||
params_block = params_block.gsub("#{param_block_name} = @()", "#{param_block_name} = [#{properties[:mof_type]}]@()") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JFC. I'm both glad you were able to figure this out, and saddened that you had to. I like it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an extremely unclever hack but it did work in this case.
Prior to this commit the DSC base provider would occasionally need to send an empty array as a defined property to
Invoke-DscResource
. However, the CIM instance on the other end may not be able to appropriately cast from an empty array to, say a[String[]]
type; this would cause the run to fail. Furthermore, the provider did not have a way to distinguish between an absent setting and an empty one. Ruby does not treat empty arrays and nil as the same, even though PowerShell can/does.This commit adds a special handler for munging the data returned from a get invocation, ensuring that if a property's value is nil AND the property was used for the query AND the resource expects an array, THEN it will set the result to the manifest value or an empty array.
It also strongly types any empty arrays being sent
to
Invoke-DscResource
, as this prevents the casting problem by making the array type casting explicit instead of merely implicit.