Skip to content

Commit bb0fefd

Browse files
author
Andrew
committed
Added get-set-test for class-based
1 parent d75cc5c commit bb0fefd

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

powershell-adapter/powershell.resource.ps1

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ switch ($Operation) {
7979
} | ConvertTo-Json -Compress
8080
}
8181
}
82-
'Get' {
82+
{ @('Get','Set','Test') -contains $_ } {
8383
$desiredState = $psDscAdapter.invoke( { param($jsonInput) Get-DscResourceObject -jsonInput $jsonInput }, $jsonInput )
8484
if ($null -eq $desiredState) {
8585
$trace = @{'Debug' = 'ERROR: Failed to create configuration object from provided input JSON.' } | ConvertTo-Json -Compress
@@ -104,7 +104,7 @@ switch ($Operation) {
104104

105105
foreach ($ds in $desiredState) {
106106
# process the INPUT (desiredState) for each resource as dscresourceInfo and return the OUTPUT as actualState
107-
$actualState = $psDscAdapter.invoke( { param($ds, $dscResourceCache) Get-ActualState -DesiredState $ds -dscResourceCache $dscResourceCache }, $ds, $dscResourceCache)
107+
$actualState = $psDscAdapter.invoke( { param($op, $ds, $dscResourceCache) Invoke-DscOperation -Operation $op -DesiredState $ds -dscResourceCache $dscResourceCache }, $Operation, $ds, $dscResourceCache)
108108
if ($null -eq $actualState) {
109109
$trace = @{'Debug' = 'ERROR: Incomplete GET for resource ' + $ds.Name } | ConvertTo-Json -Compress
110110
$host.ui.WriteErrorLine($trace)
@@ -119,20 +119,6 @@ switch ($Operation) {
119119
$host.ui.WriteErrorLine($trace)
120120
return $result
121121
}
122-
'Set' {
123-
throw 'SET not implemented'
124-
125-
# OUTPUT
126-
$result += @{}
127-
@{ result = $result } | ConvertTo-Json -Depth 10 -Compress
128-
}
129-
'Test' {
130-
throw 'TEST not implemented'
131-
132-
# OUTPUT
133-
$result += @{}
134-
@{ result = $result } | ConvertTo-Json -Depth 10 -Compress
135-
}
136122
'Export' {
137123
throw 'EXPORT not implemented'
138124

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function Invoke-DscCacheRefresh {
133133
return $dscResourceCache
134134
}
135135

136-
# Convert the INPUT to a dscResourceObject object so configuration and resource are standardized as moch as possible
136+
# Convert the INPUT to a dscResourceObject object so configuration and resource are standardized as much as possible
137137
function Get-DscResourceObject {
138138
param(
139139
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
@@ -181,8 +181,11 @@ function Get-DscResourceObject {
181181
}
182182

183183
# Get the actual state using DSC Get method from any type of DSC resource
184-
function Get-ActualState {
184+
function Invoke-DscOperation {
185185
param(
186+
[Parameter(Mandatory)]
187+
[ValidateSet('Get', 'Set', 'Test')]
188+
[string]$Operation,
186189
[Parameter(Mandatory, ValueFromPipeline = $true)]
187190
[dscResourceObject]$DesiredState,
188191
[Parameter(Mandatory)]
@@ -273,10 +276,20 @@ function Get-ActualState {
273276
$DesiredState.properties.psobject.properties | ForEach-Object -Process {
274277
$dscResourceInstance.$($_.Name) = $_.Value
275278
}
276-
$getResult = $dscResourceInstance.Get()
277279

278-
# set the properties of the OUTPUT object from the result of Get-TargetResource
279-
$addToActualState.properties = $getResult
280+
switch ($Operation) {
281+
'Get' {
282+
$Result = $dscResourceInstance.Get()
283+
$addToActualState.properties = $Result
284+
}
285+
'Set' {
286+
$dscResourceInstance.Set()
287+
}
288+
'Test' {
289+
$Result = $dscResourceInstance.Test()
290+
$addToActualState.properties = [psobject]@{'InDesiredState'=$Result}
291+
}
292+
}
280293
}
281294
catch {
282295

0 commit comments

Comments
 (0)