Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions adapters/powershell/.project.data.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"psDscAdapter/powershell.resource.ps1",
"psDscAdapter/psDscAdapter.psd1",
"psDscAdapter/psDscAdapter.psm1",
"powershell.dsc.resource.json"
"powershell.dsc.resource.json",
"PowerShell_adapter.dsc.resource.json"
],
"Windows": [
"psDscAdapter/win_psDscAdapter.psd1",
"psDscAdapter/win_psDscAdapter.psm1",
"windowspowershell.dsc.resource.json"
"windowspowershell.dsc.resource.json",
"WindowsPowerShell_adapter.dsc.resource.json"
]
}
}
117 changes: 117 additions & 0 deletions adapters/powershell/PowerShell_adapter.dsc.resource.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
"type": "Microsoft.Adapter/PowerShell",
"version": "0.1.0",
"kind": "adapter",
"description": "Resource adapter to classic DSC Powershell resources.",
"tags": [
"PowerShell"
],
"adapter": {
"list": {
"executable": "pwsh",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"./psDscAdapter/powershell.resource.ps1",
"List",
"-ResourceType",
"Single"
]
},
"config": "single"
},
"get": {
"executable": "pwsh",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$Input | ./psDscAdapter/powershell.resource.ps1",
"Get",
{
"resourceTypeArg": "-ResourceType"
}
],
"input": "stdin"
},
"set": {
"executable": "pwsh",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$Input | ./psDscAdapter/powershell.resource.ps1",
"Set",
{
"resourceTypeArg": "-ResourceType"
}
],
"input": "stdin",
"implementsPretest": true,
"return": "state"
},
"test": {
"executable": "pwsh",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$Input | ./psDscAdapter/powershell.resource.ps1",
"Test",
{
"resourceTypeArg": "-ResourceType"
}
],
"input": "stdin",
"return": "state"
},
"export": {
"executable": "pwsh",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$Input | ./psDscAdapter/powershell.resource.ps1",
"Export",
{
"resourceTypeArg": "-ResourceType"
}
],
"input": "stdin",
"return": "state"
},
"validate": {
"executable": "pwsh",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$Input | ./psDscAdapter/powershell.resource.ps1 Validate"
],
"input": "stdin"
},
"exitCodes": {
"0": "Success",
"1": "Error"
}
}
29 changes: 21 additions & 8 deletions adapters/powershell/Tests/powershellgroup.config.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -247,40 +247,53 @@ Describe 'PowerShell adapter resource tests' {
$out.results.result.actualState.result.properties.HashTableProp.Name | Should -BeExactly 'DSCv3'
}

