Skip to content

Commit 04dc007

Browse files
authored
Merge pull request #848 from Gijsreyn/implement-export-winpsadapter
Implement export Windows PowerShell adapter
2 parents 8a7c3a3 + 583f691 commit 04dc007

File tree

3 files changed

+152
-75
lines changed

3 files changed

+152
-75
lines changed

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,22 @@ class PSClassResource {
322322
[void] Set() {
323323
324324
}
325+
326+
static [PSClassResource[]] Export()
327+
{
328+
$resultList = [System.Collections.Generic.List[PSClassResource]]::new()
329+
$resultCount = 5
330+
if ($env:PSClassResourceResultCount) {
331+
$resultCount = $env:PSClassResourceResultCount
332+
}
333+
1..$resultCount | %{
334+
$obj = New-Object PSClassResource
335+
$obj.Name = "Object$_"
336+
$resultList.Add($obj)
337+
}
338+
339+
return $resultList.ToArray()
340+
}
325341
}
326342
'@
327343

@@ -350,4 +366,12 @@ class PSClassResource {
350366
$LASTEXITCODE | Should -Be 0
351367
$out.afterstate.InDesiredState | Should -Be $true
352368
}
369+
370+
It 'Export works with class-based PS DSC resources' -Skip:(!$IsWindows) {
371+
372+
$out = dsc resource export -r PSClassResource/PSClassResource | ConvertFrom-Json
373+
$LASTEXITCODE | Should -Be 0
374+
$out | Should -Not -BeNullOrEmpty
375+
$out.resources.count | Should -Be 5
376+
}
353377
}

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,21 @@ function Invoke-DscOperation {
438438
$addToActualState.properties = [psobject]@{'InDesiredState' = $Result }
439439
}
440440
'Export' {
441-
$t = $dscResourceInstance.GetType()
442-
$method = $t.GetMethod('Export')
443-
$resultArray = $method.Invoke($null, $null)
441+
$method = ValidateMethod -operation $Operation -class $dscResourceInstance
442+
$resultArray = @()
443+
$raw_obj_array = $method.Invoke($null, $null)
444+
foreach ($raw_obj in $raw_obj_array) {
445+
$Result_obj = @{}
446+
$ValidProperties | ForEach-Object {
447+
if ($raw_obj.$_ -is [System.Enum]) {
448+
$Result_obj[$_] = $raw_obj.$_.ToString()
449+
}
450+
else {
451+
$Result_obj[$_] = $raw_obj.$_
452+
}
453+
}
454+
$resultArray += $Result_obj
455+
}
444456
$addToActualState = $resultArray
445457
}
446458
}
@@ -532,6 +544,33 @@ function GetTypeInstanceFromModule {
532544
return $instance
533545
}
534546

