Skip to content

Commit

Permalink
Merge pull request #311 from tgauth/support-yaml-resources
Browse files Browse the repository at this point in the history
Support yaml resource discovery
  • Loading branch information
SteveL-MSFT authored Feb 8, 2024
2 parents 0643e31 + bd0bc51 commit 7a070e7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
62 changes: 52 additions & 10 deletions dsc/tests/dsc_discovery.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
# Licensed under the MIT License.

Describe 'tests for resource discovery' {
BeforeAll {
$env:DSC_RESOURCE_PATH = $testdrive
}

AfterEach {
Remove-Item -Path "$testdrive/test.dsc.resource.*" -ErrorAction SilentlyContinue
}

AfterAll {
$env:DSC_RESOURCE_PATH = $null
}

It 'Use DSC_RESOURCE_PATH instead of PATH when defined' {
$resourceJson = @'
{
Expand All @@ -14,15 +26,45 @@ Describe 'tests for resource discovery' {
}
'@

try {
$env:DSC_RESOURCE_PATH = $testdrive
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value $resourceJson
$resources = dsc resource list | ConvertFrom-Json
$resources.Count | Should -Be 1
$resources.type | Should -BeExactly 'DSC/TestPathResource'
}
finally {
$env:DSC_RESOURCE_PATH = $null
}
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value $resourceJson
$resources = dsc resource list | ConvertFrom-Json
$resources.Count | Should -Be 1
$resources.type | Should -BeExactly 'DSC/TestPathResource'
}

It 'support discovering <extension>' -TestCases @(
@{ extension = 'yaml' }
@{ extension = 'yml' }
) {
param($extension)

$resourceYaml = @'
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json
type: DSC/TestYamlResource
version: 0.1.0
get:
executable: dsc
'@

Set-Content -Path "$testdrive/test.dsc.resource.$extension" -Value $resourceYaml
$resources = dsc resource list | ConvertFrom-Json
$resources.Count | Should -Be 1
$resources.type | Should -BeExactly 'DSC/TestYamlResource'
}

It 'does not support discovering a file with an extension that is not json or yaml' {
param($extension)

$resourceInput = @'
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json
type: DSC/TestYamlResource
version: 0.1.0
get:
executable: dsc
'@

Set-Content -Path "$testdrive/test.dsc.resource.txt" -Value $resourceInput
$resources = dsc resource list | ConvertFrom-Json
$resources.Count | Should -Be 0
}
}
1 change: 1 addition & 0 deletions dsc_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ reqwest = { version = "0.11", features = ["blocking"] }
schemars = { version = "0.8.12", features = ["preserve_order"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }
serde_yaml = { version = "0.9.3" }
thiserror = "1.0"
chrono = "0.4.26"
tracing = "0.1.37"
Expand Down
25 changes: 20 additions & 5 deletions dsc_lib/src/discovery/command_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::dscresources::command_resource::invoke_command;
use crate::dscerror::DscError;
use std::collections::BTreeMap;
use std::env;
use std::ffi::OsStr;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
Expand Down Expand Up @@ -50,7 +51,10 @@ impl CommandDiscovery {
let path = entry.path();
if path.is_file() {
let file_name = path.file_name().unwrap().to_str().unwrap();
if file_name.to_lowercase().ends_with(".dsc.resource.json") {
let file_name_lowercase = file_name.to_lowercase();
if file_name_lowercase.ends_with(".dsc.resource.json") ||
file_name_lowercase.ends_with(".dsc.resource.yaml") ||
file_name_lowercase.ends_with(".dsc.resource.yml") {
let resource = match load_manifest(&path)
{
Ok(r) => r,
Expand Down Expand Up @@ -203,12 +207,23 @@ impl ResourceDiscovery for CommandDiscovery {
fn load_manifest(path: &Path) -> Result<DscResource, DscError> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let manifest: ResourceManifest = match serde_json::from_reader(reader) {
Ok(manifest) => manifest,
Err(err) => {
return Err(DscError::Manifest(path.to_string_lossy().to_string(), err));
let manifest: ResourceManifest = if path.extension() == Some(OsStr::new("json")) {
match serde_json::from_reader(reader) {
Ok(manifest) => manifest,
Err(err) => {
return Err(DscError::Manifest(path.to_string_lossy().to_string(), err));
}
}
}
else {
match serde_yaml::from_reader(reader) {
Ok(manifest) => manifest,
Err(err) => {
return Err(DscError::ManifestYaml(path.to_string_lossy().to_string(), err));
}
}
};

let resource = DscResource {
type_name: manifest.resource_type.clone(),
implemented_as: ImplementedAs::Command,
Expand Down
3 changes: 3 additions & 0 deletions dsc_lib/src/dscerror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ pub enum DscError {
#[error("Manifest: {0}\nJSON: {1}")]
Manifest(String, serde_json::Error),

#[error("Manifest: {0}\nYAML: {1}")]
ManifestYaml(String, serde_yaml::Error),

#[error("Missing manifest: {0}")]
MissingManifest(String),

Expand Down

0 comments on commit 7a070e7

Please sign in to comment.