PowerShell module for DSCv3 #508
Replies: 2 comments 1 reply
-
Mikey, while going through the text you've just written, I was amazed that you could extract that much information from the PRs I opened. Thanks for the time and detail you placed in this discussion. I hope to see it getting some movement. |
Beta Was this translation helpful? Give feedback.
-
Feature Group: Enhanced resource authoring and inspection
Feature: DSC base class and/or interfaceWe could provide a base class that resource authors can inherit from that implements synthetic testing, converting to and from YAML, returning a manifest for the resource, and otherwise makes implementing a DSC resource as a PowerShell class easier. The downside to using a base class is that it requires taking a dependency on the DSC module itself, but the benefits are pretty powerful. We could also provide an interface with default implementations to help users understand how to implement their own resources, or extend existing classes to work as a DSC resource without deriving from the base class. This option is not exclusive to providing a base class. Feature: DSC resource attributesWe could provide attributes that authors can use to decorate their resource classes and their members. For example, consider the following pseudocode snippet: # Define metadata about the resource
[DscResourceInfo(
Description = "Manages the existence and contents of UTF8-encoded text files.",
Version = "1.2.0",
SchemaUri = "tailspintoys.com/schemas/dsc/resources/utf8file.schema.json",
DocsUri = "tailspintoys.com/docs/dsc/resources/utf8file",
UseSyntheticTest
)]
class Utf8File {
#region Resource properties
# Define the property, don't specify a JSON schema - auto-generate from definition.
[DscResourceProperty(
Key,
Required,
Description = @'
The path to the file the instance manages. The path can be absolute
or relative. Relative paths are resolved against the current working
directory when you invoke the resource.
'@
)]
[string] $Path
# Indicates that this property uses the semantics of the _exist property
[DscResourceStandardProperty()]
[bool] $Exist
# Only defines the description for the property, uses default options
[DscResourceProperty("The contents of the file.")]
[string] $Content
# Read only property with custom JSON schema
[DscResourceProperty(ReadOnly)]
[DscJsonSchema(@'
{
"title": "File length",
"description": "The size of the file in bytes.",
"type": "integer",
"minimum": 0
}
'@)]
[int] $Length
# Read only properties with description
[DscResourceProperty(
ReadOnly,
Description = "Defines when the file was created."
)]
[datetime] $CreationTimeUtc
[DscResourceProperty(
ReadOnly,
Description = "Defines when the file was last updated."
)]
[datetime] $LastWriteTimeUtc
# Write-only property
[DscResourceProperty(
WriteOnly,
Description = "Indicates whether the resource should create parent folders if they don't exist."
)]
[bool] $CreateParentFolders = $true
#endregion Resource properties
#region Resource methods
[Utf8File] Get() {
# Implementation
}
# Don't implement test, rely on synthetic test behavior
[DscResourceSetMethod(ImplementsPreTest, HandlesExist)]
[Utf8File] Set() {
# Implementation
}
# Support the delete method
[void] Delete() {
# Implementation
}
#endregion Resource methods The attributes provide information that authors can't currently surface for PSDSC resources. More consideration is required to determine the design and scope of such attributes. Like a base class or interface, using the attributes requires taking a dependency on the module. Feature: DSC resource static metadata methodsAs an alternative to using the resource attributes, we can define and document static methods that authors can implement on their resources to provide the same information. Consider the following pseudocode snippet: class Utf8File {
#region Static metadata methods
static [hashtable] GetResourceInfo() {
return @{
Description = 'Manages the existence and contents of UTF8-encoded text files.'
Version = '1.2.0'
SchemaUri = 'tailspintoys.com/schemas/dsc/resources/utf8file.schema.json'
DocsUri = 'tailspintoys.com/docs/dsc/resources/utf8file'
UseSyntheticTest = $true
}
}
static [hashtable] GetResourcePropertyInfo() {
return @{
Path = @{
Key = $true
Required = $true,
Description = @'
The path to the file the instance manages. The path can be absolute
or relative. Relative paths are resolved against the current working
directory when you invoke the resource.
'@
}
Exist = @{ StandardProperty = $true }
Content = @{
Description = 'The contents of the file.'
}
Length = @{
ReadOnly = $true
JsonSchema = @'
{
"title": "File length",
"description": "The size of the file in bytes.",
"type": "integer",
"minimum": 0
}
'@
}
CreationTimeUtc = @{
ReadOnly = $true
Description = 'Defines when the file was created.'
}
LastWriteTimeUtc = @{
ReadOnly = $true
Description = 'Defines when the file was last updated.'
}
CreateParentFolders = @{
WriteOnly = $true
Description = "Indicates whether the resource should create parent folders if they don't exist."
}
}
}
static [hashtable] GetResourceMethodInfo() {
return @{
# No entry for test because the resource uses synthetic test
Set = @{
ImplementsPreTest = $true
HandlesExist = $true
}
}
}
#endregion Static metadata methods
#region Resource properties
[string] $Path
[bool] $Exist
[int] $Length
[datetime] $CreationTimeUtc
[datetime] $LastWriteTimeUtc
[bool] $CreateParentFolders = $true
#endregion Resource properties
#region Resource methods
[Utf8File] Get() {
# Implementation
}
# Don't implement test, rely on synthetic test behavior
[Utf8File] Set() {
# Implementation
}
# Support the delete method
[void] Delete() {
# Implementation
}
#endregion Resource methods
} This option is less convenient for resource authors, but we could also support per-member static methods or similar other solutions. I propose using static methods rather than properties, because the static properties can't always be guaranteed to return their initial values. Feature: Support v3 semantics with static methods, overloads, or new instance methodsCurrently, any PSDSC resource with the [DscResource()]` attribute must implement the old interface for resources:
For PSDSC-compatible resources, we have a few options for addressing these limitations:
This problem primarily effects being able to return better information from Feature:
|
Beta Was this translation helpful? Give feedback.
-
Note
This discussion references the work and ideas of @Gijsreyn, especially those raised in #498 and #507.
For the purposes of this discussion, I'm using the term PowerShell DSC (PSDSC) to refer to versions 1 and 2 and the PSDesiredStateConfiguration module.
Bottom Line Up Front
There are good reasons to believe that developing and maintaining a PowerShell module for DSCv3 would be useful for several personas and tasks:
Current Context
At this time, the new
Microsoft.PowerShell/DSC
resource adapter enables users to leverage class-based PSDSC resources in DSC and supports a growing number of features that don't exist in PSDSC. TheMicrosoft.Windows.PowerShell/DSC
resource adapter enables users to leverage PSDSC resources compatible with Windows PowerShell and PSDSC in DSC.Every command for DSC has a JSON schema describing the objects that the commands expect and return as output, but working with those objects requires users to convert to and from JSON or YAML manually and manage them as dictionaries or PSCustomObjects.
Users with PSDSC configurations have to recreate those configurations manually in a new DSC configuration document by hand.
Anyone who wants to create or manipulate DSC configuration documents in PowerShell has to implement their own solution.
While the adapters recognize enhancements to PSDSC resources for DSC, authors get no author-time feedback for their resources. They can't use commands to inspect their resources to determine what capabilities their resources have or whether they're implemented to best practices.
Proposed Features
Replies to this discussion include proposals for the usage, design and scope of features for a module that enhances the user and developer experience for DSC in PowerShell.
Beta Was this translation helpful? Give feedback.
All reactions