Skip to content

Commit 5b46548

Browse files
authored
Merge pull request #556 from anmenaga/issue_157
Class-based PowerShell DSC Resources should not include hidden properties
2 parents 2539e64 + a9ed999 commit 5b46548

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ class TestClassResource : BaseTestClass
2626
[DscProperty()]
2727
[string] $EnumProp
2828

29+
[string] $NonDscProperty # This property shouldn't be in results data
30+
31+
hidden
32+
[string] $HiddenNonDscProperty # This property shouldn't be in results data
33+
34+
hidden
35+
[DscProperty()]
36+
[string] $HiddenDscProperty # This property should be in results data, but is an anti-pattern.
37+
2938
[void] Set()
3039
{
3140
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ Describe 'PowerShell adapter resource tests' {
3737
$LASTEXITCODE | Should -Be 0
3838
$res = $r | ConvertFrom-Json
3939
$res.actualState.result.properties.Prop1 | Should -BeExactly 'ValueForProp1'
40+
41+
# verify that only properties with DscProperty attribute are returned
42+
$propertiesNames = $res.actualState.result.properties | Get-Member -MemberType NoteProperty | % Name
43+
$propertiesNames | Should -Not -Contain 'NonDscProperty'
44+
$propertiesNames | Should -Not -Contain 'HiddenNonDscProperty'
4045
}
4146

4247
It 'Get uses enum names on class-based resource' {
@@ -53,6 +58,11 @@ Describe 'PowerShell adapter resource tests' {
5358
$LASTEXITCODE | Should -Be 0
5459
$res = $r | ConvertFrom-Json
5560
$res.actualState.result.properties.InDesiredState | Should -Be $True
61+
62+
# verify that only properties with DscProperty attribute are returned
63+
$propertiesNames = $res.actualState.result.properties.InDesiredState | Get-Member -MemberType NoteProperty | % Name
64+
$propertiesNames | Should -Not -Contain 'NonDscProperty'
65+
$propertiesNames | Should -Not -Contain 'HiddenNonDscProperty'
5666
}
5767

5868
It 'Set works on class-based resource' {
@@ -71,6 +81,13 @@ Describe 'PowerShell adapter resource tests' {
7181
$res.resources[0].properties.result.count | Should -Be 5
7282
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
7383
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
84+
85+
# verify that only properties with DscProperty attribute are returned
86+
$res.resources[0].properties.result | %{
87+
$propertiesNames = $_ | Get-Member -MemberType NoteProperty | % Name
88+
$propertiesNames | Should -Not -Contain 'NonDscProperty'
89+
$propertiesNames | Should -Not -Contain 'HiddenNonDscProperty'
90+
}
7491
}
7592

7693
It 'Get --all works on PS class-based resource' {

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ function Invoke-DscOperation {
460460
$resource = GetTypeInstanceFromModule -modulename $cachedDscResourceInfo.ModuleName -classname $cachedDscResourceInfo.Name
461461
$dscResourceInstance = $resource::New()
462462

463+
$ValidProperties = $cachedDscResourceInfo.Properties.Name
464+
463465
if ($DesiredState.properties) {
464466
# set each property of $dscResourceInstance to the value of the property in the $desiredState INPUT object
465467
$DesiredState.properties.psobject.properties | ForEach-Object -Process {
@@ -469,14 +471,18 @@ function Invoke-DscOperation {
469471

470472
switch ($Operation) {
471473
'Get' {
472-
$Result = $dscResourceInstance.Get()
474+
$Result = @{}
475+
$raw_obj = $dscResourceInstance.Get()
476+
$ValidProperties | ForEach-Object { $Result[$_] = $raw_obj.$_ }
473477
$addToActualState.properties = $Result
474478
}
475479
'Set' {
476480
$dscResourceInstance.Set()
477481
}
478482
'Test' {
479-
$Result = $dscResourceInstance.Test()
483+
$Result = @{}
484+
$raw_obj = $dscResourceInstance.Test()
485+
$ValidProperties | ForEach-Object { $Result[$_] = $raw_obj.$_ }
480486
$addToActualState.properties = [psobject]@{'InDesiredState'=$Result}
481487
}
482488
'Export' {
@@ -486,7 +492,13 @@ function Invoke-DscOperation {
486492
"Export method not implemented by resource '$($DesiredState.Type)'" | Write-DscTrace -Operation Error
487493
exit 1
488494
}
489-
$resultArray = $method.Invoke($null,$null)
495+
$resultArray = @()
496+
$raw_obj_array = $method.Invoke($null,$null)
497+
foreach ($raw_obj in $raw_obj_array) {
498+
$Result_obj = @{}
499+
$ValidProperties | ForEach-Object { $Result_obj[$_] = $raw_obj.$_ }
500+
$resultArray += $Result_obj
501+
}
490502
$addToActualState = $resultArray
491503
}
492504
}

0 commit comments

Comments
 (0)