2
2
// Licensed under the MIT License.
3
3
4
4
use crate :: { configure:: { config_doc:: { Configuration , ExecutionKind , Resource } , Configurator } , dscresources:: resource_manifest:: Kind } ;
5
- use crate :: dscresources:: invoke_result:: ResourceGetResponse ;
5
+ use crate :: dscresources:: invoke_result:: { ResourceGetResponse , ResourceSetResponse } ;
6
6
use dscerror:: DscError ;
7
7
use rust_i18n:: t;
8
8
use schemars:: JsonSchema ;
@@ -219,10 +219,10 @@ impl Invoke for DscResource {
219
219
let mut configurator = self . clone ( ) . create_config_for_adapter ( adapter, filter) ?;
220
220
let result = configurator. invoke_get ( ) ?;
221
221
let GetResult :: Resource ( ref resource_result) = result. results [ 0 ] . result else {
222
- return Err ( DscError :: Operation ( t ! ( "dscresources.dscresource.invokeGetReturnedNoResult " , resource = self . type_name) . to_string ( ) ) ) ;
222
+ return Err ( DscError :: Operation ( t ! ( "dscresources.dscresource.invokeReturnedWrongResult" , operation = "get ", resource = self . type_name) . to_string ( ) ) ) ;
223
223
} ;
224
224
let properties = resource_result. actual_state
225
- . as_object ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "actual_state " , r#type = "object" ) . to_string ( ) ) ) ?
225
+ . as_object ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "actualState " , r#type = "object" ) . to_string ( ) ) ) ?
226
226
. get ( "result" ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyNotFound" , property = "result" ) . to_string ( ) ) ) ?
227
227
. as_array ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "result" , r#type = "array" ) . to_string ( ) ) ) ?[ 0 ]
228
228
. as_object ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "result" , r#type = "object" ) . to_string ( ) ) ) ?
@@ -252,7 +252,28 @@ impl Invoke for DscResource {
252
252
if let Some ( adapter) = & self . require_adapter {
253
253
let mut configurator = self . clone ( ) . create_config_for_adapter ( adapter, desired) ?;
254
254
let result = configurator. invoke_set ( false ) ?;
255
- return Ok ( result. results [ 0 ] . result . clone ( ) ) ;
255
+ let SetResult :: Resource ( ref resource_result) = result. results [ 0 ] . result else {
256
+ return Err ( DscError :: Operation ( t ! ( "dscresources.dscresource.invokeReturnedWrongResult" , operation = "set" , resource = self . type_name) . to_string ( ) ) ) ;
257
+ } ;
258
+ let before_state = resource_result. before_state
259
+ . as_object ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "beforeState" , r#type = "object" ) . to_string ( ) ) ) ?
260
+ . get ( "result" ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyNotFound" , property = "result" ) . to_string ( ) ) ) ?
261
+ . as_array ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "result" , r#type = "array" ) . to_string ( ) ) ) ?[ 0 ]
262
+ . as_object ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "result" , r#type = "object" ) . to_string ( ) ) ) ?
263
+ . get ( "properties" ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyNotFound" , property = "properties" ) . to_string ( ) ) ) ?. clone ( ) ;
264
+ let after_state = resource_result. after_state
265
+ . as_object ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "afterState" , r#type = "object" ) . to_string ( ) ) ) ?
266
+ . get ( "result" ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyNotFound" , property = "result" ) . to_string ( ) ) ) ?
267
+ . as_array ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "result" , r#type = "array" ) . to_string ( ) ) ) ?[ 0 ]
268
+ . as_object ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "result" , r#type = "object" ) . to_string ( ) ) ) ?
269
+ . get ( "properties" ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyNotFound" , property = "properties" ) . to_string ( ) ) ) ?. clone ( ) ;
270
+ let diff = get_diff ( & before_state, & after_state) ;
271
+ let set_result = SetResult :: Resource ( ResourceSetResponse {
272
+ before_state : before_state. clone ( ) ,
273
+ after_state : after_state. clone ( ) ,
274
+ changed_properties : if diff. is_empty ( ) { None } else { Some ( diff) } ,
275
+ } ) ;
276
+ return Ok ( set_result) ;
256
277
}
257
278
258
279
match & self . implemented_as {
@@ -274,7 +295,29 @@ impl Invoke for DscResource {
274
295
if let Some ( adapter) = & self . require_adapter {
275
296
let mut configurator = self . clone ( ) . create_config_for_adapter ( adapter, expected) ?;
276
297
let result = configurator. invoke_test ( ) ?;
277
- return Ok ( result. results [ 0 ] . result . clone ( ) ) ;
298
+ let TestResult :: Resource ( ref resource_result) = result. results [ 0 ] . result else {
299
+ return Err ( DscError :: Operation ( t ! ( "dscresources.dscresource.invokeReturnedWrongResult" , operation = "test" , resource = self . type_name) . to_string ( ) ) ) ;
300
+ } ;
301
+ let desired_state = resource_result. desired_state
302
+ . as_object ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "desiredState" , r#type = "object" ) . to_string ( ) ) ) ?
303
+ . get ( "resources" ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyNotFound" , property = "resources" ) . to_string ( ) ) ) ?
304
+ . as_array ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "resources" , r#type = "array" ) . to_string ( ) ) ) ?[ 0 ]
305
+ . as_object ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "resources" , r#type = "object" ) . to_string ( ) ) ) ?
306
+ . get ( "properties" ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyNotFound" , property = "properties" ) . to_string ( ) ) ) ?. clone ( ) ;
307
+ let actual_state = resource_result. actual_state
308
+ . as_object ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "actualState" , r#type = "object" ) . to_string ( ) ) ) ?
309
+ . get ( "result" ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyNotFound" , property = "result" ) . to_string ( ) ) ) ?
310
+ . as_array ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "result" , r#type = "array" ) . to_string ( ) ) ) ?[ 0 ]
311
+ . as_object ( ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyIncorrectType" , property = "result" , r#type = "object" ) . to_string ( ) ) ) ?
312
+ . get ( "properties" ) . ok_or ( DscError :: Operation ( t ! ( "dscresources.dscresource.propertyNotFound" , property = "properties" ) . to_string ( ) ) ) ?. clone ( ) ;
313
+ let diff_properties = get_diff ( & desired_state, & actual_state) ;
314
+ let test_result = TestResult :: Resource ( ResourceTestResponse {
315
+ desired_state,
316
+ actual_state,
317
+ in_desired_state : resource_result. in_desired_state ,
318
+ diff_properties,
319
+ } ) ;
320
+ return Ok ( test_result) ;
278
321
}
279
322
280
323
match & self . implemented_as {
0 commit comments