diff --git a/dsc_lib/src/configure/mod.rs b/dsc_lib/src/configure/mod.rs index af7c9e2de..3039e581a 100644 --- a/dsc_lib/src/configure/mod.rs +++ b/dsc_lib/src/configure/mod.rs @@ -405,12 +405,18 @@ impl Configurator { let GetResult::Resource(after_result) = after_result else { return Err(DscError::NotSupported(t!("configure.mod.groupNotSupportedForDelete").to_string())) }; - let before_value = serde_json::to_value(&before_response.actual_state)?; - let after_value = serde_json::to_value(&after_result.actual_state)?; + let diff = get_diff(&before_response.actual_state, &after_result.actual_state); + let mut before: Map = serde_json::from_value(before_response.actual_state)?; + // a `get` will return a `result` property, but an actual `set` will have that as `resources` + if before.contains_key("result") && !before.contains_key("resources") { + before.insert("resources".to_string() ,before["result"].clone()); + before.remove("result"); + } + let before_value = serde_json::to_value(&before)?; SetResult::Resource(ResourceSetResponse { - before_state: before_response.actual_state, + before_state: before_value.clone(), after_state: after_result.actual_state, - changed_properties: Some(get_diff(&before_value, &after_value)), + changed_properties: Some(diff), }) }, GetResult::Group(_) => { diff --git a/dsc_lib/src/dscresources/command_resource.rs b/dsc_lib/src/dscresources/command_resource.rs index 2ee3308be..83474eee3 100644 --- a/dsc_lib/src/dscresources/command_resource.rs +++ b/dsc_lib/src/dscresources/command_resource.rs @@ -5,7 +5,7 @@ use clap::ValueEnum; use jsonschema::Validator; use rust_i18n::t; use serde::Deserialize; -use serde_json::Value; +use serde_json::{Map, Value}; use std::{collections::HashMap, env, process::Stdio}; use crate::configure::{config_doc::ExecutionKind, config_result::{ResourceGetResult, ResourceTestResult}}; use crate::dscerror::DscError; @@ -146,12 +146,24 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te verify_json(resource, cwd, &stdout)?; } - let pre_state: Value = if exit_code == 0 { + let pre_state_value: Value = if exit_code == 0 { serde_json::from_str(&stdout)? } else { return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr)); }; + let pre_state = if pre_state_value.is_object() { + let mut pre_state_map: Map = serde_json::from_value(pre_state_value)?; + + // if the resource is an adapter, then the `get` will return a `result`, but a full `set` expects the before state to be `resources` + if resource.kind == Some(Kind::Adapter) && pre_state_map.contains_key("result") && !pre_state_map.contains_key("resources") { + pre_state_map.insert("resources".to_string(), pre_state_map["result"].clone()); + pre_state_map.remove("result"); + } + serde_json::to_value(pre_state_map)? + } else { + pre_state_value + }; let mut env: Option> = None; let mut input_desired: Option<&str> = None; diff --git a/dsc_lib/src/dscresources/dscresource.rs b/dsc_lib/src/dscresources/dscresource.rs index d69d4c8f1..644dc1526 100644 --- a/dsc_lib/src/dscresources/dscresource.rs +++ b/dsc_lib/src/dscresources/dscresource.rs @@ -259,7 +259,7 @@ impl Invoke for DscResource { }; let before_state = resource_result.before_state .as_object().ok_or(DscError::Operation(t!("dscresources.dscresource.propertyIncorrectType", property = "beforeState", property_type = "object").to_string()))? - .get("result").ok_or(DscError::Operation(t!("dscresources.dscresource.propertyNotFound", property = "result").to_string()))? + .get("resources").ok_or(DscError::Operation(t!("dscresources.dscresource.propertyNotFound", property = "resources").to_string()))? .as_array().ok_or(DscError::Operation(t!("dscresources.dscresource.propertyIncorrectType", property = "result", property_type = "array").to_string()))?[0] .as_object().ok_or(DscError::Operation(t!("dscresources.dscresource.propertyIncorrectType", property = "result", property_type = "object").to_string()))? .get("properties").ok_or(DscError::Operation(t!("dscresources.dscresource.propertyNotFound", property = "properties").to_string()))?.clone(); diff --git a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 index 9cdfdb264..649365100 100644 --- a/powershell-adapter/Tests/powershellgroup.config.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.config.tests.ps1 @@ -221,9 +221,10 @@ Describe 'PowerShell adapter resource tests' { Name: 'DSCv3' "@ - $out = dsc config $operation -i $yaml | ConvertFrom-Json - $text = dsc config $operation -i $yaml | Out-String - $LASTEXITCODE | Should -Be 0 + $out = dsc -l trace config $operation -i $yaml 2> $TestDrive/tracing.txt + $text = $out | Out-String + $out = $out | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw -Path $TestDrive/tracing.txt) switch ($Operation) { 'get' { $out.results[0].result.actualState.Name | Should -BeExactly 'TestClassResource1' -Because $text