547+
# ValidateMethod checks if the specified method exists in the class
548+
function ValidateMethod {
549+
param (
550+
[Parameter(Mandatory = $true)]
551+
[ValidateSet('Export', 'WhatIf')]
552+
[string] $operation,
553+
[Parameter(Mandatory = $true)]
554+
[object] $class
555+
)
556+
557+
$t = $class.GetType()
558+
$methods = $t.GetMethods() | Where-Object -Property Name -EQ $operation
559+
$method = foreach ($mt in $methods) {
560+
if ($mt.GetParameters().Count -eq 0) {
561+
$mt
562+
break
563+
}
564+
}
565+
566+
if ($null -eq $method) {
567+
"Method '$operation' not implemented by resource '$($t.Name)'" | Write-DscTrace -Operation Error
568+
exit 1
569+
}
570+
571+
return $method
572+
}
573+
535574
# cached resource
536575
class dscResourceCacheEntry {
537576
[string] $Type
Lines changed: 86 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,14 @@
11
{
2-
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
3-
"type": "Microsoft.Windows/WindowsPowerShell",
4-
"version": "0.1.0",
5-
"kind": "adapter",
6-
"description": "Resource adapter to classic DSC Powershell resources in Windows PowerShell.",
7-
"tags": [
8-
"PowerShell"
9-
],
10-
"adapter": {
11-
"list": {
12-
"executable": "powershell",
13-
"args": [
14-
"-NoLogo",
15-
"-NonInteractive",
16-
"-NoProfile",
17-
"-ExecutionPolicy",
18-
"Bypass",
19-
"-Command",
20-
"./psDscAdapter/powershell.resource.ps1 List"
21-
]
22-
},
23-
"config": "full"
24-
},
25-
"get": {
26-
"executable": "powershell",
27-
"args": [
28-
"-NoLogo",
29-
"-NonInteractive",
30-
"-NoProfile",
31-
"-ExecutionPolicy",
32-
"Bypass",
33-
"-Command",
34-
"$Input | ./psDscAdapter/powershell.resource.ps1 Get"
35-
],
36-
"input": "stdin"
37-
},
38-
"set": {
2+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
3+
"type": "Microsoft.Windows/WindowsPowerShell",
4+
"version": "0.1.0",
5+
"kind": "adapter",
6+
"description": "Resource adapter to classic DSC Powershell resources in Windows PowerShell.",
7+
"tags": [
8+
"PowerShell"
9+
],
10+
"adapter": {
11+
"list": {
3912
"executable": "powershell",
4013
"args": [
4114
"-NoLogo",
@@ -44,39 +17,80 @@
4417
"-ExecutionPolicy",
4518
"Bypass",
4619
"-Command",
47-
"$Input | ./psDscAdapter/powershell.resource.ps1 Set"
48-
],
49-
"input": "stdin",
50-
"preTest": true
51-
},
52-
"test": {
53-
"executable": "powershell",
54-
"args": [
55-
"-NoLogo",
56-
"-NonInteractive",
57-
"-NoProfile",
58-
"-ExecutionPolicy",
59-
"Bypass",
60-
"-Command",
61-
"$Input | ./psDscAdapter/powershell.resource.ps1 Test"
62-
],
63-
"input": "stdin",
64-
"return": "state"
65-
},
66-
"validate": {
67-
"executable": "powershell",
68-
"args": [
69-
"-NoLogo",
70-
"-NonInteractive",
71-
"-NoProfile",
72-
"-ExecutionPolicy",
73-
"Bypass",
74-
"-Command",
75-
"$Input | ./psDscAdapter/powershell.resource.ps1 Validate"
76-
]
77-
},
78-
"exitCodes": {
79-
"0": "Success",
80-
"1": "Error"
81-
}
20+
"./psDscAdapter/powershell.resource.ps1 List"
21+
]
22+
},
23+
"config": "full"
24+
},
25+
"get": {
26+
"executable": "powershell",
27+
"args": [
28+
"-NoLogo",
29+
"-NonInteractive",
30+
"-NoProfile",
31+
"-ExecutionPolicy",
32+
"Bypass",
33+
"-Command",
34+
"$Input | ./psDscAdapter/powershell.resource.ps1 Get"
35+
],
36+
"input": "stdin"
37+
},
38+
"set": {
39+
"executable": "powershell",
40+
"args": [
41+
"-NoLogo",
42+
"-NonInteractive",
43+
"-NoProfile",
44+
"-ExecutionPolicy",
45+
"Bypass",
46+
"-Command",
47+
"$Input | ./psDscAdapter/powershell.resource.ps1 Set"
48+
],
49+
"input": "stdin",
50+
"preTest": true
51+
},
52+
"test": {
53+
"executable": "powershell",
54+
"args": [
55+
"-NoLogo",
56+
"-NonInteractive",
57+
"-NoProfile",
58+
"-ExecutionPolicy",
59+
"Bypass",
60+
"-Command",
61+
"$Input | ./psDscAdapter/powershell.resource.ps1 Test"
62+
],
63+
"input": "stdin",
64+
"return": "state"
65+
},
66+
"export": {
67+
"executable": "powershell",
68+
"args": [
69+
"-NoLogo",
70+
"-NonInteractive",
71+
"-NoProfile",
72+
"-ExecutionPolicy",
73+
"Bypass",
74+
"-Command",
75+
"$Input | ./psDscAdapter/powershell.resource.ps1 Export"
76+
],
77+
"input": "stdin",
78+
"return": "state"
79+
},
80+
"validate": {
81+
"executable": "powershell",
82+
"args": [
83+
"-NoLogo",
84+
"-NonInteractive",
85+
"-NoProfile",
86+
"-ExecutionPolicy",
87+
"Bypass",
88+
"-Command",
89+
"$Input | ./psDscAdapter/powershell.resource.ps1 Validate"
90+
]
91+
},
92+
"exitCodes": {
93+
"0": "Success",
94+
"1": "Error"
8295
}
96+
}

0 commit comments

Comments
 (0)