Skip to content
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

Class-based PowerShell DSC Resources should not include hidden properties #556

Merged
merged 6 commits into from
Sep 20, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ class TestClassResource : BaseTestClass
[DscProperty()]
[string] $EnumProp

[string] $NonDscProperty # This property shouldn't be in results data

hidden
[string] $HiddenNonDscProperty # This property shouldn't be in results data

hidden
[DscProperty()]
[string] $HiddenDscProperty # This property should be in results data, but is an anti-pattern.

[void] Set()
{
}
Expand Down
17 changes: 17 additions & 0 deletions powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ Describe 'PowerShell adapter resource tests' {
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.Prop1 | Should -BeExactly 'ValueForProp1'

# verify that only properties with DscProperty attribute are returned
$propertiesNames = $res.actualState.result.properties | Get-Member -MemberType NoteProperty | % Name
$propertiesNames | Should -Not -Contain 'NonDscProperty'
$propertiesNames | Should -Not -Contain 'HiddenNonDscProperty'
}

It 'Get uses enum names on class-based resource' {
Expand All @@ -53,6 +58,11 @@ Describe 'PowerShell adapter resource tests' {
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.properties.InDesiredState | Should -Be $True

# verify that only properties with DscProperty attribute are returned
$propertiesNames = $res.actualState.result.properties.InDesiredState | Get-Member -MemberType NoteProperty | % Name
$propertiesNames | Should -Not -Contain 'NonDscProperty'
$propertiesNames | Should -Not -Contain 'HiddenNonDscProperty'
}

It 'Set works on class-based resource' {
Expand All @@ -71,6 +81,13 @@ Describe 'PowerShell adapter resource tests' {
$res.resources[0].properties.result.count | Should -Be 5
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"

# verify that only properties with DscProperty attribute are returned
$res.resources[0].properties.result | %{
$propertiesNames = $_ | Get-Member -MemberType NoteProperty | % Name
$propertiesNames | Should -Not -Contain 'NonDscProperty'
$propertiesNames | Should -Not -Contain 'HiddenNonDscProperty'
}
}

It 'Get --all works on PS class-based resource' {
Expand Down
18 changes: 15 additions & 3 deletions powershell-adapter/psDscAdapter/psDscAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ function Invoke-DscOperation {
$resource = GetTypeInstanceFromModule -modulename $cachedDscResourceInfo.ModuleName -classname $cachedDscResourceInfo.Name
$dscResourceInstance = $resource::New()

$ValidProperties = $cachedDscResourceInfo.Properties.Name

if ($DesiredState.properties) {
# set each property of $dscResourceInstance to the value of the property in the $desiredState INPUT object
$DesiredState.properties.psobject.properties | ForEach-Object -Process {
Expand All @@ -469,14 +471,18 @@ function Invoke-DscOperation {

switch ($Operation) {
'Get' {
$Result = $dscResourceInstance.Get()
$Result = @{}
$raw_obj = $dscResourceInstance.Get()
$ValidProperties | ForEach-Object { $Result[$_] = $raw_obj.$_ }
$addToActualState.properties = $Result
}
'Set' {
$dscResourceInstance.Set()
}
'Test' {
$Result = $dscResourceInstance.Test()
$Result = @{}
$raw_obj = $dscResourceInstance.Test()
$ValidProperties | ForEach-Object { $Result[$_] = $raw_obj.$_ }
$addToActualState.properties = [psobject]@{'InDesiredState'=$Result}
}
'Export' {
Expand All @@ -486,7 +492,13 @@ function Invoke-DscOperation {
"Export method not implemented by resource '$($DesiredState.Type)'" | Write-DscTrace -Operation Error
exit 1
}
$resultArray = $method.Invoke($null,$null)
$resultArray = @()
$raw_obj_array = $method.Invoke($null,$null)
foreach ($raw_obj in $raw_obj_array) {
$Result_obj = @{}
$ValidProperties | ForEach-Object { $Result_obj[$_] = $raw_obj.$_ }
$resultArray += $Result_obj
}
$addToActualState = $resultArray
}
}
Expand Down
Loading