Skip to content

Commit 7a070e7

Browse files
authored
Merge pull request #311 from tgauth/support-yaml-resources
Support yaml resource discovery
2 parents 0643e31 + bd0bc51 commit 7a070e7

File tree

4 files changed

+76
-15
lines changed

4 files changed

+76
-15
lines changed

dsc/tests/dsc_discovery.tests.ps1

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
# Licensed under the MIT License.
33

44
Describe 'tests for resource discovery' {
5+
BeforeAll {
6+
$env:DSC_RESOURCE_PATH = $testdrive
7+
}
8+
9+
AfterEach {
10+
Remove-Item -Path "$testdrive/test.dsc.resource.*" -ErrorAction SilentlyContinue
11+
}
12+
13+
AfterAll {
14+
$env:DSC_RESOURCE_PATH = $null
15+
}
16+
517
It 'Use DSC_RESOURCE_PATH instead of PATH when defined' {
618
$resourceJson = @'
719
{
@@ -14,15 +26,45 @@ Describe 'tests for resource discovery' {
1426
}
1527
'@
1628

17-
try {
18-
$env:DSC_RESOURCE_PATH = $testdrive
19-
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value $resourceJson
20-
$resources = dsc resource list | ConvertFrom-Json
21-
$resources.Count | Should -Be 1
22-
$resources.type | Should -BeExactly 'DSC/TestPathResource'
23-
}
24-
finally {
25-
$env:DSC_RESOURCE_PATH = $null
26-
}
29+
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value $resourceJson
30+
$resources = dsc resource list | ConvertFrom-Json
31+
$resources.Count | Should -Be 1
32+
$resources.type | Should -BeExactly 'DSC/TestPathResource'
33+
}
34+
35+
It 'support discovering <extension>' -TestCases @(
36+
@{ extension = 'yaml' }
37+
@{ extension = 'yml' }
38+
) {
39+
param($extension)
40+
41+
$resourceYaml = @'
42+
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json
43+
type: DSC/TestYamlResource
44+
version: 0.1.0
45+
get:
46+
executable: dsc
47+
'@
48+
49+
Set-Content -Path "$testdrive/test.dsc.resource.$extension" -Value $resourceYaml
50+
$resources = dsc resource list | ConvertFrom-Json
51+
$resources.Count | Should -Be 1
52+
$resources.type | Should -BeExactly 'DSC/TestYamlResource'
53+
}
54+
55+
It 'does not support discovering a file with an extension that is not json or yaml' {
56+
param($extension)
57+
58+
$resourceInput = @'
59+
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json
60+
type: DSC/TestYamlResource
61+
version: 0.1.0
62+
get:
63+
executable: dsc
64+
'@
65+
66+
Set-Content -Path "$testdrive/test.dsc.resource.txt" -Value $resourceInput
67+
$resources = dsc resource list | ConvertFrom-Json
68+
$resources.Count | Should -Be 0
2769
}
2870
}

dsc_lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ reqwest = { version = "0.11", features = ["blocking"] }
1212
schemars = { version = "0.8.12", features = ["preserve_order"] }
1313
serde = { version = "1.0", features = ["derive"] }
1414
serde_json = { version = "1.0", features = ["preserve_order"] }
15+
serde_yaml = { version = "0.9.3" }
1516
thiserror = "1.0"
1617
chrono = "0.4.26"
1718
tracing = "0.1.37"

dsc_lib/src/discovery/command_discovery.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::dscresources::command_resource::invoke_command;
88
use crate::dscerror::DscError;
99
use std::collections::BTreeMap;
1010
use std::env;
11+
use std::ffi::OsStr;
1112
use std::fs::File;
1213
use std::io::BufReader;
1314
use std::path::Path;
@@ -50,7 +51,10 @@ impl CommandDiscovery {
5051
let path = entry.path();
5152
if path.is_file() {
5253
let file_name = path.file_name().unwrap().to_str().unwrap();
53-
if file_name.to_lowercase().ends_with(".dsc.resource.json") {
54+
let file_name_lowercase = file_name.to_lowercase();
55+
if file_name_lowercase.ends_with(".dsc.resource.json") ||
56+
file_name_lowercase.ends_with(".dsc.resource.yaml") ||
57+
file_name_lowercase.ends_with(".dsc.resource.yml") {
5458
let resource = match load_manifest(&path)
5559
{
5660
Ok(r) => r,
@@ -203,12 +207,23 @@ impl ResourceDiscovery for CommandDiscovery {
203207
fn load_manifest(path: &Path) -> Result<DscResource, DscError> {
204208
let file = File::open(path)?;
205209
let reader = BufReader::new(file);
206-
let manifest: ResourceManifest = match serde_json::from_reader(reader) {
207-
Ok(manifest) => manifest,
208-
Err(err) => {
209-
return Err(DscError::Manifest(path.to_string_lossy().to_string(), err));
210+
let manifest: ResourceManifest = if path.extension() == Some(OsStr::new("json")) {
211+
match serde_json::from_reader(reader) {
212+
Ok(manifest) => manifest,
213+
Err(err) => {
214+
return Err(DscError::Manifest(path.to_string_lossy().to_string(), err));
215+
}
216+
}
217+
}
218+
else {
219+
match serde_yaml::from_reader(reader) {
220+
Ok(manifest) => manifest,
221+
Err(err) => {
222+
return Err(DscError::ManifestYaml(path.to_string_lossy().to_string(), err));
223+
}
210224
}
211225
};
226+
212227
let resource = DscResource {
213228
type_name: manifest.resource_type.clone(),
214229
implemented_as: ImplementedAs::Command,

dsc_lib/src/dscerror.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ pub enum DscError {
5252
#[error("Manifest: {0}\nJSON: {1}")]
5353
Manifest(String, serde_json::Error),
5454

55+
#[error("Manifest: {0}\nYAML: {1}")]
56+
ManifestYaml(String, serde_yaml::Error),
57+
5558
#[error("Missing manifest: {0}")]
5659
MissingManifest(String),
5760

0 commit comments

Comments
 (0)