From 8d168ae807be59474d1c4fe7b060ad11cd3d1b97 Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Tue, 10 Jun 2025 05:22:40 +0200 Subject: [PATCH 1/2] Fix discovery of capabilities PSAdapter --- .../Tests/powershellgroup.resource.tests.ps1 | 1 + .../psDscAdapter/powershell.resource.ps1 | 3 ++ .../psDscAdapter/psDscAdapter.psm1 | 28 +++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index c8be165e..87226277 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -28,6 +28,7 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json ($resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' }).Count | Should -Be 1 + ($resources | Where-Object -Property type -EQ 'PSClassResource/PSClassResource').capabilities | Should -BeIn @('get', 'set', 'test', 'export') } It 'Get works on class-based resource' { diff --git a/powershell-adapter/psDscAdapter/powershell.resource.ps1 b/powershell-adapter/psDscAdapter/powershell.resource.ps1 index d5417775..32ec539d 100644 --- a/powershell-adapter/psDscAdapter/powershell.resource.ps1 +++ b/powershell-adapter/psDscAdapter/powershell.resource.ps1 @@ -94,6 +94,9 @@ switch ($Operation) { if ($module.PrivateData.PSData.DscCapabilities) { $capabilities = $module.PrivateData.PSData.DscCapabilities } + elseif ($DscResourceInfo.Methods) { + $capabilities = $DscResourceInfo.Methods + } else { $capabilities = @('get', 'set', 'test') } diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index ffee99d9..65e9193a 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. -$script:CurrentCacheSchemaVersion = 2 +$script:CurrentCacheSchemaVersion = 3 function Write-DscTrace { param( @@ -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); @@ -139,6 +139,7 @@ function FindAndParseResourceDefinitions { $DscResourceInfo.Version = $moduleVersion $DscResourceInfo.Properties = [System.Collections.Generic.List[DscResourcePropertyInfo]]::new() + $DscResourceInfo.Methods = GetClassBasedCapabilities $typeDefinitionAst.Members Add-AstMembers $typeDefinitions $typeDefinitionAst $DscResourceInfo.Properties $resourceList.Add($DscResourceInfo) @@ -529,6 +530,28 @@ function GetTypeInstanceFromModule { return $instance } +function GetClassBasedCapabilities ($functionMemberAst) { + $capabilities = @() + # 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 += 'get' } + 'Set' { $capabilities += 'set' } + 'Test' { $capabilities += 'test' } + 'WhatIf' { $capabilities += 'whatIf' } + 'SetHandlesExist' { $capabilities += 'setHandlesExist' } + 'Delete' { $capabilities += 'delete' } + 'Export' { $capabilities += 'export' } + } + } + + return ($capabilities | Select-Object -Unique) +} + # cached resource class dscResourceCacheEntry { [string] $Type @@ -578,4 +601,5 @@ class DscResourceInfo { [string] $ImplementedAs [string] $CompanyName [System.Collections.Generic.List[DscResourcePropertyInfo]] $Properties + [string[]] $Methods } From 8c21a21cbc2c5358c389ab5e2c7ea8bed18e2ea5 Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Tue, 10 Jun 2025 05:59:24 +0200 Subject: [PATCH 2/2] Fix test --- powershell-adapter/Tests/powershellgroup.resource.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index 87226277..29629742 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -28,7 +28,7 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json ($resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' }).Count | Should -Be 1 - ($resources | Where-Object -Property type -EQ 'PSClassResource/PSClassResource').capabilities | Should -BeIn @('get', 'set', 'test', 'export') + ($resources | Where-Object -Property type -EQ 'TestClassResource/TestClassResource').capabilities | Should -BeIn @('get', 'set', 'test', 'export') } It 'Get works on class-based resource' {