It 'Config calling PS Resource directly works for <operation>' -TestCases @(
@{ Operation = 'get' }
@{ Operation = 'set' }
@{ Operation = 'test' }
It 'Config calling PS Resource directly works for <operation> with metadata <metadata> and adapter <adapter>' -TestCases @(
@{ Operation = 'get'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.DSC/PowerShell' }
@{ Operation = 'set'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.DSC/PowerShell' }
@{ Operation = 'test'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.DSC/PowerShell' }
@{ Operation = 'get'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.Adapter/PowerShell' }
@{ Operation = 'set'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.Adapter/PowerShell' }
@{ Operation = 'test'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.Adapter/PowerShell' }
@{ Operation = 'get'; metadata = 'Ignored' }
@{ Operation = 'set'; metadata = 'Ignored' }
@{ Operation = 'test'; metadata = 'Ignored' }
) {
param($Operation)
param($Operation, $metadata, $adapter)

$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
metadata:
${metadata}:
requireAdapter: $adapter
properties:
Name: 'TestClassResource1'
HashTableProp:
Name: 'DSCv3'
Prop1: foo
"@

$out = dsc -l trace config $operation -i $yaml 2> $TestDrive/tracing.txt
$text = $out | Out-String
$out = $out | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw -Path $TestDrive/tracing.txt)
switch ($Operation) {
'get' {
$out.results[0].result.actualState.Name | Should -BeExactly 'TestClassResource1' -Because $text
$out.results[0].result.actualState.Name | Should -BeExactly 'TestClassResource1' -Because ("$text`n" + (Get-Content -Raw -Path $TestDrive/tracing.txt))
}
'set' {
$out.results[0].result.beforeState.Name | Should -BeExactly 'TestClassResource1' -Because $text
$out.results[0].result.afterState.Name | Should -BeExactly 'TestClassResource1' -Because $text
}
'test' {
$out.results[0].result.actualState.InDesiredState | Should -BeFalse -Because $text
$out.results[0].result.inDesiredState | Should -BeFalse -Because $text
}
}
if ($metadata -eq 'Microsoft.DSC') {
"$TestDrive/tracing.txt" | Should -FileContentMatch "Invoking $Operation for '$adapter'" -Because (Get-Content -Raw -Path $TestDrive/tracing.txt)

}
}

It 'Config works with credential object' {
Expand Down
8 changes: 1 addition & 7 deletions adapters/powershell/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,7 @@ Describe 'PowerShell adapter resource tests' {
$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource test -r 'TestClassResource/TestClassResource' -f -
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.InDesiredState | Should -Be $True
$res.actualState.InDesiredState.GetType().Name | Should -Be "Boolean"

# verify that only properties with DscProperty attribute are returned
$propertiesNames = $res.actualState.InDesiredState | Get-Member -MemberType NoteProperty | % Name
$propertiesNames | Should -Not -Contain 'NonDscProperty'
$propertiesNames | Should -Not -Contain 'HiddenNonDscProperty'
$res.InDesiredState | Should -Be $True -Because $r
}

It 'Set works on class-based resource' {
Expand Down
53 changes: 52 additions & 1 deletion adapters/powershell/Tests/win_powershellgroup.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,62 @@ resources:
}

It 'Export works with class-based PS DSC resources' {
$out = dsc resource export -r PSClassResource/PSClassResource 2> "$testdrive/error.log" | ConvertFrom-Json
$out = dsc -l trace resource export -r PSClassResource/PSClassResource 2> "$testdrive/error.log" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path "$testdrive/error.log" -Raw | Out-String)
$out | Should -Not -BeNullOrEmpty
$out.resources.count | Should -Be 5
$out.resources[0].properties.Ensure | Should -Be 'Present' # Check for enum property
}

It 'Config calling PS Resource directly works for <operation> with metadata <metadata> and adapter <adapter>' -TestCases @(
@{ Operation = 'get'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.Windows/WindowsPowerShell' }
@{ Operation = 'set'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.Windows/WindowsPowerShell' }
@{ Operation = 'test'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.Windows/WindowsPowerShell' }
@{ Operation = 'get'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.Adapter/WindowsPowerShell' }
@{ Operation = 'set'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.Adapter/WindowsPowerShell' }
@{ Operation = 'test'; metadata = 'Microsoft.DSC'; adapter = 'Microsoft.Adapter/WindowsPowerShell' }
@{ Operation = 'get'; metadata = 'Ignored' }
@{ Operation = 'set'; metadata = 'Ignored' }
@{ Operation = 'test'; metadata = 'Ignored' }
) {
param($Operation, $metadata, $adapter)

$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Class-resource Info
type: PSClassResource/PSClassResource
metadata:
${metadata}:
requireAdapter: $adapter
properties:
Name: TestInstance
Credential:
UserName: 'MyUser'
Password: 'MyPassword'
"@
$out = dsc -l trace config $operation -i $yaml 2> $TestDrive/tracing.txt
$text = $out | Out-String
$out = $out | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw -Path $TestDrive/tracing.txt)
switch ($Operation) {
'get' {
$out.results[0].result.actualState.Name | Should -BeExactly 'TestInstance' -Because ("$text`n" + (Get-Content -Raw -Path $TestDrive/tracing.txt))
}
'set' {
$out.results[0].result.beforeState.Name | Should -BeExactly 'TestInstance' -Because $text
if ($adapter -eq 'Microsoft.Adapter/WindowsPowerShell') {
$out.results[0].result.afterState.Name | Should -BeExactly 'TestInstance' -Because $text
}
}
'test' {
$out.results[0].result.inDesiredState | Should -BeTrue -Because $text
}
}
if ($metadata -eq 'Microsoft.DSC') {
"$TestDrive/tracing.txt" | Should -FileContentMatch "Invoking $Operation for '$adapter'" -Because (Get-Content -Raw -Path $TestDrive/tracing.txt)

}
}
}

117 changes: 117 additions & 0 deletions adapters/powershell/WindowsPowerShell_adapter.dsc.resource.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
"type": "Microsoft.Adapter/WindowsPowerShell",
"version": "0.1.0",
"kind": "adapter",
"description": "Resource adapter to classic DSC Powershell resources in Windows PowerShell.",
"tags": [
"PowerShell"
],
"adapter": {
"list": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"./psDscAdapter/powershell.resource.ps1",
"List",
"-ResourceType",
"Single"
]
},
"config": "single"
},
"get": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$Input | ./psDscAdapter/powershell.resource.ps1",
"Get",
{
"resourceTypeArg": "-ResourceType"
}
],
"input": "stdin"
},
"set": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$Input | ./psDscAdapter/powershell.resource.ps1",
"Set",
{
"resourceTypeArg": "-ResourceType"
}
],
"input": "stdin",
"implementsPretest": true,
"return": "state"
},
"test": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$Input | ./psDscAdapter/powershell.resource.ps1",
"Test",
{
"resourceTypeArg": "-ResourceType"
}
],
"input": "stdin",
"return": "state"
},
"export": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$Input | ./psDscAdapter/powershell.resource.ps1",
"Export",
{
"resourceTypeArg": "-ResourceType"
}
],
"input": "stdin",
"return": "state"
},
"validate": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-Command",
"$Input | ./psDscAdapter/powershell.resource.ps1 Validate"
],
"input": "stdin"
},
"exitCodes": {
"0": "Success",
"1": "Error"
}
}
Loading
Loading