Skip to content

Commit 05fed7c

Browse files
committed
Fix discovery on class-based resources
1 parent 56910ef commit 05fed7c

File tree

1 file changed

+81
-4
lines changed

1 file changed

+81
-4
lines changed

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ function Invoke-DscCacheRefresh {
230230
if ($classBased -and ($classBased.CustomAttributes.AttributeType.Name -eq 'DscResourceAttribute')) {
231231
"Detected class-based resource: $($dscResource.Name) => Type: $($classBased.BaseType.FullName)" | Write-DscTrace
232232
$dscResourceInfo.ImplementationDetail = 'ClassBased'
233+
$properties = GetClassBasedProperties -filePath $dscResource.Path -className $dscResource.Name
234+
if ($null -ne $properties) {
235+
$DscResourceInfo.Properties = $properties
236+
}
233237
}
234238

235239
# fill in resource files (and their last-write-times) that will be used for up-do-date checks
@@ -239,10 +243,10 @@ function Invoke-DscCacheRefresh {
239243
}
240244

241245
$dscResourceCacheEntries.Add([dscResourceCacheEntry]@{
242-
Type = "$moduleName/$($dscResource.Name)"
243-
DscResourceInfo = $DscResourceInfo
244-
LastWriteTimes = $lastWriteTimes
245-
})
246+
Type = "$moduleName/$($dscResource.Name)"
247+
DscResourceInfo = $DscResourceInfo
248+
LastWriteTimes = $lastWriteTimes
249+
})
246250
}
247251

248252
if ($namedModules.Count -gt 0) {
@@ -584,6 +588,72 @@ function ValidateMethod {
584588
return $method
585589
}
586590

591+
function GetClassBasedProperties {
592+
param (
593+
[Parameter(Mandatory = $true)]
594+
[string] $filePath,
595+
596+
[Parameter(Mandatory = $true)]
597+
[string] $className
598+
)
599+
600+
if (".psd1" -notcontains ([System.IO.Path]::GetExtension($filePath))) {
601+
return @('get', 'set', 'test')
602+
}
603+
604+
$module = $filePath.Replace('.psd1', '.psm1')
605+
606+
$properties = [System.Collections.Generic.List[DscResourcePropertyInfo]]::new()
607+
608+
if (Test-Path $module -ErrorAction Ignore) {
609+
[System.Management.Automation.Language.Token[]] $tokens = $null
610+
[System.Management.Automation.Language.ParseError[]] $errors = $null
611+
$ast = [System.Management.Automation.Language.Parser]::ParseFile($module, [ref]$tokens, [ref]$errors)
612+
foreach ($e in $errors) {
613+
$e | Out-String | Write-DscTrace -Operation Error
614+
}
615+
616+
$typeDefinitions = $ast.FindAll(
617+
{
618+
$typeAst = $args[0] -as [System.Management.Automation.Language.TypeDefinitionAst]
619+
return $null -ne $typeAst;
620+
},
621+
$false);
622+
623+
$typeDefinition = $typeDefinitions | Where-Object -Property Name -EQ $className
624+
625+
foreach ($member in $typeDefinition.Members) {
626+
$property = $member -as [System.Management.Automation.Language.PropertyMemberAst]
627+
if (($null -eq $property) -or ($property.IsStatic)) {
628+
continue;
629+
}
630+
$skipProperty = $true
631+
$isKeyProperty = $false
632+
foreach ($attr in $property.Attributes) {
633+
if ($attr.TypeName.Name -eq 'DscProperty') {
634+
$skipProperty = $false
635+
foreach ($attrArg in $attr.NamedArguments) {
636+
if ($attrArg.ArgumentName -eq 'Key') {
637+
$isKeyProperty = $true
638+
break
639+
}
640+
}
641+
}
642+
}
643+
if ($skipProperty) {
644+
continue;
645+
}
646+
647+
[DscResourcePropertyInfo]$prop = [DscResourcePropertyInfo]::new()
648+
$prop.Name = $property.Name
649+
$prop.PropertyType = $property.PropertyType.TypeName.Name
650+
$prop.IsMandatory = $isKeyProperty
651+
$properties.Add($prop)
652+
}
653+
return $properties
654+
}
655+
}
656+
587657
# cached resource
588658
class dscResourceCacheEntry {
589659
[string] $Type
@@ -612,6 +682,13 @@ enum dscResourceType {
612682
Composite
613683
}
614684

685+
class DscResourcePropertyInfo {
686+
[string] $Name
687+
[string] $PropertyType
688+
[bool] $IsMandatory
689+
[System.Collections.Generic.List[string]] $Values
690+
}
691+
615692
# dsc resource type (settable clone)
616693
class DscResourceInfo {
617694
[dscResourceType] $ImplementationDetail

0 commit comments

Comments
 (0)