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

Refactor resource discovery code #422

Merged
merged 15 commits into from
Apr 27, 2024
7 changes: 6 additions & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ if (!$SkipBuild) {
# make sure dependencies are built first so clippy runs correctly
$windows_projects = @("pal", "registry", "reboot_pending", "wmi-adapter")

$macOS_projects = @("resources/brew")

# projects are in dependency order
$projects = @(
"tree-sitter-dscexpression",
Expand All @@ -170,7 +172,6 @@ if (!$SkipBuild) {
"osinfo",
"powershell-adapter",
"process",
"resources/brew",
"runcommandonset",
"tools/dsctest",
"tools/test_group_resource",
Expand All @@ -187,6 +188,10 @@ if (!$SkipBuild) {
Get-ChildItem -Path $target -Recurse -Hidden | ForEach-Object { $_.Attributes = 'Normal' }
}

if ($IsMacOS) {
$projects += $macOS_projects
}

$failed = $false
foreach ($project in $projects) {
## Build format_json
Expand Down
8 changes: 4 additions & 4 deletions dsc/src/resource_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op
if let Some(pr) = get_resource(dsc, requires) {
resource = pr;
} else {
error!("Adapter {} not found", requires);
error!("Adapter '{}' not found", requires);
return;
};
}
Expand Down Expand Up @@ -112,7 +112,7 @@ pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op
if let Some(pr) = get_resource(dsc, requires) {
resource = pr;
} else {
error!("Adapter {} not found", requires);
error!("Adapter '{}' not found", requires);
return;
};
}
Expand Down Expand Up @@ -149,7 +149,7 @@ pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: &O
if let Some(pr) = get_resource(dsc, requires) {
resource = pr;
} else {
error!("Adapter {} not found", requires);
error!("Adapter '{}' not found", requires);
return;
};
}
Expand Down Expand Up @@ -186,7 +186,7 @@ pub fn delete(dsc: &DscManager, resource_type: &str, mut input: String) {
if let Some(pr) = get_resource(dsc, requires) {
resource = pr;
} else {
error!("Adapter {} not found", requires);
error!("Adapter '{}' not found", requires);
return;
};
}
Expand Down
14 changes: 7 additions & 7 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ pub fn validate_config(config: &str) -> Result<(), DscError> {

resource_types.push(type_name.to_lowercase().to_string());
}
dsc.discover_resources(&resource_types);
dsc.find_resources(&resource_types);

for resource_block in resources {
let Some(type_name) = resource_block["type"].as_str() else {
Expand Down Expand Up @@ -402,33 +402,33 @@ pub fn resource(subcommand: &ResourceSubCommand, stdin: &Option<String>) {
list_resources(&mut dsc, resource_name, adapter_name, description, tags, format);
},
ResourceSubCommand::Schema { resource , format } => {
dsc.discover_resources(&[resource.to_lowercase().to_string()]);
dsc.find_resources(&[resource.to_lowercase().to_string()]);
resource_command::schema(&dsc, resource, format);
},
ResourceSubCommand::Export { resource, format } => {
dsc.discover_resources(&[resource.to_lowercase().to_string()]);
dsc.find_resources(&[resource.to_lowercase().to_string()]);
resource_command::export(&mut dsc, resource, format);
},
ResourceSubCommand::Get { resource, input, path, all, format } => {
dsc.discover_resources(&[resource.to_lowercase().to_string()]);
dsc.find_resources(&[resource.to_lowercase().to_string()]);
if *all { resource_command::get_all(&dsc, resource, format); }
else {
let parsed_input = get_input(input, stdin, path);
resource_command::get(&dsc, resource, parsed_input, format);
}
},
ResourceSubCommand::Set { resource, input, path, format } => {
dsc.discover_resources(&[resource.to_lowercase().to_string()]);
dsc.find_resources(&[resource.to_lowercase().to_string()]);
let parsed_input = get_input(input, stdin, path);
resource_command::set(&dsc, resource, parsed_input, format);
},
ResourceSubCommand::Test { resource, input, path, format } => {
dsc.discover_resources(&[resource.to_lowercase().to_string()]);
dsc.find_resources(&[resource.to_lowercase().to_string()]);
let parsed_input = get_input(input, stdin, path);
resource_command::test(&dsc, resource, parsed_input, format);
},
ResourceSubCommand::Delete { resource, input, path } => {
dsc.discover_resources(&[resource.to_lowercase().to_string()]);
dsc.find_resources(&[resource.to_lowercase().to_string()]);
let parsed_input = get_input(input, stdin, path);
resource_command::delete(&dsc, resource, parsed_input);
},
Expand Down
14 changes: 7 additions & 7 deletions dsc/tests/dsc_tracing.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe 'tracing tests' {

It 'trace level error does not emit other levels' {
$logPath = "$TestDrive/dsc_trace.log"
$null = '{}' | dsc --trace-level error resource get -r 'DoesNotExist' 2> $logPath
$null = '{}' | dsc --trace-level error resource list 'DoesNotExist' 2> $logPath
$log = Get-Content $logPath -Raw
$log | Should -Not -BeLikeExactly "* WARNING *"
$log | Should -Not -BeLikeExactly "* INFO *"
Expand All @@ -29,18 +29,18 @@ Describe 'tracing tests' {

It 'trace format plaintext does not emit ANSI' {
$logPath = "$TestDrive/dsc_trace.log"
$null = '{}' | dsc --trace-format plaintext resource get -r 'DoesNotExist' 2> $logPath
$null = '{}' | dsc --trace-format plaintext resource list 'DoesNotExist' 2> $logPath
$log = Get-Content $logPath -Raw
$log | Should -Not -BeLikeExactly "*``[0m*"
}

It 'trace format json emits json' {
$logPath = "$TestDrive/dsc_trace.log"
$null = '{}' | dsc --trace-format json resource get -r 'DoesNotExist' 2> $logPath
$null = '{}' | dsc --trace-format json resource list 'DoesNotExist' 2> $logPath
foreach ($line in (Get-Content $logPath)) {
$trace = $line | ConvertFrom-Json -Depth 10
$trace.timestamp | Should -Not -BeNullOrEmpty
$trace.level | Should -BeIn 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'TRACE'
$trace.level | Should -BeIn 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE'
$trace.fields.message | Should -Not -BeNullOrEmpty
}
}
Expand All @@ -55,12 +55,12 @@ Describe 'tracing tests' {
param($level, $sourceExpected)

$logPath = "$TestDrive/dsc_trace.log"
$null = '{}' | dsc -l $level resource get -r 'DoesNotExist' 2> $logPath
$null = '{}' | dsc -l $level resource list 'DoesNotExist' 2> $logPath
$log = Get-Content $logPath -Raw
if ($sourceExpected) {
$log | Should -BeLike "*dsc*: *"
$log | Should -BeLike "*dsc_lib*: *"
} else {
$log | Should -Not -BeLike "*dsc*: *"
$log | Should -Not -BeLike "*dsc_lib*: *"
}
}
}
8 changes: 4 additions & 4 deletions dsc_lib/src/configure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,18 @@ impl Configurator {
resource_type: resource.resource_type.clone(),
result: set_result,
};
result.results.push(resource_result);
result.results.push(resource_result);
} else if dsc_resource.capabilities.contains(&Capability::Delete) {
debug!("Resource implements delete and _exist is false");
let before_result = dsc_resource.get(&desired)?;
let start_datetime = chrono::Local::now();
dsc_resource.delete(&desired)?;
let end_datetime = chrono::Local::now();
let after_result = dsc_resource.get(&desired)?;
// convert get result to set result
// convert get result to set result
let set_result = match before_result {
GetResult::Resource(before_response) => {
let GetResult::Resource(after_result) = after_result else {
let GetResult::Resource(after_result) = after_result else {
return Err(DscError::NotSupported("Group resources not supported for delete".to_string()))
};
let before_value = serde_json::to_value(&before_response.actual_state)?;
Expand Down Expand Up @@ -614,7 +614,7 @@ impl Configurator {
let mut required_resources = config.resources.iter().map(|p| p.resource_type.to_lowercase()).collect::<Vec<String>>();
required_resources.sort_unstable();
required_resources.dedup();
self.discovery.discover_resources(&required_resources);
self.discovery.find_resources(&required_resources);
Ok(config)
}

Expand Down
Loading
Loading