diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index c8be165e3..296297425 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 'TestClassResource/TestClassResource').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 d5417775e..32ec539d5 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 ffee99d94..65e9193a2 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 }