@@ -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
+ r. properties = escape_property_values ( & props) ?;
86
+ let mut properties = serde_json:: to_value ( & r. properties ) ?;
87
+ let mut metadata = Metadata {
88
+ microsoft : None ,
89
+ other : Map :: new ( ) ,
90
+ } ;
91
+ get_metadata_from_result ( & mut properties, & mut metadata) ?;
78
92
if let Some ( security_context) = props. remove ( "_securityContext" ) {
79
93
let context: SecurityContextKind = serde_json:: from_value ( security_context) ?;
80
- let metadata = Metadata {
81
- microsoft : Some (
94
+ metadata. microsoft = Some (
82
95
MicrosoftDscMetadata {
83
96
security_context : Some ( context) ,
84
97
..Default :: default ( )
85
98
}
86
- ) ,
87
- other : Map :: new ( ) ,
88
- } ;
89
- r. metadata = Some ( metadata) ;
99
+ ) ;
90
100
}
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 ( ) ) ) ?
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,20 @@ 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 {
331
+ duration : Some ( end_datetime. signed_duration_since ( start_datetime) . to_string ( ) ) ,
332
+ ..Default :: default ( )
333
+ }
334
+ ) ,
335
+ other : Map :: new ( ) ,
336
+ } ;
337
+
338
+ match & mut get_result {
339
+ GetResult :: Resource ( ref mut resource_result) => {
302
340
self . context . references . insert ( format ! ( "{}:{}" , resource. resource_type, resource. name) , serde_json:: to_value ( & resource_result. actual_state ) ?) ;
341
+ get_metadata_from_result ( & mut resource_result. actual_state , & mut metadata) ?;
303
342
} ,
304
343
GetResult :: Group ( group) => {
305
344
let mut results = Vec :: < Value > :: new ( ) ;
@@ -310,17 +349,7 @@ impl Configurator {
310
349
} ,
311
350
}
312
351
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
- ) ,
352
+ metadata : Some ( metadata) ,
324
353
name : resource. name . clone ( ) ,
325
354
resource_type : resource. resource_type . clone ( ) ,
326
355
result : get_result. clone ( ) ,
@@ -383,7 +412,7 @@ impl Configurator {
383
412
384
413
let start_datetime;
385
414
let end_datetime;
386
- let set_result;
415
+ let mut set_result;
387
416
if exist || dsc_resource. capabilities . contains ( & Capability :: SetHandlesExist ) {
388
417
debug ! ( "{}" , t!( "configure.mod.handlesExist" ) ) ;
389
418
start_datetime = chrono:: Local :: now ( ) ;
@@ -453,9 +482,19 @@ impl Configurator {
453
482
return Err ( DscError :: NotImplemented ( t ! ( "configure.mod.deleteNotSupported" , resource = resource. resource_type) . to_string ( ) ) ) ;
454
483
}
455
484
456
- match & set_result {
485
+ let mut metadata = Metadata {
486
+ microsoft : Some (
487
+ MicrosoftDscMetadata {
488
+ duration : Some ( end_datetime. signed_duration_since ( start_datetime) . to_string ( ) ) ,
489
+ ..Default :: default ( )
490
+ }
491
+ ) ,
492
+ other : Map :: new ( ) ,
493
+ } ;
494
+ match & mut set_result {
457
495
SetResult :: Resource ( resource_result) => {
458
496
self . context . references . insert ( format ! ( "{}:{}" , resource. resource_type, resource. name) , serde_json:: to_value ( & resource_result. after_state ) ?) ;
497
+ get_metadata_from_result ( & mut resource_result. after_state , & mut metadata) ?;
459
498
} ,
460
499
SetResult :: Group ( group) => {
461
500
let mut results = Vec :: < Value > :: new ( ) ;
@@ -466,17 +505,7 @@ impl Configurator {
466
505
} ,
467
506
}
468
507
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
- ) ,
508
+ metadata : Some ( metadata) ,
480
509
name : resource. name . clone ( ) ,
481
510
resource_type : resource. resource_type . clone ( ) ,
482
511
result : set_result. clone ( ) ,
@@ -517,7 +546,7 @@ impl Configurator {
517
546
let expected = add_metadata ( & dsc_resource. kind , properties) ?;
518
547
trace ! ( "{}" , t!( "configure.mod.expectedState" , state = expected) ) ;
519
548
let start_datetime = chrono:: Local :: now ( ) ;
520
- let test_result = match dsc_resource. test ( & expected) {
549
+ let mut test_result = match dsc_resource. test ( & expected) {
521
550
Ok ( result) => result,
522
551
Err ( e) => {
523
552
progress. set_failure ( get_failure_from_error ( & e) ) ;
@@ -526,9 +555,19 @@ impl Configurator {
526
555
} ,
527
556
} ;
528
557
let end_datetime = chrono:: Local :: now ( ) ;
529
- match & test_result {
558
+ let mut metadata = Metadata {
559
+ microsoft : Some (
560
+ MicrosoftDscMetadata {
561
+ duration : Some ( end_datetime. signed_duration_since ( start_datetime) . to_string ( ) ) ,
562
+ ..Default :: default ( )
563
+ }
564
+ ) ,
565
+ other : Map :: new ( ) ,
566
+ } ;
567
+ match & mut test_result {
530
568
TestResult :: Resource ( resource_test_result) => {
531
569
self . context . references . insert ( format ! ( "{}:{}" , resource. resource_type, resource. name) , serde_json:: to_value ( & resource_test_result. actual_state ) ?) ;
570
+ get_metadata_from_result ( & mut resource_test_result. actual_state , & mut metadata) ?;
532
571
} ,
533
572
TestResult :: Group ( group) => {
534
573
let mut results = Vec :: < Value > :: new ( ) ;
@@ -539,17 +578,7 @@ impl Configurator {
539
578
} ,
540
579
}
541
580
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
- ) ,
581
+ metadata : Some ( metadata) ,
553
582
name : resource. name . clone ( ) ,
554
583
resource_type : resource. resource_type . clone ( ) ,
555
584
result : test_result. clone ( ) ,
0 commit comments