Skip to content

Commit 3419747

Browse files
committed
Fix discovery of capabilities PSAdapter
1 parent 90b4e2a commit 3419747

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

powershell-adapter/Tests/powershellgroup.resource.tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Describe 'PowerShell adapter resource tests' {
2828
$LASTEXITCODE | Should -Be 0
2929
$resources = $r | ConvertFrom-Json
3030
($resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' }).Count | Should -Be 1
31+
($resources | Where-Object -Property type -EQ 'PSClassResource/PSClassResource').capabilities | Should -BeIn @('get', 'set', 'test', 'export')
3132
}
3233

3334
It 'Get works on class-based resource' {

powershell-adapter/psDscAdapter/powershell.resource.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ switch ($Operation) {
9494
if ($module.PrivateData.PSData.DscCapabilities) {
9595
$capabilities = $module.PrivateData.PSData.DscCapabilities
9696
}
97+
elseif ($DscResourceInfo.Methods) {
98+
$capabilities = $DscResourceInfo.Methods
99+
}
97100
else {
98101
$capabilities = @('get', 'set', 'test')
99102
}

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4-
$script:CurrentCacheSchemaVersion = 2
4+
$script:CurrentCacheSchemaVersion = 3
55

66
function Write-DscTrace {
77
param(
@@ -117,7 +117,7 @@ function FindAndParseResourceDefinitions {
117117
$typeDefinitions = $ast.FindAll(
118118
{
119119
$typeAst = $args[0] -as [System.Management.Automation.Language.TypeDefinitionAst]
120-
return $typeAst -ne $null;
120+
return $null -ne $typeAst;
121121
},
122122
$false);
123123

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

141141
$DscResourceInfo.Properties = [System.Collections.Generic.List[DscResourcePropertyInfo]]::new()
142+
$DscResourceInfo.Methods = GetClassBasedCapabilities $typeDefinitionAst.Members
142143
Add-AstMembers $typeDefinitions $typeDefinitionAst $DscResourceInfo.Properties
143144

144145
$resourceList.Add($DscResourceInfo)
@@ -529,6 +530,28 @@ function GetTypeInstanceFromModule {
529530
return $instance
530531
}
531532

533+
function GetClassBasedCapabilities ($functionMemberAst) {
534+
$capabilities = @()
535+
# These are the methods that we can potentially expect in a class-based DSC resource.
536+
$availableMethods = @('get', 'set', 'setHandlesExist', 'whatIf', 'test', 'delete', 'export')
537+
$methods = $functionMemberAst | Where-Object { $_ -is [System.Management.Automation.Language.FunctionMemberAst] -and $_.Name -in $availableMethods }
538+
539+
foreach ($method in $methods.Name) {
540+
# We go through each method to properly case handle the method names.
541+
switch ($method) {
542+
'Get' { $capabilities += 'get' }
543+
'Set' { $capabilities += 'set' }
544+
'Test' { $capabilities += 'test' }
545+
'WhatIf' { $capabilities += 'whatIf' }
546+
'SetHandlesExist' { $capabilities += 'setHandlesExist' }
547+
'Delete' { $capabilities += 'delete' }
548+
'Export' { $capabilities += 'export' }
549+
}
550+
}
551+
552+
return ($capabilities | Select-Object -Unique)
553+
}
554+
532555
# cached resource
533556
class dscResourceCacheEntry {
534557
[string] $Type
@@ -578,4 +601,5 @@ class DscResourceInfo {
578601
[string] $ImplementedAs
579602
[string] $CompanyName
580603
[System.Collections.Generic.List[DscResourcePropertyInfo]] $Properties
604+
[string[]] $Methods
581605
}

0 commit comments

Comments
 (0)