Skip to content

Commit 739a66b

Browse files
authored
Merge branch 'main' into cfs
2 parents 04faa65 + b0e6ed9 commit 739a66b

15 files changed

+383
-116
lines changed

dsc/tests/dsc_whatif.tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ Describe 'whatif tests' {
4141
$what_if_result.results.result.beforeState._exist | Should -Be $set_result.results.result.beforeState._exist
4242
$what_if_result.results.result.beforeState.keyPath | Should -Be $set_result.results.result.beforeState.keyPath
4343
$what_if_result.results.result.afterState.KeyPath | Should -Be $set_result.results.result.afterState.keyPath
44-
$what_if_result.results.result.changedProperties | Should -Be $set_result.results.result.changedProperties
44+
# can be changed back to the following once _metadata is pulled out of resource return
45+
# $what_if_result.results.result.changedProperties | Should -Be $set_result.results.result.changedProperties
46+
$what_if_result.results.result.changedProperties | Should -Be @('_metadata', '_exist')
4547
$what_if_result.hadErrors | Should -BeFalse
4648
$what_if_result.results.Count | Should -Be 1
4749
$LASTEXITCODE | Should -Be 0

dsc_lib/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ indicatif = { version = "0.17" }
1111
jsonschema = "0.17"
1212
num-traits = "0.2"
1313
regex = "1.7"
14-
reqwest = { version = "0.12", features = ["blocking"] }
1514
schemars = { version = "0.8.12", features = ["preserve_order"] }
1615
serde = { version = "1.0", features = ["derive"] }
1716
serde_json = { version = "1.0", features = ["preserve_order"] }

dsc_lib/src/dscerror.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use std::str::Utf8Error;
55

66
use indicatif::style::TemplateError;
7-
use reqwest::StatusCode;
87
use thiserror::Error;
98
use tracing::error;
109
use tree_sitter::LanguageError;
@@ -32,12 +31,6 @@ pub enum DscError {
3231
#[error("Function '{0}' error: {1}")]
3332
FunctionArg(String, String),
3433

35-
#[error("HTTP: {0}")]
36-
Http(#[from] reqwest::Error),
37-
38-
#[error("HTTP status: {0}")]
39-
HttpStatus(StatusCode),
40-
4134
#[error("Function integer argument conversion error: {0}")]
4235
IntegerConversion(#[from] std::num::ParseIntError),
4336

dsc_lib/src/dscresources/command_resource.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,6 @@ pub fn get_schema(resource: &ResourceManifest, cwd: &str) -> Result<String, DscE
465465
let json = serde_json::to_string(schema)?;
466466
Ok(json)
467467
},
468-
SchemaKind::Url(ref url) => {
469-
// TODO: cache downloaded schemas so we don't have to download them every time
470-
let mut response = reqwest::blocking::get(url)?;
471-
if !response.status().is_success() {
472-
return Err(DscError::HttpStatus(response.status()));
473-
}
474-
475-
let mut body = String::new();
476-
response.read_to_string(&mut body)?;
477-
Ok(body)
478-
},
479468
}
480469
}
481470

dsc_lib/src/dscresources/resource_manifest.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ pub enum SchemaKind {
126126
/// The schema is embedded in the manifest.
127127
#[serde(rename = "embedded")]
128128
Embedded(Value),
129-
/// The schema is retrieved from a URL. Required for intellisense support.
130-
#[serde(rename = "url")]
131-
Url(String),
132129
}
133130

134131
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,44 @@ Describe 'PowerShell adapter resource tests' {
8282
$res.actualState.result| % {$_.Name | Should -Not -BeNullOrEmpty}
8383
}
8484

85+
It 'Verify that ClearCache works in PSAdapter' {
86+
# generate the cache
87+
$null = dsc resource list '*' -a Microsoft.DSC/PowerShell
88+
# call the ClearCache operation
89+
$scriptPath = Join-Path $PSScriptRoot '..' 'psDscAdapter' 'powershell.resource.ps1'
90+
$null = & $scriptPath -Operation ClearCache
91+
# verify that PSAdapter does not find the cache
92+
dsc -l debug resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
93+
$LASTEXITCODE | Should -Be 0
94+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Cache file not found'
95+
}
96+
97+
It 'Verify that a new PS Cache version results in cache rebuid' {
98+
# generate the cache
99+
$null = dsc resource list '*' -a Microsoft.DSC/PowerShell
100+
# update the version in the cache file
101+
$cacheFilePath = if ($IsWindows) {
102+
# PS 6+ on Windows
103+
Join-Path $env:LocalAppData "dsc\PSAdapterCache.json"
104+
} else {
105+
# either WinPS or PS 6+ on Linux/Mac
106+
if ($PSVersionTable.PSVersion.Major -le 5) {
107+
Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
108+
} else {
109+
Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
110+
}
111+
}
112+
$cache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json
113+
$cache.CacheSchemaVersion = 0
114+
$jsonCache = $cache | ConvertTo-Json -Depth 90
115+
New-Item -Force -Path $cacheFilePath -Value $jsonCache -Type File | Out-Null
116+
117+
# verify that a new PS Cache version results in cache rebuid
118+
dsc -l debug resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
119+
$LASTEXITCODE | Should -Be 0
120+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Incompatible version of cache in file'
121+
}
122+
85123
It 'Verify inheritance works in class-based resources' {
86124

87125
$r = dsc resource list '*' -a Microsoft.DSC/PowerShell

powershell-adapter/psDscAdapter/powershell.resource.ps1

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[CmdletBinding()]
44
param(
55
[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')]
6+
[ValidateSet('List', 'Get', 'Set', 'Test', 'Export', 'Validate', 'ClearCache')]
77
[string]$Operation,
88
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true, HelpMessage = 'Configuration or resource input in JSON format.')]
99
[string]$jsonInput = '@{}'
@@ -27,6 +27,24 @@ function Write-DscTrace {
2727
'PSPath=' + $PSHome | Write-DscTrace
2828
'PSModulePath=' + $env:PSModulePath | Write-DscTrace
2929

30+
if ($Operation -eq 'ClearCache') {
31+
$cacheFilePath = if ($IsWindows) {
32+
# PS 6+ on Windows
33+
Join-Path $env:LocalAppData "dsc\PSAdapterCache.json"
34+
} else {
35+
# either WinPS or PS 6+ on Linux/Mac
36+
if ($PSVersionTable.PSVersion.Major -le 5) {
37+
Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
38+
} else {
39+
Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
40+
}
41+
}
42+
43+
'Deleting cache file ' + $cacheFilePath | Write-DscTrace
44+
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
45+
exit 0
46+
}
47+
3048
if ('Validate' -ne $Operation) {
3149
# write $jsonInput to STDERR for debugging
3250
$trace = @{'Debug' = 'jsonInput=' + $jsonInput } | ConvertTo-Json -Compress

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4+
$script:CurrentCacheSchemaVersion = 1
5+
46
function Write-DscTrace {
57
param(
68
[Parameter(Mandatory = $false)]
@@ -242,47 +244,53 @@ function Invoke-DscCacheRefresh {
242244
"Reading from Get-DscResource cache file $cacheFilePath" | Write-DscTrace
243245

244246
$cache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json
245-
$dscResourceCacheEntries = $cache.ResourceCache
246247

247-
if ($dscResourceCacheEntries.Count -eq 0) {
248-
# if there is nothing in the cache file - refresh cache
248+
if ($cache.CacheSchemaVersion -ne $script:CurrentCacheSchemaVersion) {
249249
$refreshCache = $true
250+
"Incompatible version of cache in file '"+$cache.CacheSchemaVersion+"' (expected '"+$script:CurrentCacheSchemaVersion+"')" | Write-DscTrace
251+
} else {
252+
$dscResourceCacheEntries = $cache.ResourceCache
250253

251-
"Filtered DscResourceCache cache is empty" | Write-DscTrace
252-
}
253-
else
254-
{
255-
"Checking cache for stale entries" | Write-DscTrace
254+
if ($dscResourceCacheEntries.Count -eq 0) {
255+
# if there is nothing in the cache file - refresh cache
256+
$refreshCache = $true
256257

257-
foreach ($cacheEntry in $dscResourceCacheEntries) {
258-
#"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace
258+
"Filtered DscResourceCache cache is empty" | Write-DscTrace
259+
}
260+
else
261+
{
262+
"Checking cache for stale entries" | Write-DscTrace
259263

260-
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
261-
262-
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
263-
{
264-
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
265-
$refreshCache = $true
266-
break
264+
foreach ($cacheEntry in $dscResourceCacheEntries) {
265+
#"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace
266+
267+
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
268+
269+
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
270+
{
271+
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
272+
$refreshCache = $true
273+
break
274+
}
267275
}
268-
}
269276

270-
if ($refreshCache) {break}
271-
}
277+
if ($refreshCache) {break}
278+
}
272279

273-
"Checking cache for stale PSModulePath" | Write-DscTrace
280+
"Checking cache for stale PSModulePath" | Write-DscTrace
274281

275-
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
282+
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
276283

277-
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
278-
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
279-
$hs_cache.SymmetricExceptWith($hs_live)
280-
$diff = $hs_cache
284+
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
285+
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
286+
$hs_cache.SymmetricExceptWith($hs_live)
287+
$diff = $hs_cache
281288

282-
"PSModulePath diff '$diff'" | Write-DscTrace
289+
"PSModulePath diff '$diff'" | Write-DscTrace
283290

284-
if ($diff.Count -gt 0) {
285-
$refreshCache = $true
291+
if ($diff.Count -gt 0) {
292+
$refreshCache = $true
293+
}
286294
}
287295
}
288296
}
@@ -330,6 +338,7 @@ function Invoke-DscCacheRefresh {
330338
$cache.ResourceCache = $dscResourceCacheEntries
331339
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
332340
$cache.PSModulePaths = $m.FullName
341+
$cache.CacheSchemaVersion = $script:CurrentCacheSchemaVersion
333342

334343
# save cache for future use
335344
# TODO: replace this with a high-performance serializer
@@ -494,6 +503,7 @@ class dscResourceCacheEntry {
494503
}
495504

496505
class dscResourceCache {
506+
[int] $CacheSchemaVersion
497507
[string[]] $PSModulePaths
498508
[dscResourceCacheEntry[]] $ResourceCache
499509
}

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4+
$script:CurrentCacheSchemaVersion = 1
5+
46
function Write-DscTrace {
57
param(
68
[Parameter(Mandatory = $false)]
@@ -68,47 +70,51 @@ function Invoke-DscCacheRefresh {
6870
"Reading from Get-DscResource cache file $cacheFilePath" | Write-DscTrace
6971

7072
$cache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json
71-
$dscResourceCacheEntries = $cache.ResourceCache
72-
73-
if ($dscResourceCacheEntries.Count -eq 0) {
74-
# if there is nothing in the cache file - refresh cache
73+
if ($cache.CacheSchemaVersion -ne $script:CurrentCacheSchemaVersion) {
7574
$refreshCache = $true
75+
"Incompartible version of cache in file '"+$cache.CacheSchemaVersion+"' (expected '"+$script:CurrentCacheSchemaVersion+"')" | Write-DscTrace
76+
} else {
77+
$dscResourceCacheEntries = $cache.ResourceCache
7678

77-
"Filtered DscResourceCache cache is empty" | Write-DscTrace
78-
}
79-
else
80-
{
81-
"Checking cache for stale entries" | Write-DscTrace
82-
83-
foreach ($cacheEntry in $dscResourceCacheEntries) {
84-
#"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace
85-
86-
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
87-
88-
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
89-
{
90-
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
91-
$refreshCache = $true
92-
break
79+
if ($dscResourceCacheEntries.Count -eq 0) {
80+
# if there is nothing in the cache file - refresh cache
81+
$refreshCache = $true
82+
"Filtered DscResourceCache cache is empty" | Write-DscTrace
83+
}
84+
else
85+
{
86+
"Checking cache for stale entries" | Write-DscTrace
87+
88+
foreach ($cacheEntry in $dscResourceCacheEntries) {
89+
#"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace
90+
91+
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
92+
93+
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
94+
{
95+
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
96+
$refreshCache = $true
97+
break
98+
}
9399
}
94-
}
95100

96-
if ($refreshCache) {break}
97-
}
101+
if ($refreshCache) {break}
102+
}
98103

99-
"Checking cache for stale PSModulePath" | Write-DscTrace
104+
"Checking cache for stale PSModulePath" | Write-DscTrace
100105

101-
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
106+
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
102107

103-
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
104-
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
105-
$hs_cache.SymmetricExceptWith($hs_live)
106-
$diff = $hs_cache
108+
$hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths)
109+
$hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName)
110+
$hs_cache.SymmetricExceptWith($hs_live)
111+
$diff = $hs_cache
107112

108-
"PSModulePath diff '$diff'" | Write-DscTrace
113+
"PSModulePath diff '$diff'" | Write-DscTrace
109114

110-
if ($diff.Count -gt 0) {
111-
$refreshCache = $true
115+
if ($diff.Count -gt 0) {
116+
$refreshCache = $true
117+
}
112118
}
113119
}
114120
}
@@ -223,6 +229,7 @@ function Invoke-DscCacheRefresh {
223229
$cache.ResourceCache = $dscResourceCacheEntries
224230
$m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue}
225231
$cache.PSModulePaths = $m.FullName
232+
$cache.CacheSchemaVersion = $script:CurrentCacheSchemaVersion
226233

227234
# save cache for future use
228235
# TODO: replace this with a high-performance serializer
@@ -472,6 +479,7 @@ class dscResourceCacheEntry {
472479
}
473480

474481
class dscResourceCache {
482+
[int] $CacheSchemaVersion
475483
[string[]] $PSModulePaths
476484
[dscResourceCacheEntry[]] $ResourceCache
477485
}

registry/registry.dsc.resource.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@
3939
}
4040
]
4141
},
42+
"whatIf": {
43+
"executable": "registry",
44+
"args": [
45+
"config",
46+
"set",
47+
"-w",
48+
{
49+
"jsonInputArg": "--input",
50+
"mandatory": true
51+
}
52+
],
53+
"return": "state"
54+
},
4255
"exitCodes": {
4356
"0": "Success",
4457
"1": "Invalid parameter",

0 commit comments

Comments
 (0)