Skip to content
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-154) Fix return data from Invoke-DscResource for empty strings and single item arrays in DSC Base Provider #159

Merged
merged 2 commits into from
Jun 21, 2021
Merged

(GH-154) Fix return data from Invoke-DscResource for empty strings and single item arrays in DSC Base Provider #159

merged 2 commits into from
Jun 21, 2021

Conversation

michaeltlombardi
Copy link
Contributor

Prior to this PR, the ConvertTo-CanonicalResource function used to munge the values Invoke-DscResource emits for the Get method into a usable hash for Puppet/Ruby to consume had minor bugs which:

  1. Caused canonicalization of values for empty strings to fail against enums (because Puppet treats an empty string as an invalid enum specification whereas it treats nil as undef)
  2. Caused canonicalization of values for CIM Instances which should be arrays to fail against single-item arrays (because the logic in ConvertTo-CanonicalResult mishandled turning single hashes into an array of hashes with a single item).

This PR corrects both issues.

@michaeltlombardi michaeltlombardi requested a review from a team as a code owner June 21, 2021 18:45
@michaeltlombardi michaeltlombardi changed the title (GH-154) Fix return data from Invoke-DscResource in base provider (GH-154) Fix return data from Invoke-DscResource for empty strings and single item arrays in DSC Base Provider Jun 21, 2021
Prior to this commit, the ConvertTo-CanonicalResult function used
to turn output from Invoke-DscResource with the Get method into
something Puppet can use would return empty strings for unset
values.

Puppet distinguishes between nil and an empty string when deciding
if a value has been defined - in these cases, nil is treated as
undef whereas an empty string is treated as a set value.

This causes canonicalization (and the run) to fail when the value
for an unset enum is returned as an empty string.

This commit modifies the logic in ConvertTo-CanonicalResult to
munge an empty string to nil to prevent invalid enum errors.
Prior to this commit, the logic for determining if a CIM Instance
value should be munged to an array value failed against hashes;
the logic checked to see if the count was less than 2 and, if so,
wrapped the value in an array.

This failed against hashes because the count property represents
the number of key-value pairs in a hash; this means that to the
original logic, a single hash with multiple keys would never be
wrapped in an array.

When passing back a single item for a value to Puppet which Puppet
expects to be an array, an error gets raised.

This commit corrects the logic by validating the typename of the
value includes the [] suffix and thereby ensures that values
returned for CIM instances which are single hashes and should be
treated as single-member arrays of hashes can be canonicalized.
@jpogran jpogran merged commit cc0ceb8 into puppetlabs:main Jun 21, 2021
@michaeltlombardi michaeltlombardi deleted the gh-154/main/fix-dsc-returns branch June 22, 2021 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants