Skip to content

Fix discovery of capabilities PSAdapter #876

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ Describe 'PowerShell adapter resource tests' {
$r = dsc resource list '*' -a Microsoft.DSC/PowerShell
$LASTEXITCODE | Should -Be 0
$resources = $r | ConvertFrom-Json
($resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' }).Count | Should -Be 1
($resources | Where-Object { $_.Type -eq 'TestClassResource/TestClassResource' }).Count | Should -Be 1
($resources | Where-Object -Property type -EQ 'TestClassResource/TestClassResource').capabilities | Should -BeIn @('get', 'set', 'test', 'export')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should be more specific. Looking at the psd1, it should only be exactly get and test. Maybe it makes sense to have another test resource that has set and export

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly why this change was made. If it cannot find it from the class, it looks at the .psd1 file. If the .psd1 file doesn't contain the DscCapabilities, it defaults back to get, set, and test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Perhaps you can add a comment in the code indicating this is the behavior.

($resources | Where-Object -Property type -EQ 'TestClassResource/NoExport').capabilities | Should -BeIn @('get', 'set', 'test')
}

It 'Get works on class-based resource' {
Expand Down
8 changes: 5 additions & 3 deletions powershell-adapter/psDscAdapter/powershell.resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ switch ($Operation) {
# TODO: for perf, it is better to take capabilities from psd1 in Invoke-DscCacheRefresh, not by extra call to Get-Module
if ($DscResourceInfo.ModuleName) {
$module = Get-Module -Name $DscResourceInfo.ModuleName -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1
if ($module.PrivateData.PSData.DscCapabilities) {
if ($DscResourceInfo.Capabilities) {
$capabilities = $DscResourceInfo.Capabilities
} elseif ($module.PrivateData.PSData.DscCapabilities) {

$capabilities = $module.PrivateData.PSData.DscCapabilities
}
else {
} else {
$capabilities = @('get', 'set', 'test')
}
}
Expand Down
30 changes: 27 additions & 3 deletions powershell-adapter/psDscAdapter/psDscAdapter.psm1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

$script:CurrentCacheSchemaVersion = 2
$script:CurrentCacheSchemaVersion = 3

function Write-DscTrace {
param(
Expand Down Expand Up @@ -60,7 +60,7 @@ function Add-AstMembers {

foreach ($member in $TypeAst.Members) {
$property = $member -as [System.Management.Automation.Language.PropertyMemberAst]
if (($property -eq $null) -or ($property.IsStatic)) {
if (($null -eq $property) -or ($property.IsStatic)) {
continue;
}
$skipProperty = $true
Expand Down Expand Up @@ -117,7 +117,7 @@ function FindAndParseResourceDefinitions {
$typeDefinitions = $ast.FindAll(
{
$typeAst = $args[0] -as [System.Management.Automation.Language.TypeDefinitionAst]
return $typeAst -ne $null;
return $null -ne $typeAst;
},
$false);

Expand All @@ -139,6 +139,7 @@ function FindAndParseResourceDefinitions {
$DscResourceInfo.Version = $moduleVersion

$DscResourceInfo.Properties = [System.Collections.Generic.List[DscResourcePropertyInfo]]::new()
$DscResourceInfo.Capabilities = GetClassBasedCapabilities $typeDefinitionAst.Members
Add-AstMembers $typeDefinitions $typeDefinitionAst $DscResourceInfo.Properties

$resourceList.Add($DscResourceInfo)
Expand Down Expand Up @@ -529,6 +530,28 @@ function GetTypeInstanceFromModule {
return $instance
}

function GetClassBasedCapabilities ($functionMemberAst) {
$capabilities = [System.Collections.Generic.List[string[]]]::new()
# These are the methods that we can potentially expect in a class-based DSC resource.
$availableMethods = @('get', 'set', 'setHandlesExist', 'whatIf', 'test', 'delete', 'export')
$methods = $functionMemberAst | Where-Object { $_ -is [System.Management.Automation.Language.FunctionMemberAst] -and $_.Name -in $availableMethods }

foreach ($method in $methods.Name) {
# We go through each method to properly case handle the method names.
switch ($method) {
'Get' { $capabilities.Add('get') }
'Set' { $capabilities.Add('set') }
'Test' { $capabilities.Add('test') }
'WhatIf' { $capabilities.Add('whatIf') }
'SetHandlesExist' { $capabilities.Add('setHandlesExist') }
'Delete' { $capabilities.Add('delete') }
'Export' { $capabilities.Add('export') }
}
}

return ($capabilities | Select-Object -Unique)
}

# cached resource
class dscResourceCacheEntry {
[string] $Type
Expand Down Expand Up @@ -578,4 +601,5 @@ class DscResourceInfo {
[string] $ImplementedAs
[string] $CompanyName
[System.Collections.Generic.List[DscResourcePropertyInfo]] $Properties
[string[]] $Capabilities
}
Loading