Skip to content

Commit 7de5ab9

Browse files
author
Andrew
committed
Feedback 1
1 parent 86bd66d commit 7de5ab9

File tree

2 files changed

+50
-59
lines changed

2 files changed

+50
-59
lines changed

osinfo/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::string::ToString;
88
#[derive(Debug, Clone, PartialEq, Serialize)]
99
#[serde(deny_unknown_fields)]
1010
pub struct OsInfo {
11-
/// Returns the unique ID for the OSInfo instance data type.
11+
/// Returns the unique ID for the `OSInfo` instance data type.
1212
#[serde(rename = "$id")]
1313
pub id: String,
1414
family: Family,

powershell-adapter/psDscAdapter/psDscAdapter.psm1

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

4+
function Write-DscTrace {
5+
param(
6+
[Parameter(Mandatory = $false)]
7+
[ValidateSet('Error', 'Warn', 'Info', 'Debug', 'Trace')]
8+
[string]$Operation = 'Debug',
9+
10+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
11+
[string]$Message
12+
)
13+
14+
$trace = @{$Operation = $Message } | ConvertTo-Json -Compress
15+
$host.ui.WriteErrorLine($trace)
16+
}
17+
418
# if the version of PowerShell is greater than 5, import the PSDesiredStateConfiguration module
519
# this is necessary because the module is not included in the PowerShell 7.0+ releases;
620
# In Windows PowerShell, we should always use version 1.1 that ships in Windows.
@@ -12,8 +26,7 @@ else {
1226
$env:PSModulePath += ";$env:windir\System32\WindowsPowerShell\v1.0\Modules"
1327
$PSDesiredStateConfiguration = Import-Module -Name 'PSDesiredStateConfiguration' -RequiredVersion '1.1' -Force -PassThru -ErrorAction stop -ErrorVariable $importModuleError
1428
if (-not [string]::IsNullOrEmpty($importModuleError)) {
15-
$trace = @{'Debug' = 'ERROR: Could not import PSDesiredStateConfiguration 1.1 in Windows PowerShell. ' + $importModuleError } | ConvertTo-Json -Compress
16-
$host.ui.WriteErrorLine($trace)
29+
'ERROR: Could not import PSDesiredStateConfiguration 1.1 in Windows PowerShell. ' + $importModuleError | Write-DscTrace
1730
}
1831
}
1932

@@ -39,17 +52,20 @@ function Invoke-DscCacheRefresh {
3952

4053
$refreshCache = $false
4154

42-
$cacheFilePath = Join-Path $env:LocalAppData "dsc\PSAdapterCache.json"
43-
if ($PSVersionTable.PSVersion.Major -le 5) {
44-
$cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
45-
}
46-
if ($IsLinux -or $IsMacOS) {
47-
$cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
55+
$cacheFilePath = if ($IsWindows) {
56+
# PS 6+ on Windows
57+
Join-Path $env:LocalAppData "dsc\PSAdapterCache.json"
58+
} else {
59+
# either WinPS or PS 6+ on Linux/Mac
60+
if ($PSVersionTable.PSVersion.Major -le 5) {
61+
Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json"
62+
} else {
63+
Join-Path $env:HOME ".dsc" "PSAdapterCache.json"
64+
}
4865
}
4966

5067
if (Test-Path $cacheFilePath) {
51-
$trace = @{'Debug' = "Reading from Get-DscResource cache file $cacheFilePath"} | ConvertTo-Json -Compress
52-
$host.ui.WriteErrorLine($trace)
68+
"Reading from Get-DscResource cache file $cacheFilePath" | Write-DscTrace
5369

5470
$cache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json
5571
$dscResourceCacheEntries = $cache.ResourceCache
@@ -58,25 +74,20 @@ function Invoke-DscCacheRefresh {
5874
# if there is nothing in the cache file - refresh cache
5975
$refreshCache = $true
6076

61-
$trace = @{'Debug' = "Filtered DscResourceCache cache is empty"} | ConvertTo-Json -Compress
62-
$host.ui.WriteErrorLine($trace)
77+
"Filtered DscResourceCache cache is empty" | Write-DscTrace
6378
}
6479
else
6580
{
66-
$trace = @{'Debug' = "Checking cache for stale entries"} | ConvertTo-Json -Compress
67-
$host.ui.WriteErrorLine($trace)
81+
"Checking cache for stale entries" | Write-DscTrace
6882

6983
foreach ($cacheEntry in $dscResourceCacheEntries) {
70-
$trace = @{'Trace' = "Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'"} | ConvertTo-Json -Compress
71-
$host.ui.WriteErrorLine($trace)
84+
"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace
7285

7386
$cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object {
7487

7588
if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value)))
7689
{
77-
$trace = @{'Debug' = "Detected stale cache entry '$($_.Name)'"} | ConvertTo-Json -Compress
78-
$host.ui.WriteErrorLine($trace)
79-
90+
"Detected stale cache entry '$($_.Name)'" | Write-DscTrace
8091
$refreshCache = $true
8192
break
8293
}
@@ -85,8 +96,7 @@ function Invoke-DscCacheRefresh {
8596
if ($refreshCache) {break}
8697
}
8798

88-
$trace = @{'Debug' = "Checking cache for stale PSModulePath"} | ConvertTo-Json -Compress
89-
$host.ui.WriteErrorLine($trace)
99+
"Checking cache for stale PSModulePath" | Write-DscTrace
90100

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

@@ -95,24 +105,20 @@ function Invoke-DscCacheRefresh {
95105
$hs_cache.SymmetricExceptWith($hs_live)
96106
$diff = $hs_cache
97107

98-
$trace = @{'Debug' = "PSModulePath diff '$diff'"} | ConvertTo-Json -Compress
99-
$host.ui.WriteErrorLine($trace)
108+
"PSModulePath diff '$diff'" | Write-DscTrace
100109

101110
if ($diff.Count -gt 0) {
102111
$refreshCache = $true
103112
}
104113
}
105114
}
106115
else {
107-
$trace = @{'Debug' = "Cache file not found '$cacheFilePath'"} | ConvertTo-Json -Compress
108-
$host.ui.WriteErrorLine($trace)
109-
116+
"Cache file not found '$cacheFilePath'" | Write-DscTrace
110117
$refreshCache = $true
111118
}
112119

113120
if ($refreshCache) {
114-
$trace = @{'Debug' = 'Constructing Get-DscResource cache'} | ConvertTo-Json -Compress
115-
$host.ui.WriteErrorLine($trace)
121+
'Constructing Get-DscResource cache' | Write-DscTrace
116122

117123
# create a list object to store cache of Get-DscResource
118124
[dscResourceCacheEntry[]]$dscResourceCacheEntries = [System.Collections.Generic.List[Object]]::new()
@@ -153,8 +159,7 @@ function Invoke-DscCacheRefresh {
153159
if ( $psdscVersion -ge '2.0.7' ) {
154160
# only support known dscResourceType
155161
if ([dscResourceType].GetEnumNames() -notcontains $dscResource.ImplementationDetail) {
156-
$trace = @{'Debug' = 'WARNING: implementation detail not found: ' + $dscResource.ImplementationDetail } | ConvertTo-Json -Compress
157-
$host.ui.WriteErrorLine($trace)
162+
'WARNING: implementation detail not found: ' + $dscResource.ImplementationDetail | Write-DscTrace
158163
continue
159164
}
160165
}
@@ -221,8 +226,7 @@ function Invoke-DscCacheRefresh {
221226

222227
# save cache for future use
223228
# TODO: replace this with a high-performance serializer
224-
$trace = @{'Debug' = "Saving Get-DscResource cache to '$cacheFilePath'"} | ConvertTo-Json -Compress
225-
$host.ui.WriteErrorLine($trace)
229+
"Saving Get-DscResource cache to '$cacheFilePath'" | Write-DscTrace
226230
$jsonCache = $cache | ConvertTo-Json -Depth 90
227231
New-Item -Force -Path $cacheFilePath -Value $jsonCache -Type File | Out-Null
228232
}
@@ -242,8 +246,7 @@ function Get-DscResourceObject {
242246

243247
# catch potential for improperly formatted configuration input
244248
if ($inputObj.resources -and -not $inputObj.metadata.'Microsoft.DSC'.context -eq 'configuration') {
245-
$trace = @{'Debug' = 'WARNING: The input has a top level property named "resources" but is not a configuration. If the input should be a configuration, include the property: "metadata": {"Microsoft.DSC": {"context": "Configuration"}}' } | ConvertTo-Json -Compress
246-
$host.ui.WriteErrorLine($trace)
249+
'WARNING: The input has a top level property named "resources" but is not a configuration. If the input should be a configuration, include the property: "metadata": {"Microsoft.DSC": {"context": "Configuration"}}' | Write-DscTrace
247250
}
248251

249252
# match adapter to version of powershell
@@ -290,16 +293,13 @@ function Invoke-DscOperation {
290293
)
291294

292295
$osVersion = [System.Environment]::OSVersion.VersionString
293-
$trace = @{'Debug' = 'OS version: ' + $osVersion } | ConvertTo-Json -Compress
294-
$host.ui.WriteErrorLine($trace)
296+
'OS version: ' + $osVersion | Write-DscTrace
295297

296298
$psVersion = $PSVersionTable.PSVersion.ToString()
297-
$trace = @{'Debug' = 'PowerShell version: ' + $psVersion } | ConvertTo-Json -Compress
298-
$host.ui.WriteErrorLine($trace)
299+
'PowerShell version: ' + $psVersion | Write-DscTrace
299300

300301
$moduleVersion = Get-Module PSDesiredStateConfiguration | ForEach-Object Version
301-
$trace = @{'Debug' = 'PSDesiredStateConfiguration module version: ' + $moduleVersion } | ConvertTo-Json -Compress
302-
$host.ui.WriteErrorLine($trace)
302+
'PSDesiredStateConfiguration module version: ' + $moduleVersion | Write-DscTrace
303303

304304
# get details from cache about the DSC resource, if it exists
305305
$cachedDscResourceInfo = $dscResourceCache | Where-Object Type -EQ $DesiredState.type | ForEach-Object DscResourceInfo
@@ -315,17 +315,15 @@ function Invoke-DscOperation {
315315
if ($_.TypeNameOfValue -EQ 'System.String') { $addToActualState.$($_.Name) = $DesiredState.($_.Name) }
316316
}
317317

318-
$trace = @{'Debug' = 'DSC resource implementation: ' + [dscResourceType]$cachedDscResourceInfo.ImplementationDetail } | ConvertTo-Json -Compress
319-
$host.ui.WriteErrorLine($trace)
318+
'DSC resource implementation: ' + [dscResourceType]$cachedDscResourceInfo.ImplementationDetail | Write-DscTrace
320319

321320
# workaround: script based resources do not validate Get parameter consistency, so we need to remove any parameters the author chose not to include in Get-TargetResource
322321
switch ([dscResourceType]$cachedDscResourceInfo.ImplementationDetail) {
323322
'ScriptBased' {
324323

325324
# For Linux/MacOS, only class based resources are supported and are called directly.
326325
if ($IsLinux) {
327-
$trace = @{'Debug' = 'ERROR: Script based resources are only supported on Windows.' } | ConvertTo-Json -Compress
328-
$host.ui.WriteErrorLine($trace)
326+
'ERROR: Script based resources are only supported on Windows.' | Write-DscTrace
329327
exit 1
330328
}
331329

@@ -361,8 +359,7 @@ function Invoke-DscOperation {
361359
$addToActualState.properties = $getDscResult
362360
}
363361
catch {
364-
$trace = @{'Debug' = 'ERROR: ' + $_.Exception.Message } | ConvertTo-Json -Compress
365-
$host.ui.WriteErrorLine($trace)
362+
'ERROR: ' + $_.Exception.Message | Write-DscTrace
366363
exit 1
367364
}
368365
}
@@ -401,21 +398,18 @@ function Invoke-DscOperation {
401398
}
402399
catch {
403400

404-
$trace = @{'Debug' = 'ERROR: ' + $_.Exception.Message } | ConvertTo-Json -Compress
405-
$host.ui.WriteErrorLine($trace)
401+
'ERROR: ' + $_.Exception.Message | Write-DscTrace
406402
exit 1
407403
}
408404
}
409405
'Binary' {
410406
if ($PSVersionTable.PSVersion.Major -gt 5) {
411-
$trace = @{'Debug' = 'To use a binary resource such as File, Log, or SignatureValidation, use the Microsoft.Windows/WindowsPowerShell adapter.' } | ConvertTo-Json -Compress
412-
$host.ui.WriteErrorLine($trace)
407+
'To use a binary resource such as File, Log, or SignatureValidation, use the Microsoft.Windows/WindowsPowerShell adapter.' | Write-DscTrace
413408
exit 1
414409
}
415410

416411
if (-not (($cachedDscResourceInfo.ImplementedAs -eq 'Binary') -and ('File', 'Log', 'SignatureValidation' -contains $cachedDscResourceInfo.Name))) {
417-
$trace = @{'Debug' = 'Only File, Log, and SignatureValidation are supported as Binary resources.' } | ConvertTo-Json -Compress
418-
$host.ui.WriteErrorLine($trace)
412+
'Only File, Log, and SignatureValidation are supported as Binary resources.' | Write-DscTrace
419413
exit 1
420414
}
421415

@@ -438,14 +432,12 @@ function Invoke-DscOperation {
438432
$addToActualState.properties = $getDscResult
439433
}
440434
catch {
441-
$trace = @{'Debug' = 'ERROR: ' + $_.Exception.Message } | ConvertTo-Json -Compress
442-
$host.ui.WriteErrorLine($trace)
435+
'ERROR: ' + $_.Exception.Message | Write-DscTrace
443436
exit 1
444437
}
445438
}
446439
Default {
447-
$trace = @{'Debug' = 'Can not find implementation of type: ' + $cachedDscResourceInfo.ImplementationDetail } | ConvertTo-Json -Compress
448-
$host.ui.WriteErrorLine($trace)
440+
'Can not find implementation of type: ' + $cachedDscResourceInfo.ImplementationDetail | Write-DscTrace
449441
exit 1
450442
}
451443
}
@@ -455,8 +447,7 @@ function Invoke-DscOperation {
455447
else {
456448
$dsJSON = $DesiredState | ConvertTo-Json -Depth 10
457449
$errmsg = 'Can not find type "' + $DesiredState.type + '" for resource "' + $dsJSON + '". Please ensure that Get-DscResource returns this resource type.'
458-
$trace = @{'Debug' = 'ERROR: ' + $errmsg } | ConvertTo-Json -Compress
459-
$host.ui.WriteErrorLine($trace)
450+
'ERROR: ' + $errmsg | Write-DscTrace
460451
exit 1
461452
}
462453
}

0 commit comments

Comments
 (0)