@@ -24,7 +24,7 @@ use security_context_lib::{SecurityContext, get_security_context};
24
24
use serde_json:: { Map , Value } ;
25
25
use std:: path:: PathBuf ;
26
26
use std:: collections:: HashMap ;
27
- use tracing:: { debug, info, trace} ;
27
+ use tracing:: { debug, info, trace, warn } ;
28
28
pub mod context;
29
29
pub mod config_doc;
30
30
pub mod config_result;
@@ -75,27 +75,35 @@ pub fn add_resource_export_results_to_configuration(resource: &DscResource, conf
75
75
}
76
76
r. kind = kind. as_str ( ) . map ( std:: string:: ToString :: to_string) ;
77
77
}
78
+ r. name = if let Some ( name) = props. remove ( "_name" ) {
79
+ name. as_str ( )
80
+ . map ( std:: string:: ToString :: to_string)
81
+ . ok_or_else ( || DscError :: Parser ( t ! ( "configure.mod.propertyNotString" , name = "_name" , value = name) . to_string ( ) ) ) ?
82
+ } else {
83
+ format ! ( "{}-{i}" , r. resource_type)
84
+ } ;
85
+ let mut metadata = Metadata {
86
+ microsoft : None ,
87
+ other : Map :: new ( ) ,
88
+ } ;
78
89
if let Some ( security_context) = props. remove ( "_securityContext" ) {
79
90
let context: SecurityContextKind = serde_json:: from_value ( security_context) ?;
80
- let metadata = Metadata {
81
- microsoft : Some (
91
+ metadata. microsoft = Some (
82
92
MicrosoftDscMetadata {
83
93
security_context : Some ( context) ,
84
94
..Default :: default ( )
85
95
}
86
- ) ,
87
- other : Map :: new ( ) ,
88
- } ;
89
- r. metadata = Some ( metadata) ;
96
+ ) ;
90
97
}
91
- r. name = if let Some ( name) = props. remove ( "_name" ) {
92
- name. as_str ( )
93
- . map ( std:: string:: ToString :: to_string)
94
- . ok_or_else ( || DscError :: Parser ( t ! ( "configure.mod.propertyNotString" , name = "_name" , value = name) . to_string ( ) ) ) ?
98
+ r. properties = escape_property_values ( & props) ?;
99
+ let mut properties = serde_json:: to_value ( & r. properties ) ?;
100
+ get_metadata_from_result ( & mut properties, & mut metadata) ?;
101
+ r. properties = Some ( properties. as_object ( ) . cloned ( ) . unwrap_or_default ( ) ) ;
102
+ r. metadata = if metadata. microsoft . is_some ( ) || !metadata. other . is_empty ( ) {
103
+ Some ( metadata)
95
104
} else {
96
- format ! ( "{}-{i}" , r . resource_type )
105
+ None
97
106
} ;
98
- r. properties = escape_property_values ( & props) ?;
99
107
100
108
conf. resources . push ( r) ;
101
109
}
@@ -217,6 +225,26 @@ fn check_security_context(metadata: Option<&Metadata>) -> Result<(), DscError> {
217
225
Ok ( ( ) )
218
226
}
219
227
228
+ fn get_metadata_from_result ( result : & mut Value , metadata : & mut Metadata ) -> Result < ( ) , DscError > {
229
+ if let Some ( metadata_value) = result. get ( "_metadata" ) {
230
+ if let Some ( metadata_map) = metadata_value. as_object ( ) {
231
+ for ( key, value) in metadata_map {
232
+ if key. starts_with ( "Microsoft.DSC" ) {
233
+ warn ! ( "{}" , t!( "configure.mod.metadataMicrosoftDscIgnored" , key = key) ) ;
234
+ continue ;
235
+ }
236
+ metadata. other . insert ( key. clone ( ) , value. clone ( ) ) ;
237
+ }
238
+ } else {
239
+ return Err ( DscError :: Parser ( t ! ( "configure.mod.metadataNotObject" , value = metadata_value) . to_string ( ) ) ) ;
240
+ }
241
+ if let Some ( value_map) = result. as_object_mut ( ) {
242
+ value_map. remove ( "_metadata" ) ;
243
+ }
244
+ }
245
+ Ok ( ( ) )
246
+ }
247
+
220
248
impl Configurator {
221
249
/// Create a new `Configurator` instance.
222
250
///
@@ -288,7 +316,7 @@ impl Configurator {
288
316
let filter = add_metadata ( & dsc_resource. kind , properties) ?;
289
317
trace ! ( "filter: {filter}" ) ;
290
318
let start_datetime = chrono:: Local :: now ( ) ;
291
- let get_result = match dsc_resource. get ( & filter) {
319
+ let mut get_result = match dsc_resource. get ( & filter) {
292
320
Ok ( result) => result,
293
321
Err ( e) => {
294
322
progress. set_failure ( get_failure_from_error ( & e) ) ;
@@ -297,9 +325,17 @@ impl Configurator {
297
325
} ,
298
326
} ;
299
327
let end_datetime = chrono:: Local :: now ( ) ;
300
- match & get_result {
301
- GetResult :: Resource ( resource_result) => {
328
+ let mut metadata = Metadata {
329
+ microsoft : Some (
330
+ MicrosoftDscMetadata :: new_with_duration ( & start_datetime, & end_datetime)
331
+ ) ,
332
+ other : Map :: new ( ) ,
333
+ } ;
334
+
335
+ match & mut get_result {
336
+ GetResult :: Resource ( ref mut resource_result) => {
302
337
self . context . references . insert ( format ! ( "{}:{}" , resource. resource_type, resource. name) , serde_json:: to_value ( & resource_result. actual_state ) ?) ;
338
+ get_metadata_from_result ( & mut resource_result. actual_state , & mut metadata) ?;
303
339
} ,
304
340
GetResult :: Group ( group) => {
305
341
let mut results = Vec :: < Value > :: new ( ) ;
@@ -310,17 +346,7 @@ impl Configurator {
310
346
} ,
311
347
}
312
348
let resource_result = config_result:: ResourceGetResult {
313
- metadata : Some (
314
- Metadata {
315
- microsoft : Some (
316
- MicrosoftDscMetadata {
317
- duration : Some ( end_datetime. signed_duration_since ( start_datetime) . to_string ( ) ) ,
318
- ..Default :: default ( )
319
- }
320
- ) ,
321
- other : Map :: new ( ) ,
322
- }
323
- ) ,
349
+ metadata : Some ( metadata) ,
324
350
name : resource. name . clone ( ) ,
325
351
resource_type : resource. resource_type . clone ( ) ,
326
352
result : get_result. clone ( ) ,
@@ -383,7 +409,7 @@ impl Configurator {
383
409
384
410
let start_datetime;
385
411
let end_datetime;
386
- let set_result;
412
+ let mut set_result;
387
413
if exist || dsc_resource. capabilities . contains ( & Capability :: SetHandlesExist ) {
388
414
debug ! ( "{}" , t!( "configure.mod.handlesExist" ) ) ;
389
415
start_datetime = chrono:: Local :: now ( ) ;
@@ -453,9 +479,16 @@ impl Configurator {
453
479
return Err ( DscError :: NotImplemented ( t ! ( "configure.mod.deleteNotSupported" , resource = resource. resource_type) . to_string ( ) ) ) ;
454
480
}
455
481
456
- match & set_result {
482
+ let mut metadata = Metadata {
483
+ microsoft : Some (
484
+ MicrosoftDscMetadata :: new_with_duration ( & start_datetime, & end_datetime)
485
+ ) ,
486
+ other : Map :: new ( ) ,
487
+ } ;
488
+ match & mut set_result {
457
489
SetResult :: Resource ( resource_result) => {
458
490
self . context . references . insert ( format ! ( "{}:{}" , resource. resource_type, resource. name) , serde_json:: to_value ( & resource_result. after_state ) ?) ;
491
+ get_metadata_from_result ( & mut resource_result. after_state , & mut metadata) ?;
459
492
} ,
460
493
SetResult :: Group ( group) => {
461
494
let mut results = Vec :: < Value > :: new ( ) ;
@@ -466,17 +499,7 @@ impl Configurator {
466
499
} ,
467
500
}
468
501
let resource_result = config_result:: ResourceSetResult {
469
- metadata : Some (
470
- Metadata {
471
- microsoft : Some (
472
- MicrosoftDscMetadata {
473
- duration : Some ( end_datetime. signed_duration_since ( start_datetime) . to_string ( ) ) ,
474
- ..Default :: default ( )
475
- }
476
- ) ,
477
- other : Map :: new ( ) ,
478
- }
479
- ) ,
502
+ metadata : Some ( metadata) ,
480
503
name : resource. name . clone ( ) ,
481
504
resource_type : resource. resource_type . clone ( ) ,
482
505
result : set_result. clone ( ) ,
@@ -517,7 +540,7 @@ impl Configurator {
517
540
let expected = add_metadata ( & dsc_resource. kind , properties) ?;
518
541
trace ! ( "{}" , t!( "configure.mod.expectedState" , state = expected) ) ;
519
542
let start_datetime = chrono:: Local :: now ( ) ;
520
- let test_result = match dsc_resource. test ( & expected) {
543
+ let mut test_result = match dsc_resource. test ( & expected) {
521
544
Ok ( result) => result,
522
545
Err ( e) => {
523
546
progress. set_failure ( get_failure_from_error ( & e) ) ;
@@ -526,9 +549,16 @@ impl Configurator {
526
549
} ,
527
550
} ;
528
551
let end_datetime = chrono:: Local :: now ( ) ;
529
- match & test_result {
552
+ let mut metadata = Metadata {
553
+ microsoft : Some (
554
+ MicrosoftDscMetadata :: new_with_duration ( & start_datetime, & end_datetime)
555
+ ) ,
556
+ other : Map :: new ( ) ,
557
+ } ;
558
+ match & mut test_result {
530
559
TestResult :: Resource ( resource_test_result) => {
531
560
self . context . references . insert ( format ! ( "{}:{}" , resource. resource_type, resource. name) , serde_json:: to_value ( & resource_test_result. actual_state ) ?) ;
561
+ get_metadata_from_result ( & mut resource_test_result. actual_state , & mut metadata) ?;
532
562
} ,
533
563
TestResult :: Group ( group) => {
534
564
let mut results = Vec :: < Value > :: new ( ) ;
@@ -539,17 +569,7 @@ impl Configurator {
539
569
} ,
540
570
}
541
571
let resource_result = config_result:: ResourceTestResult {
542
- metadata : Some (
543
- Metadata {
544
- microsoft : Some (
545
- MicrosoftDscMetadata {
546
- duration : Some ( end_datetime. signed_duration_since ( start_datetime) . to_string ( ) ) ,
547
- ..Default :: default ( )
548
- }
549
- ) ,
550
- other : Map :: new ( ) ,
551
- }
552
- ) ,
572
+ metadata : Some ( metadata) ,
553
573
name : resource. name . clone ( ) ,
554
574
resource_type : resource. resource_type . clone ( ) ,
555
575
result : test_result. clone ( ) ,
0 commit comments