diff --git a/dsc/src/args.rs b/dsc/src/args.rs index dd331a516..add67b0bb 100644 --- a/dsc/src/args.rs +++ b/dsc/src/args.rs @@ -264,6 +264,10 @@ pub enum ResourceSubCommand { input: Option, #[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")] file: Option, + #[clap(short = 'o', long, help = t!("args.outputFormat").to_string())] + output_format: Option, + #[clap(short = 'w', long, visible_aliases = ["dry-run", "noop"], help = t!("args.whatIf").to_string())] + what_if: bool, }, #[clap(name = "schema", about = "Get the JSON schema for a resource", arg_required_else_help = true)] Schema { diff --git a/dsc/src/resource_command.rs b/dsc/src/resource_command.rs index 54a20283a..0bec12cc2 100644 --- a/dsc/src/resource_command.rs +++ b/dsc/src/resource_command.rs @@ -6,7 +6,7 @@ use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, EXIT_DSC_R use dsc_lib::configure::config_doc::{Configuration, ExecutionKind}; use dsc_lib::configure::add_resource_export_results_to_configuration; use dsc_lib::discovery::discovery_trait::DiscoveryFilter; -use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{GetResult, ResourceGetResponse, ResourceSetResponse, SetResult}}; +use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{DeleteResultKind, GetResult, ResourceGetResponse, ResourceSetResponse, SetResult}}; use dsc_lib::dscresources::dscresource::{Capability, get_diff}; use dsc_lib::dscerror::DscError; use rust_i18n::t; @@ -258,7 +258,7 @@ pub fn test(dsc: &mut DscManager, resource_type: &str, version: Option<&str>, in } } -pub fn delete(dsc: &mut DscManager, resource_type: &str, version: Option<&str>, input: &str) { +pub fn delete(dsc: &mut DscManager, resource_type: &str, version: Option<&str>, input: &str, format: Option<&OutputFormat>, what_if: bool) { let Some(resource) = get_resource(dsc, resource_type, version) else { error!("{}", DscError::ResourceNotFound(resource_type.to_string(), version.unwrap_or("").to_string()).to_string()); exit(EXIT_DSC_RESOURCE_NOT_FOUND); @@ -270,8 +270,33 @@ pub fn delete(dsc: &mut DscManager, resource_type: &str, version: Option<&str>, exit(EXIT_DSC_ERROR); } - match resource.delete(input, &ExecutionKind::Actual) { - Ok(_) => {} + let execution_kind = if what_if { ExecutionKind::WhatIf } else { ExecutionKind::Actual }; + + match resource.delete(input, &execution_kind) { + Ok(result) => { + match result { + DeleteResultKind::ResourceActual => { + }, + DeleteResultKind::ResourceWhatIf(delete_result) => { + match serde_json::to_string(&delete_result) { + Ok(json) => write_object(&json, format, false), + Err(err) => { + error!("JSON: {err}"); + exit(EXIT_JSON_ERROR); + } + } + }, + DeleteResultKind::SyntheticWhatIf(test_result) => { + match serde_json::to_string(&test_result) { + Ok(json) => write_object(&json, format, false), + Err(err) => { + error!("JSON: {err}"); + exit(EXIT_JSON_ERROR); + } + } + } + } + }, Err(err) => { error!("{err}"); exit(EXIT_DSC_ERROR); diff --git a/dsc/src/subcommand.rs b/dsc/src/subcommand.rs index e5decc185..fe683b5f8 100644 --- a/dsc/src/subcommand.rs +++ b/dsc/src/subcommand.rs @@ -595,13 +595,13 @@ pub fn resource(subcommand: &ResourceSubCommand, progress_format: ProgressFormat let parsed_input = get_input(input.as_ref(), path.as_ref()); resource_command::test(&mut dsc, resource, version.as_deref(), &parsed_input, output_format.as_ref()); }, - ResourceSubCommand::Delete { resource, version, input, file: path } => { + ResourceSubCommand::Delete { resource, version, input, file: path, output_format, what_if } => { if let Err(err) = dsc.find_resources(&[DiscoveryFilter::new(resource, version.as_deref(), None)], progress_format) { error!("{}: {err}", t!("subcommand.failedDiscoverResource")); exit(EXIT_DSC_ERROR); } let parsed_input = get_input(input.as_ref(), path.as_ref()); - resource_command::delete(&mut dsc, resource, version.as_deref(), &parsed_input); + resource_command::delete(&mut dsc, resource, version.as_deref(), &parsed_input, output_format.as_ref(), *what_if); }, } } diff --git a/dsc/tests/dsc_whatif.tests.ps1 b/dsc/tests/dsc_whatif.tests.ps1 index ba838f8b6..c05b3d21d 100644 --- a/dsc/tests/dsc_whatif.tests.ps1 +++ b/dsc/tests/dsc_whatif.tests.ps1 @@ -181,4 +181,12 @@ Describe 'whatif tests' { $out.results[0].result.afterState._exist | Should -BeFalse $out.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'whatIf' } + + It 'dsc resource delete supports what-if flag' { + $result = dsc resource delete -r Test/WhatIfDelete -i '{"_exist": false}' --what-if | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 + $result._metadata.whatIf | Should -Not -BeNullOrEmpty + $result._metadata.whatIf | Should -Contain 'Delete what-if message 1' + $result._metadata.whatIf | Should -Contain 'Delete what-if message 2' + } }