-
Notifications
You must be signed in to change notification settings - Fork 41
Proposal: Define method signatures for PSDSC resource classes #860
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
Comments
We should really be using defined interfaces so that the user and adapter doesn't have to parse or discover method signatures. |
That's one option, for sure - it will require defining the interfaces in C# (since we can't do so in PowerShell). Other options, which I've been considering for the RDK, include:
My main (but not insurmountable) reluctance to requiring an interface is that my experience with the community shows that contributions to C# modules are at least an order of magnitude less frequent than for modules implemented in PowerShell. My secondary reluctance to relying on an interface is the need to define several of them - because in DSC, not every resource needs to implement every method overload. I think this isn't a huge problem, but it's worth noting. |
Might be missing a memo, but don't we use Ensure = Absent for deleting resources at present? |
It has changed to '_exist'. See: https://learn.microsoft.com/en-us/powershell/dsc/reference/schemas/resource/properties/exist?view=dsc-3.0#description |
Thanks. Why do we need the 'delete' operation as well as _exist = false; don't they do the same thing? |
I can explain it in my words, but I think the explanation by Steve on: #290 explains it perfectly :) |
@Gijsreyn Ah I see, to make module authoring easier. That seems to make sense so long as 'delete' remains an optional operation. |
Most DSC operations are now optional. For practical purposes, most resources only need to implement the get and set operations to enable using the resource in configuration documents. |
Summary of the new feature / enhancement
Currently, implementing a PSDSC resource requires the following method signatures, which map to the operations in PSDSC:
Returns a single instance of the class representing the actual current state.
[bool] Test()
Returns
$true
if the instance is in the desired state and$false
otherwise.[void] Set()
Enforces the desired state for the instance.
These method signatures are checked by the parser when a PowerShell class has the
[DscResource()]
attribute. The parser also checks:[DscProperty(Key)]
attribute.For DSC, we have added support for the export operation with the following method signature:
However, we don't currently support:
Test()
method returns$false
,I propose that we consider and define method signatures in support of the semantics of DSC. When we implement the PowerShell resource development kit (RDK), we can provide a bridge between classic implementations for PSDSC and DSC-compliant semantics through a base class.
Proposed technical implementation details (optional)
I propose supporting the following method signatures for PowerShell classes.
Test operation method signatures
Result object (requires dependency)
This signature assumes the availability of the Microsoft.PowerShell.Dsc module to provide the
[DscResourceTestResult]
, which would have a definition like:Which we could use to provide the equivalent of the
state
andstateAndDiff
outputs without requiring the resource to define anInDesiredState
property. TheDscResourceInstance
class is just a lightweight type to ensure that the value is always an instance of a DSC resource.Tuples (no dependency)
This option is more complex for the author, but doesn't require a dependency.
Set operation method signatures
Here we could enable resource authors to support what-if mode with less overhead. If the class doesn't implement the signature with the
whatIf
parameter, the resource doesn't support invoking the set operation in what-if mode. If the resource does support what-if mode, authors would only need to implement their code in that method. The other method could just invoke the what-if mode method withwhatIf
as$false
.Result object (requires dependency)
This signature assumes the availability of the Microsoft.PowerShell.Dsc module to provide the
[DscResourceSetResult]
, which would have a definition like:Which we could use to provide the equivalent of the
state
andstateAndDiff
outputs. TheDscResourceInstance
class is just a lightweight type to ensure that the value is always an instance of a DSC resource.Tuples (no dependency)
This option is more complex for the author, but doesn't require a dependency.
Delete operation method signatures
For resources that support the delete operation, we could expect the following signatures:
As with the set operation methods, this would enable authors to implement what-if support with
little overhead.
Export operation method signatures
For resources that support the export operation, we could expect the following signatures:
Here the resource author could choose whether to support export with filters, similar to supporting
what-if modes for the set and delete operations. We could infer from static analysis whether the
resource supports filtering.
Get operation method signature
This static method returns the actual state for the provided instance. This is nearly identical to the PSDSC signature, except that it's static (and thus requires passing the instance instead of invoking the method on an instance.
This is primarily to bring the method signature in line with the other proposed signatures.
Example classes
The following example classes show the methods together.
Without dependencies
With dependency (no base class)
With dependency and base class
Additional thoughts
We could certainly support both the friendly result types and the tuples for method output - it
would complicate the implementation for the adapter/module, but it would enable authors to choose
the implementation that best suits their needs.
All of this would be much friendlier with the RDK, where we could iteratively enhance the DevX for
resource authors writing PowerShell.
Separating the DSC methods from the PSDSC instance methods would also ensure we can adjust/enhance the supported definitions as the capabilities and expectations for DSC evolve without affecting the PSDSC functionality.
The text was updated successfully, but these errors were encountered: