Skip to content

Commit 390b431

Browse files
committed
Implement export in WinPS adapter
1 parent 37777fc commit 390b431

File tree

6 files changed

+147
-86
lines changed

6 files changed

+147
-86
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Rust
22

33
on:
44
push:
5-
branches: [ "main" ]
5+
branches: [ "*" ]
66
pull_request:
77
branches: [ "main" ]
88
paths-ignore:

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/psDscAdapter.psm1

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -464,19 +464,7 @@ function Invoke-DscOperation {
464464
$addToActualState.properties = [psobject]@{'InDesiredState' = $Result }
465465
}
466466
'Export' {
467-
$t = $dscResourceInstance.GetType()
468-
$methods = $t.GetMethods() | Where-Object { $_.Name -eq 'Export' }
469-
$method = foreach ($mt in $methods) {
470-
if ($mt.GetParameters().Count -eq 0) {
471-
$mt
472-
break
473-
}
474-
}
475-
476-
if ($null -eq $method) {
477-
"Export method not implemented by resource '$($DesiredState.Type)'" | Write-DscTrace -Operation Error
478-
exit 1
479-
}
467+
$method = ValidateMethod -operation $Operation -class $dscResourceInstance
480468
$resultArray = @()
481469
$raw_obj_array = $method.Invoke($null, $null)
482470
foreach ($raw_obj in $raw_obj_array) {

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,33 @@ function GetTypeInstanceFromModule {
532532
return $instance
533533
}
534534

535+
# ValidateMethod checks if the specified method exists in the class
536+
function ValidateMethod {
537+
param (
538+
[Parameter(Mandatory = $true)]
539+
[ValidateSet('Export', 'WhatIf')]
540+
[string] $operation,
541+
[Parameter(Mandatory = $true)]
542+
[object] $class
543+
)
544+
545+
$t = $class.GetType()
546+
$methods = $t.GetMethods() | Where-Object -Property Name -EQ $operation
547+
$method = foreach ($mt in $methods) {
548+
if ($mt.GetParameters().Count -eq 0) {
549+
$mt
550+
break
551+
}
552+
}
553+
554+
if ($null -eq $method) {
555+
"Method '$operation' not implemented by resource '$($t.Name)'" | Write-DscTrace -Operation Error
556+
exit 1
557+
}
558+
559+
return $method
560+
}
561+
535562
# cached resource
536563
class dscResourceCacheEntry {
537564
[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+
}

test.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
2+
resources:
3+
- name: Class-resource Info
4+
type: PSClassResource/PSClassResource
5+
# $schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
6+
# resources:
7+
# - name: Class-resource Info
8+
# type: TestClassResource/TestClassResource

0 commit comments

Comments
 (0)