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-172) Enable use of class-based DSC Resources by munging PSModulePath #173

Merged
merged 1 commit into from
Jul 1, 2021
Merged

(GH-172) Enable use of class-based DSC Resources by munging PSModulePath #173

merged 1 commit into from
Jul 1, 2021

Conversation

michaeltlombardi
Copy link
Contributor

@michaeltlombardi michaeltlombardi commented Jul 1, 2021

Prior to this PR, it was not possible to use the DSC Base Provider to invoke class-based DSC Resources. DSC 1.1 (the version of DSC used by Windows PowerShell 5.1) cannot call Invoke-DscResource for class- based resources by module path, only by module name; further more, the module containing the DSC Resource must be available in the system level PSModulePath environment variable; munging the process level PSModulePath does not work.

This PR updates the DSC Base Provider to:

  1. Read the dscmeta_resource_implementation value from a the Puppet Resource API type definition of a resource and inject it into the invocable resource definition
  2. Add the munge_psmodulepath method to return the PowerShell code to modify the system-level PSModulePath if the invocable resource is is implemented as a PowerShell class; if the resource is any other implementation it instead returns nil
  3. Modify the invoke_params method to set the ModuleName key to the PowerShell module's name instead of the path to the module's manifest if the resource is implemented as a PowerShell class
  4. Inject the PowerShell code from munge_psmodulepath into the output from the ps_script_content method to ensure it will be called when invoking the resource
  5. Update the try/catch in the postscript code file to have a finally block which resets the PSModulePath if it was munged earlier in the invocation script, regardless of whether or not the invocation errors

TODO:

  • Add acceptance tests for class-based resources
  • Add unit tests for munge_psmodulepath
  • All existing tests pass (update if needed)

@michaeltlombardi michaeltlombardi added the enhancement New feature or request label Jul 1, 2021
@michaeltlombardi michaeltlombardi requested a review from a team as a code owner July 1, 2021 12:26
Prior to this commit, it was not possible to use the DSC Base Provider
to invoke class-based DSC Resources. DSC `1.1` (the version of DSC used
by Windows PowerShell `5.1`) cannot call `Invoke-DscResource` for class-
based resources by module path, only by module name; further more, the
module containing the DSC Resource _must_ be available in the *system*
level `PSModulePath` environment variable; munging the *process* level
`PSModulePath` does not work.

This commit updates the DSC Base Provider to:

1. Read the `dscmeta_resource_implementation` value from a the Puppet
   Resource API type definition of a resource and inject it into the
   invocable resource definition
2. Add the `munge_psmodulepath` method to return the PowerShell code to
   modify the system-level `PSModulePath` if the invocable resource is
   is implemented as a PowerShell class; if the resource is any other
   implementation it instead returns nil
3. Modify the `invoke_params` method to set the `ModuleName` key to the
   PowerShell module's name instead of the path to the module's manifest
   if the resource is implemented as a PowerShell class
4. Inject the PowerShell code from `munge_psmodulepath` into the output
   from the `ps_script_content` method to ensure it will be called when
   invoking the resource
5. Update the try/catch in the postscript code file to have a `finally`
   block which resets the `PSModulePath` if it was munged earlier in the
   invocation script, regardless of whether or not the invocation errors
@michaeltlombardi michaeltlombardi changed the title (WIP) Munge PSModulePath for class-based resources (GH-172) Munge PSModulePath for class-based resources Jul 1, 2021
@michaeltlombardi michaeltlombardi changed the title (GH-172) Munge PSModulePath for class-based resources (GH-172) Enable use of class-based DSC Resources by munging PSModulePath Jul 1, 2021
@RandomNoun7 RandomNoun7 merged commit 85cb182 into puppetlabs:main Jul 1, 2021
@michaeltlombardi michaeltlombardi deleted the gh-172/main/handle-class-based-resources branch July 1, 2021 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants