Skip to content

Blocked non-list operations on adapters #525

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

Merged
merged 13 commits into from
Aug 30, 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
34 changes: 33 additions & 1 deletion dsc/src/resource_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::args::OutputFormat;
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, add_type_name_to_json, write_output};
use dsc_lib::configure::config_doc::{Configuration, ExecutionKind};
use dsc_lib::configure::add_resource_export_results_to_configuration;
use dsc_lib::dscresources::invoke_result::{GetResult, ResourceGetResponse};
use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{GetResult, ResourceGetResponse}};
use dsc_lib::dscerror::DscError;
use tracing::{error, debug};

Expand All @@ -22,6 +22,11 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op
};

debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
if resource.kind == Kind::Adapter {
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
exit(EXIT_DSC_ERROR);
}

if let Some(requires) = &resource.require_adapter {
input = add_type_name_to_json(input, resource.type_name.clone());
if let Some(pr) = get_resource(dsc, requires) {
Expand Down Expand Up @@ -59,6 +64,11 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: &Option<OutputForm
};

debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
if resource.kind == Kind::Adapter {
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
exit(EXIT_DSC_ERROR);
}

if let Some(requires) = &resource.require_adapter {
input = add_type_name_to_json(input, resource.type_name.clone());
if let Some(pr) = get_resource(dsc, requires) {
Expand Down Expand Up @@ -106,6 +116,10 @@ pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op
};

debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
if resource.kind == Kind::Adapter {
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
exit(EXIT_DSC_ERROR);
}

if let Some(requires) = &resource.require_adapter {
input = add_type_name_to_json(input, resource.type_name.clone());
Expand Down Expand Up @@ -148,6 +162,10 @@ pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: &O
};

debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
if resource.kind == Kind::Adapter {
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
exit(EXIT_DSC_ERROR);
}

if let Some(requires) = &resource.require_adapter {
input = add_type_name_to_json(input, resource.type_name.clone());
Expand Down Expand Up @@ -185,6 +203,10 @@ pub fn delete(dsc: &DscManager, resource_type: &str, mut input: String) {
};

debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
if resource.kind == Kind::Adapter {
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
exit(EXIT_DSC_ERROR);
}

if let Some(requires) = &resource.require_adapter {
input = add_type_name_to_json(input, resource.type_name.clone());
Expand All @@ -210,6 +232,11 @@ pub fn schema(dsc: &DscManager, resource_type: &str, format: &Option<OutputForma
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
return
};
if resource.kind == Kind::Adapter {
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
exit(EXIT_DSC_ERROR);
}

match resource.schema() {
Ok(json) => {
// verify is json
Expand All @@ -236,6 +263,11 @@ pub fn export(dsc: &mut DscManager, resource_type: &str, format: &Option<OutputF
return
};

if dsc_resource.kind == Kind::Adapter {
error!("Can not perform this operation on the adapter {} itself", dsc_resource.type_name);
exit(EXIT_DSC_ERROR);
}

let mut adapter_resource: Option<&DscResource> = None;
if let Some(requires) = &dsc_resource.require_adapter {
input = add_type_name_to_json(input, dsc_resource.type_name.clone());
Expand Down
36 changes: 36 additions & 0 deletions dsc/tests/dsc_args.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,40 @@ resources:
$stderr = dsc config get -d $configFile 2>&1
$stderr | Should -Match '.*?--path.*?'
}

It 'Get operation on the adapter itself should fail' {
dsc resource get -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 2
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
}

It 'Get-all operation on the adapter itself should fail' {
dsc resource get --all -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 2
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
}

It 'Set operation on the adapter itself should fail' {
'abc' | dsc resource set -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 2
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
}

It 'Test operation on the adapter itself should fail' {
'abc' | dsc resource test -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 2
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
}

It 'Export operation on the adapter itself should fail' {
dsc resource export -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 2
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
}

It 'Delete operation on the adapter itself should fail' {
dsc resource delete -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
$LASTEXITCODE | Should -Be 2
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter'
}
}
Loading