Skip to content

Commit ffd58ca

Browse files
authored
Merge pull request #377 from anmenaga/issue_274
Added adapter filter
2 parents d85f90e + 16a25ae commit ffd58ca

File tree

13 files changed

+317
-159
lines changed

13 files changed

+317
-159
lines changed

dsc/src/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ pub enum ResourceSubCommand {
128128
List {
129129
/// Optional filter to apply to the list of resources
130130
resource_name: Option<String>,
131+
/// Optional adapter filter to apply to the list of resources
132+
#[clap(short = 'a', long = "adapter", help = "Adapter filter to limit the resource search")]
133+
adapter_name: Option<String>,
131134
#[clap(short, long, help = "Description keyword to search for in the resource description")]
132135
description: Option<String>,
133136
#[clap(short, long, help = "Tag to search for in the resource tags")]

dsc/src/subcommand.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ pub fn validate_config(config: &str) -> Result<(), DscError> {
387387
Ok(())
388388
}
389389

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

399400
match subcommand {
400-
ResourceSubCommand::List { resource_name, description, tags, format } => {
401+
ResourceSubCommand::List { resource_name, adapter_name, description, tags, format } => {
401402

402403
let mut write_table = false;
403404
let mut table = Table::new(&["Type", "Kind", "Version", "Caps", "RequireAdapter", "Description"]);
404405
if format.is_none() && atty::is(Stream::Stdout) {
405406
// write as table if format is not specified and interactive
406407
write_table = true;
407408
}
408-
for resource in dsc.list_available_resources(&resource_name.clone().unwrap_or_default()) {
409+
for resource in dsc.list_available_resources(
410+
&resource_name.clone().unwrap_or("*".to_string()),
411+
&adapter_name.clone().unwrap_or_default()) {
409412
let mut capabilities = "g---".to_string();
410413
if resource.capabilities.contains(&Capability::Set) { capabilities.replace_range(1..2, "s"); }
411414
if resource.capabilities.contains(&Capability::Test) { capabilities.replace_range(2..3, "t"); }

dsc/tests/dsc_args.tests.ps1

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ resources:
180180
$LASTEXITCODE | Should -Be 0
181181
}
182182

183-
It 'resource tracing shows up' {
183+
It 'resource tracing shows up' -Skip:(!$IsWindows) {
184184
# Assumption here is that DSC/PowerShellGroup provider is visible
185-
dsc -l trace resource list 2> $TestDrive/tracing.txt
185+
dsc -l trace resource list * -a *PowerShell* 2> $TestDrive/tracing.txt
186186
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'PSModulePath'
187187
$LASTEXITCODE | Should -Be 0
188188
}
@@ -215,4 +215,60 @@ resources:
215215
$err.Length | Should -Not -Be 0
216216
$LASTEXITCODE | Should -Be 4
217217
}
218+
219+
It 'verify `dsc resource list` and `dsc resource list *`' {
220+
# return all native resources, providers, but not adapter-based resources;
221+
# results for `dsc resource list` and `dsc resource list *` should be the same
222+
$a = dsc resource list -f json
223+
$b = dsc resource list '*' -f json
224+
$a.Count | Should -Be $b.Count
225+
0..($a.Count-1) | %{
226+
$a_obj = $a[$_] | ConvertFrom-Json
227+
$b_obj = $b[$_] | ConvertFrom-Json
228+
$a_obj.type | Should -Be $b_obj.type
229+
# adapter-based resources should Not be in the results
230+
$a_obj.requireAdapter | Should -BeNullOrEmpty
231+
$b_obj.requireAdapter | Should -BeNullOrEmpty
232+
}
233+
}
234+
235+
It 'verify `dsc resource list resource_filter`' {
236+
# same as previous but also apply resource_filter filter
237+
$a = dsc resource list 'Test*' -f json
238+
0..($a.Count-1) | %{
239+
$a_obj = $a[$_] | ConvertFrom-Json
240+
$a_obj.type.StartsWith("Test") | Should -Be $true
241+
# adapter-based resources should Not be in the results
242+
$a_obj.requireAdapter | Should -BeNullOrEmpty
243+
}
244+
}
245+
246+
It 'verify `dsc resource list * -a *`' {
247+
# return all adapter-based resources
248+
$a = dsc resource list '*' -a '*' -f json
249+
0..($a.Count-1) | %{
250+
$a_obj = $a[$_] | ConvertFrom-Json
251+
$a_obj.requireAdapter | Should -Not -BeNullOrEmpty
252+
$a_obj.kind | Should -Be "Resource"
253+
}
254+
}
255+
256+
It 'verify `dsc resource list * adapter_filter`' {
257+
# return all resources of adapters that match adapter_filter filter
258+
$a = dsc resource list '*' -a Test* -f json | ConvertFrom-Json
259+
foreach ($r in $a) {
260+
$r.requireAdapter.StartsWith("Test") | Should -Be $true
261+
$r.kind | Should -Be "Resource"
262+
}
263+
}
264+
265+
It 'verify `dsc resource list resource_filter adapter_filter`' {
266+
# same as previous but also apply resource_filter filter to resource types
267+
$a = dsc resource list *TestResource2 -a *TestGroup -f json | ConvertFrom-Json
268+
$a.Count | Should -Be 1
269+
$r = $a[0]
270+
$r.requireAdapter | Should -Not -BeNullOrEmpty
271+
$r.requireAdapter.StartsWith("Test") | Should -Be $true
272+
$r.kind | Should -Be "Resource"
273+
}
218274
}

0 commit comments

Comments
 (0)