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

Added adapter filter #377

Merged
merged 21 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions dsc/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ pub enum ResourceSubCommand {
List {
/// Optional filter to apply to the list of resources
resource_name: Option<String>,
/// Optional adapter filter to apply to the list of resources
#[clap(short = 'a', long = "adapter", help = "Adapter filter to limit the resource search")]
adapter_name: Option<String>,
#[clap(short, long, help = "Description keyword to search for in the resource description")]
description: Option<String>,
#[clap(short, long, help = "Tag to search for in the resource tags")]
Expand Down
7 changes: 5 additions & 2 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ pub fn validate_config(config: &str) -> Result<(), DscError> {
Ok(())
}

#[allow(clippy::too_many_lines)]
pub fn resource(subcommand: &ResourceSubCommand, stdin: &Option<String>) {
let mut dsc = match DscManager::new() {
Ok(dsc) => dsc,
Expand All @@ -397,15 +398,17 @@ pub fn resource(subcommand: &ResourceSubCommand, stdin: &Option<String>) {
};

match subcommand {
ResourceSubCommand::List { resource_name, description, tags, format } => {
ResourceSubCommand::List { resource_name, adapter_name, description, tags, format } => {

let mut write_table = false;
let mut table = Table::new(&["Type", "Kind", "Version", "Caps", "RequireAdapter", "Description"]);
if format.is_none() && atty::is(Stream::Stdout) {
// write as table if format is not specified and interactive
write_table = true;
}
for resource in dsc.list_available_resources(&resource_name.clone().unwrap_or_default()) {
for resource in dsc.list_available_resources(
&resource_name.clone().unwrap_or("*".to_string()),
&adapter_name.clone().unwrap_or_default()) {
let mut capabilities = "g---".to_string();
if resource.capabilities.contains(&Capability::Set) { capabilities.replace_range(1..2, "s"); }
if resource.capabilities.contains(&Capability::Test) { capabilities.replace_range(2..3, "t"); }
Expand Down
60 changes: 58 additions & 2 deletions dsc/tests/dsc_args.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ resources:
$LASTEXITCODE | Should -Be 0
}

It 'resource tracing shows up' {
It 'resource tracing shows up' -Skip:(!$IsWindows) {
anmenaga marked this conversation as resolved.
Show resolved Hide resolved
# Assumption here is that DSC/PowerShellGroup provider is visible
dsc -l trace resource list 2> $TestDrive/tracing.txt
dsc -l trace resource list * -a *PowerShell* 2> $TestDrive/tracing.txt
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'PSModulePath'
$LASTEXITCODE | Should -Be 0
}
Expand Down Expand Up @@ -215,4 +215,60 @@ resources:
$err.Length | Should -Not -Be 0
$LASTEXITCODE | Should -Be 4
}

It 'verify `dsc resource list` and `dsc resource list *`' {
# return all native resources, providers, but not adapter-based resources;
# results for `dsc resource list` and `dsc resource list *` should be the same
$a = dsc resource list -f json
$b = dsc resource list '*' -f json
$a.Count | Should -Be $b.Count
0..($a.Count-1) | %{
$a_obj = $a[$_] | ConvertFrom-Json
$b_obj = $b[$_] | ConvertFrom-Json
$a_obj.type | Should -Be $b_obj.type
# adapter-based resources should Not be in the results
$a_obj.requireAdapter | Should -BeNullOrEmpty
$b_obj.requireAdapter | Should -BeNullOrEmpty
}
}

It 'verify `dsc resource list resource_filter`' {
# same as previous but also apply resource_filter filter
$a = dsc resource list 'Test*' -f json
0..($a.Count-1) | %{
$a_obj = $a[$_] | ConvertFrom-Json
$a_obj.type.StartsWith("Test") | Should -Be $true
# adapter-based resources should Not be in the results
$a_obj.requireAdapter | Should -BeNullOrEmpty
}
}

It 'verify `dsc resource list * -a *`' {
# return all adapter-based resources
$a = dsc resource list '*' -a '*' -f json
0..($a.Count-1) | %{
$a_obj = $a[$_] | ConvertFrom-Json
$a_obj.requireAdapter | Should -Not -BeNullOrEmpty
$a_obj.kind | Should -Be "Resource"
}
}

It 'verify `dsc resource list * adapter_filter`' {
# return all resources of adapters that match adapter_filter filter
$a = dsc resource list '*' -a Test* -f json | ConvertFrom-Json
foreach ($r in $a) {
$r.requireAdapter.StartsWith("Test") | Should -Be $true
$r.kind | Should -Be "Resource"
}
}

It 'verify `dsc resource list resource_filter adapter_filter`' {
# same as previous but also apply resource_filter filter to resource types
$a = dsc resource list *TestResource2 -a *TestGroup -f json | ConvertFrom-Json
$a.Count | Should -Be 1
$r = $a[0]
$r.requireAdapter | Should -Not -BeNullOrEmpty
$r.requireAdapter.StartsWith("Test") | Should -Be $true
$r.kind | Should -Be "Resource"
}
}
Loading
Loading