Skip to content

Commit ad75d30

Browse files
authored
Merge pull request #800 from Gijsreyn/fix-enum-output
Add fix to return enum string values
2 parents 8053bc1 + d2f5205 commit ad75d30

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ enum EnumPropEnumeration {
88
Expected
99
}
1010

11+
enum Ensure {
12+
Present
13+
Absent
14+
}
15+
1116
class BaseTestClass
1217
{
1318
[DscProperty()]
@@ -32,6 +37,9 @@ class TestClassResource : BaseTestClass
3237
[DscProperty()]
3338
[PSCredential] $Credential
3439

40+
[DscProperty()]
41+
[Ensure] $Ensure
42+
3543
[string] $NonDscProperty # This property shouldn't be in results data
3644

3745
hidden

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,40 @@ Describe 'PowerShell adapter resource tests' {
274274
$out | Should -Not -BeNullOrEmpty
275275
$out | Should -BeLike "*ERROR*Credential object 'Credential' requires both 'username' and 'password' properties*"
276276
}
277+
278+
It 'Config get is able to return proper enum value' {
279+
$yaml = @"
280+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
281+
resources:
282+
- name: Class-resource Info
283+
type: TestClassResource/TestClassResource
284+
properties:
285+
Name: 'TestClassResource'
286+
Ensure: 'Present'
287+
"@
288+
289+
$out = dsc config get -i $yaml | ConvertFrom-Json
290+
$LASTEXITCODE | Should -Be 0
291+
$out.results.result.actualState.Ensure | Should -Be 'Present'
292+
}
293+
294+
It 'Config export is able to return proper enum value' {
295+
$yaml = @"
296+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
297+
resources:
298+
- name: Working with class-based resources
299+
type: Microsoft.DSC/PowerShell
300+
properties:
301+
resources:
302+
- name: Class-resource Info
303+
type: TestClassResource/TestClassResource
304+
"@
305+
306+
$out = dsc config export -i $yaml | ConvertFrom-Json
307+
$LASTEXITCODE | Should -Be 0
308+
$out.resources[0].properties.result.count | Should -Be 5
309+
$out.resources[0].properties.result[0].Name | Should -Be "Object1"
310+
$out.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
311+
}
277312
}
313+

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,15 @@ function Invoke-DscOperation {
445445
'Get' {
446446
$Result = @{}
447447
$raw_obj = $dscResourceInstance.Get()
448-
$ValidProperties | ForEach-Object { $Result[$_] = $raw_obj.$_ }
448+
$ValidProperties | ForEach-Object {
449+
if ($raw_obj.$_ -is [System.Enum]) {
450+
$Result[$_] = $raw_obj.$_.ToString()
451+
452+
}
453+
else {
454+
$Result[$_] = $raw_obj.$_
455+
}
456+
}
449457
$addToActualState.properties = $Result
450458
}
451459
'Set' {
@@ -473,7 +481,14 @@ function Invoke-DscOperation {
473481
$raw_obj_array = $method.Invoke($null, $null)
474482
foreach ($raw_obj in $raw_obj_array) {
475483
$Result_obj = @{}
476-
$ValidProperties | ForEach-Object { $Result_obj[$_] = $raw_obj.$_ }
484+
$ValidProperties | ForEach-Object {
485+
if ($raw_obj.$_ -is [System.Enum]) {
486+
$Result_obj[$_] = $raw_obj.$_.ToString()
487+
}
488+
else {
489+
$Result_obj[$_] = $raw_obj.$_
490+
}
491+
}
477492
$resultArray += $Result_obj
478493
}
479494
$addToActualState = $resultArray

0 commit comments

Comments
 (0)