Skip to content

Commit fe1a427

Browse files
authored
Merge pull request #482 from anmenaga/rename_type_prop
Renamed field that specifies adapted type
2 parents d208aea + b7afce2 commit fe1a427

File tree

7 files changed

+221
-9
lines changed

7 files changed

+221
-9
lines changed

dsc/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub fn add_fields_to_json(json: &str, fields_to_add: &HashMap<String, String>) -
118118
pub fn add_type_name_to_json(json: String, type_name: String) -> String
119119
{
120120
let mut map:HashMap<String,String> = HashMap::new();
121-
map.insert(String::from("type"), type_name);
121+
map.insert(String::from("adapted_dsc_type"), type_name);
122122

123123
let mut j = json;
124124
if j.is_empty()
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/bundled/resource/manifest.json",
3+
"type": "Test/TestAdapter",
4+
"version": "0.1.0",
5+
"kind": "Adapter",
6+
"description": "Resource adapter for testing.",
7+
"tags": [
8+
"PowerShell"
9+
],
10+
"adapter": {
11+
"list": {
12+
"executable": "pwsh",
13+
"args": [
14+
"-NoLogo",
15+
"-NonInteractive",
16+
"-NoProfile",
17+
"-Command",
18+
"./testadapter.resource.ps1 List"
19+
]
20+
},
21+
"config": "full"
22+
},
23+
"get": {
24+
"executable": "pwsh",
25+
"args": [
26+
"-NoLogo",
27+
"-NonInteractive",
28+
"-NoProfile",
29+
"-Command",
30+
"$Input | ./testadapter.resource.ps1 Get"
31+
],
32+
"input": "stdin"
33+
},
34+
"set": {
35+
"executable": "pwsh",
36+
"args": [
37+
"-NoLogo",
38+
"-NonInteractive",
39+
"-NoProfile",
40+
"-Command",
41+
"$Input | ./testadapter.resource.ps1 Set"
42+
],
43+
"input": "stdin",
44+
"implementsPretest": true,
45+
"return": "state"
46+
},
47+
"test": {
48+
"executable": "pwsh",
49+
"args": [
50+
"-NoLogo",
51+
"-NonInteractive",
52+
"-NoProfile",
53+
"-Command",
54+
"$Input | ./testadapter.resource.ps1 Test"
55+
],
56+
"input": "stdin",
57+
"return": "state"
58+
},
59+
"export": {
60+
"executable": "pwsh",
61+
"args": [
62+
"-NoLogo",
63+
"-NonInteractive",
64+
"-NoProfile",
65+
"-Command",
66+
"$Input | ./testadapter.resource.ps1 Export"
67+
],
68+
"input": "stdin",
69+
"return": "state"
70+
},
71+
"validate": {
72+
"executable": "pwsh",
73+
"args": [
74+
"-NoLogo",
75+
"-NonInteractive",
76+
"-NoProfile",
77+
"-Command",
78+
"$Input | ./testadapter.resource.ps1 Validate"
79+
],
80+
"input": "stdin"
81+
},
82+
"exitCodes": {
83+
"0": "Success",
84+
"1": "Error"
85+
}
86+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
[CmdletBinding()]
4+
param(
5+
[Parameter(Mandatory = $true, Position = 0, HelpMessage = 'Operation to perform. Choose from List, Get, Set, Test, Export, Validate.')]
6+
[ValidateSet('List', 'Get', 'Set', 'Test', 'Export', 'Validate')]
7+
[string]$Operation,
8+
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true, HelpMessage = 'Configuration or resource input in JSON format.')]
9+
[string]$jsonInput = '@{}'
10+
)
11+
12+
function Write-DscTrace {
13+
param(
14+
[Parameter(Mandatory = $false)]
15+
[ValidateSet('Error', 'Warn', 'Info', 'Debug', 'Trace')]
16+
[string]$Operation = 'Debug',
17+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
18+
[string]$Message
19+
)
20+
21+
$trace = @{$Operation = $Message } | ConvertTo-Json -Compress
22+
$host.ui.WriteErrorLine($trace)
23+
}
24+
25+
'Hello from TestAdapter.' | Write-DscTrace
26+
'PSPath=' + $PSHome | Write-DscTrace
27+
'PSModulePath=' + $env:PSModulePath | Write-DscTrace
28+
29+
if ($jsonInput -ne '@{}') {
30+
$inputobj = $jsonInput | ConvertFrom-Json
31+
}
32+
33+
$jsonInput | Write-DscTrace
34+
35+
switch ($Operation) {
36+
'List' {
37+
@{
38+
type = "Test/TestCase"
39+
kind = 'Resource'
40+
version = '1'
41+
capabilities = @('Get', 'Set', 'Test', 'Export')
42+
path = $PSScriptRoot
43+
directory = Split-Path $PSScriptRoot
44+
implementedAs = 'Adapter'
45+
author = 'Test'
46+
properties = @('TestCaseId', 'Input', 'Result')
47+
requireAdapter = 'Test/TestAdapter'
48+
description = 'TestCase resource'
49+
} | ConvertTo-Json -Compress
50+
}
51+
{ @('Get','Set','Test','Export') -contains $_ } {
52+
53+
# TestCase 1 = 'Verify adapted_dsc_type field'
54+
if (($inputobj.TestCaseId -eq 1 ) -or ($_ -eq 'Export')){
55+
$result = $inputobj.adapted_dsc_type -eq 'Test/TestCase'
56+
$result = @{'TestCaseId'=1; 'Input'=''; result = $result } | ConvertTo-Json -Depth 10 -Compress
57+
return $result
58+
}
59+
60+
}
61+
'Validate' {
62+
@{ valid = $true } | ConvertTo-Json
63+
}
64+
}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,69 @@ Describe 'PowerShell adapter resource tests' {
128128
$t = $resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'}
129129
$t.properties | Should -Contain "BaseProperty"
130130
}
131+
132+
It 'Verify adapted_dsc_type field in Get' {
133+
$oldPath = $env:PATH
134+
try {
135+
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
136+
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath
137+
138+
$r = '{TestCaseId: 1}'| dsc resource get -r 'Test/TestCase'
139+
$LASTEXITCODE | Should -Be 0
140+
$resources = $r | ConvertFrom-Json
141+
$resources.actualState.result | Should -Be $True
142+
}
143+
finally {
144+
$env:PATH = $oldPath
145+
}
146+
}
147+
148+
It 'Verify adapted_dsc_type field in Set' {
149+
$oldPath = $env:PATH
150+
try {
151+
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
152+
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath
153+
154+
$r = '{TestCaseId: 1}'| dsc resource set -r 'Test/TestCase'
155+
$LASTEXITCODE | Should -Be 0
156+
$resources = $r | ConvertFrom-Json
157+
$resources.beforeState.result | Should -Be $True
158+
$resources.afterState.result | Should -Be $True
159+
}
160+
finally {
161+
$env:PATH = $oldPath
162+
}
163+
}
164+
165+
It 'Verify adapted_dsc_type field in Test' {
166+
$oldPath = $env:PATH
167+
try {
168+
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
169+
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath
170+
171+
$r = '{TestCaseId: 1}'| dsc resource test -r 'Test/TestCase'
172+
$LASTEXITCODE | Should -Be 0
173+
$resources = $r | ConvertFrom-Json
174+
$resources.actualState.result | Should -Be $True
175+
}
176+
finally {
177+
$env:PATH = $oldPath
178+
}
179+
}
180+
181+
It 'Verify adapted_dsc_type field in Export' {
182+
$oldPath = $env:PATH
183+
try {
184+
$adapterPath = Join-Path $PSScriptRoot 'TestAdapter'
185+
$env:PATH += [System.IO.Path]::PathSeparator + $adapterPath
186+
187+
$r = dsc resource export -r 'Test/TestCase'
188+
$LASTEXITCODE | Should -Be 0
189+
$resources = $r | ConvertFrom-Json
190+
$resources.resources[0].properties.result | Should -Be $True
191+
}
192+
finally {
193+
$env:PATH = $oldPath
194+
}
195+
}
131196
}

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ function Get-DscResourceObject {
379379
}
380380
else {
381381
# mimic a config object with a single resource
382-
$type = $inputObj.type
383-
$inputObj.psobject.properties.Remove('type')
382+
$type = $inputObj.adapted_dsc_type
383+
$inputObj.psobject.properties.Remove('adapted_dsc_type')
384384
$desiredState += [dscResourceObject]@{
385385
name = $adapterName
386386
type = $type
@@ -408,9 +408,6 @@ function Invoke-DscOperation {
408408
$psVersion = $PSVersionTable.PSVersion.ToString()
409409
'PowerShell version: ' + $psVersion | Write-DscTrace
410410

411-
$moduleVersion = Get-Module PSDesiredStateConfiguration | ForEach-Object Version
412-
'PSDesiredStateConfiguration module version: ' + $moduleVersion | Write-DscTrace
413-
414411
# get details from cache about the DSC resource, if it exists
415412
$cachedDscResourceInfo = $dscResourceCache | Where-Object Type -EQ $DesiredState.type | ForEach-Object DscResourceInfo
416413

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ function Get-DscResourceObject {
276276
}
277277
else {
278278
# mimic a config object with a single resource
279-
$type = $inputObj.type
280-
$inputObj.psobject.properties.Remove('type')
279+
$type = $inputObj.adapted_dsc_type
280+
$inputObj.psobject.properties.Remove('adapted_dsc_type')
281281
$desiredState += [dscResourceObject]@{
282282
name = $adapterName
283283
type = $type

wmi-adapter/wmi.resource.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ elseif ($Operation -eq 'Get')
105105
}
106106
else # we are processing an individual resource call
107107
{
108-
$type_fields = $inputobj_pscustomobj.type -split "/"
108+
$type_fields = $inputobj_pscustomobj.adapted_dsc_type -split "/"
109109
$wmi_namespace = $type_fields[0].Replace('.','\')
110110
$wmi_classname = $type_fields[1]
111111

0 commit comments

Comments
 (0)