Skip to content

Commit 6f1aaf4

Browse files
author
Andrew
committed
re-enabled Export
1 parent baea4a5 commit 6f1aaf4

File tree

4 files changed

+47
-43
lines changed

4 files changed

+47
-43
lines changed

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

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Describe 'PowerShell adapter resource tests' {
5656
}
5757

5858

59-
<#It 'Export works on config with class-based resources' -Skip:(!$IsWindows){
59+
It 'Export works on config with class-based resources' -Skip:(!$IsWindows){
6060

6161
$yaml = @'
6262
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
@@ -66,51 +66,47 @@ Describe 'PowerShell adapter resource tests' {
6666
properties:
6767
resources:
6868
- name: Class-resource Info
69-
type: PSTestModule/TestClassResource
69+
type: TestClassResource/TestClassResource
7070
'@
7171
$out = $yaml | dsc config export
7272
$LASTEXITCODE | Should -Be 0
7373
$res = $out | ConvertFrom-Json
7474
$res.'$schema' | Should -BeExactly 'https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json'
7575
$res.'resources' | Should -Not -BeNullOrEmpty
76-
$res.resources.count | Should -Be 5
77-
$res.resources[0].properties.Name | Should -Be "Object1"
78-
$res.resources[0].properties.Prop1 | Should -Be "Property of object1"
76+
$res.resources[0].properties.result.count | Should -Be 5
77+
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
78+
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
7979
}
8080

81-
#>
82-
8381
It 'Custom psmodulepath in config works' -Skip:(!$IsWindows){
8482

8583
$OldPSModulePath = $env:PSModulePath
86-
Copy-Item -Recurse -Force -Path "$PSScriptRoot/PSTestModule" -Destination $TestDrive
87-
Rename-Item -Path "$PSScriptRoot/PSTestModule" -NewName "_PSTestModule"
84+
Copy-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" -Destination $TestDrive
85+
Rename-Item -Path "$PSScriptRoot/TestClassResource" -NewName "_TestClassResource"
8886

8987
try {
9088
$yaml = @"
91-
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
89+
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
90+
resources:
91+
- name: Working with class-based resources
92+
type: Microsoft.DSC/PowerShell
93+
properties:
94+
psmodulepath: `$env:PSModulePath;$TestDrive
9295
resources:
93-
- name: Working with class-based resources
94-
type: Microsoft.DSC/PowerShell
95-
properties:
96-
psmodulepath: `$env:PSModulePath;$TestDrive
97-
resources:
98-
- name: Class-resource Info
99-
type: PSTestModule/TestClassResource
96+
- name: Class-resource Info
97+
type: TestClassResource/TestClassResource
10098
"@
101-
<#
10299
$out = $yaml | dsc config export
103100
$LASTEXITCODE | Should -Be 0
104101
$res = $out | ConvertFrom-Json
105102
$res.'$schema' | Should -BeExactly 'https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json'
106103
$res.'resources' | Should -Not -BeNullOrEmpty
107-
$res.resources.count | Should -Be 5
108-
$res.resources[0].properties.Name | Should -Be "Object1"
109-
$res.resources[0].properties.Prop1 | Should -Be "Property of object1"
110-
#>
104+
$res.resources[0].properties.result.count | Should -Be 5
105+
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
106+
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
111107
}
112108
finally {
113-
Rename-Item -Path "$PSScriptRoot/_PSTestModule" -NewName "PSTestModule"
109+
Rename-Item -Path "$PSScriptRoot/_TestClassResource" -NewName "TestClassResource"
114110
$env:PSModulePath = $OldPSModulePath
115111
}
116112
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,22 @@ Describe 'PowerShell adapter resource tests' {
115115
$res.afterState.result.properties.RebootRequired | Should -Not -BeNull
116116
}
117117

118-
<#It 'Export works on PS class-based resource' -Skip:(!$IsWindows){
118+
It 'Export works on PS class-based resource' -Skip:(!$IsWindows){
119119

120120
$r = dsc resource export -r TestClassResource/TestClassResource
121121
$LASTEXITCODE | Should -Be 0
122122
$res = $r | ConvertFrom-Json
123-
$res.resources.count | Should -Be 5
124-
$res.resources[0].type | Should -Be "TestClassResource/TestClassResource"
125-
$res.resources[0].properties.Name | Should -Be "Object1"
126-
$res.resources[0].properties.Prop1 | Should -Be "Property of object1"
123+
$res.resources[0].properties.result.count | Should -Be 5
124+
$res.resources[0].properties.result[0].Name | Should -Be "Object1"
125+
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
127126
}
128127

129128
It 'Get --all works on PS class-based resource' -Skip:(!$IsWindows){
130129

131130
$r = dsc resource get --all -r TestClassResource/TestClassResource
132131
$LASTEXITCODE | Should -Be 0
133132
$res = $r | ConvertFrom-Json
134-
$res.count | Should -Be 5
135-
$res | % {$_.actualState | Should -Not -BeNullOrEmpty}
133+
$res.actualState.result.count | Should -Be 5
134+
$res.actualState.result| % {$_.Name | Should -Not -BeNullOrEmpty}
136135
}
137-
#>
138136
}

powershell-adapter/powershell.resource.ps1

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ if ('Validate' -ne $Operation) {
2121
$result = [System.Collections.Generic.List[Object]]::new()
2222
}
2323

24+
if ($jsonInput) {
25+
$inputobj_pscustomobj = $jsonInput | ConvertFrom-Json
26+
$new_psmodulepath = $inputobj_pscustomobj.psmodulepath
27+
if ($new_psmodulepath)
28+
{
29+
$env:PSModulePath = $ExecutionContext.InvokeCommand.ExpandString($new_psmodulepath)
30+
}
31+
}
32+
2433
# process the operation requested to the script
2534
switch ($Operation) {
2635
'List' {
@@ -79,7 +88,7 @@ switch ($Operation) {
7988
} | ConvertTo-Json -Compress
8089
}
8190
}
82-
{ @('Get','Set','Test') -contains $_ } {
91+
{ @('Get','Set','Test','Export') -contains $_ } {
8392
$desiredState = $psDscAdapter.invoke( { param($jsonInput) Get-DscResourceObject -jsonInput $jsonInput }, $jsonInput )
8493
if ($null -eq $desiredState) {
8594
$trace = @{'Debug' = 'ERROR: Failed to create configuration object from provided input JSON.' } | ConvertTo-Json -Compress
@@ -119,13 +128,6 @@ switch ($Operation) {
119128
$host.ui.WriteErrorLine($trace)
120129
return $result
121130
}
122-
'Export' {
123-
throw 'EXPORT not implemented'
124-
125-
# OUTPUT
126-
$result += @{}
127-
@{ result = $result } | ConvertTo-Json -Depth 10 -Compress
128-
}
129131
'Validate' {
130132
# VALIDATE not implemented
131133

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function Get-DscResourceObject {
183183
function Invoke-DscOperation {
184184
param(
185185
[Parameter(Mandatory)]
186-
[ValidateSet('Get', 'Set', 'Test')]
186+
[ValidateSet('Get', 'Set', 'Test', 'Export')]
187187
[string]$Operation,
188188
[Parameter(Mandatory, ValueFromPipeline = $true)]
189189
[dscResourceObject]$DesiredState,
@@ -274,9 +274,11 @@ function Invoke-DscOperation {
274274
$resource = GetTypeInstanceFromModule -modulename $cachedDscResourceInfo.ModuleName -classname $cachedDscResourceInfo.Name
275275
$dscResourceInstance = $resource::New()
276276

277-
# set each property of $dscResourceInstance to the value of the property in the $desiredState INPUT object
278-
$DesiredState.properties.psobject.properties | ForEach-Object -Process {
279-
$dscResourceInstance.$($_.Name) = $_.Value
277+
if ($DesiredState.properties) {
278+
# set each property of $dscResourceInstance to the value of the property in the $desiredState INPUT object
279+
$DesiredState.properties.psobject.properties | ForEach-Object -Process {
280+
$dscResourceInstance.$($_.Name) = $_.Value
281+
}
280282
}
281283

282284
switch ($Operation) {
@@ -291,6 +293,12 @@ function Invoke-DscOperation {
291293
$Result = $dscResourceInstance.Test()
292294
$addToActualState.properties = [psobject]@{'InDesiredState'=$Result}
293295
}
296+
'Export' {
297+
$t = $dscResourceInstance.GetType()
298+
$method = $t.GetMethod('Export')
299+
$resultArray = $method.Invoke($null,$null)
300+
$addToActualState = $resultArray
301+
}
294302
}
295303
}
296304
catch {

0 commit comments

Comments
 (0)