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

It 'Get works on class-based resource' {
Expand Down
9 changes: 6 additions & 3 deletions powershell-adapter/psDscAdapter/powershell.resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ 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 the DscResourceInfo does have capabilities, use them or else use the module's capabilities
if ($DscResourceInfo.Capabilities) {
$capabilities = $DscResourceInfo.Capabilities
} elseif ($module.PrivateData.PSData.DscCapabilities) {

$capabilities = $module.PrivateData.PSData.DscCapabilities
}
else {
} else {
$capabilities = @('get', 'set', 'test')
}
}
Expand Down
34 changes: 29 additions & 5 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 @@ -325,7 +326,7 @@ function Invoke-DscCacheRefresh {

# fill in resource files (and their last-write-times) that will be used for up-do-date checks
$lastWriteTimes = @{}
Get-ChildItem -Recurse -File -Path $dscResource.ParentPath -Include "*.ps1", "*.psd1", "*.psm1", "*.mof" -ea Ignore | % {
Get-ChildItem -Recurse -File -Path $dscResource.ParentPath -Include "*.ps1", "*.psd1", "*.psm1", "*.mof" -ea Ignore | ForEach-Object {
$lastWriteTimes.Add($_.FullName, $_.LastWriteTime)
}

Expand All @@ -338,7 +339,7 @@ function Invoke-DscCacheRefresh {

[dscResourceCache]$cache = [dscResourceCache]::new()
$cache.ResourceCache = $dscResourceCacheEntries
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | % { Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue }
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | ForEach-Object { Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue }
$cache.PSModulePaths = $m.FullName
$cache.CacheSchemaVersion = $script:CurrentCacheSchemaVersion

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