Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support yaml resource discovery #311

Merged
merged 5 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 36 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,29 @@ 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'
}
}
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
24 changes: 19 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,9 @@ 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") {
if file_name.to_lowercase().ends_with(".dsc.resource.json") |
tgauth marked this conversation as resolved.
Show resolved Hide resolved
file_name.to_lowercase().ends_with(".dsc.resource.yaml") |
file_name.to_lowercase().ends_with(".dsc.resource.yml") {
let resource = match load_manifest(&path)
{
Ok(r) => r,
Expand Down Expand Up @@ -203,12 +206,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
Loading