@@ -31,7 +31,8 @@ pub mod depends_on;
31
31
pub mod parameters;
32
32
33
33
pub struct Configurator {
34
- config : String ,
34
+ json : String ,
35
+ config : Configuration ,
35
36
pub context : Context ,
36
37
discovery : Discovery ,
37
38
statement_parser : Statement ,
@@ -200,14 +201,26 @@ impl Configurator {
200
201
/// # Errors
201
202
///
202
203
/// This function will return an error if the configuration is invalid or the underlying discovery fails.
203
- pub fn new ( config : & str ) -> Result < Configurator , DscError > {
204
+ pub fn new ( json : & str ) -> Result < Configurator , DscError > {
204
205
let discovery = Discovery :: new ( ) ?;
205
- Ok ( Configurator {
206
- config : config. to_owned ( ) ,
206
+ let mut config = Configurator {
207
+ json : json. to_owned ( ) ,
208
+ config : Configuration :: new ( ) ,
207
209
context : Context :: new ( ) ,
208
210
discovery,
209
211
statement_parser : Statement :: new ( ) ?,
210
- } )
212
+ } ;
213
+ config. validate_config ( ) ?;
214
+ Ok ( config)
215
+ }
216
+
217
+ /// Get the configuration.
218
+ ///
219
+ /// # Returns
220
+ ///
221
+ /// * `&Configuration` - The configuration.
222
+ pub fn get_config ( & self ) -> & Configuration {
223
+ & self . config
211
224
}
212
225
213
226
/// Invoke the get operation on a resource.
@@ -220,9 +233,8 @@ impl Configurator {
220
233
///
221
234
/// This function will return an error if the underlying resource fails.
222
235
pub fn invoke_get ( & mut self ) -> Result < ConfigurationGetResult , DscError > {
223
- let config = self . validate_config ( ) ?;
224
236
let mut result = ConfigurationGetResult :: new ( ) ;
225
- let resources = get_resource_invocation_order ( & config, & mut self . statement_parser , & self . context ) ?;
237
+ let resources = get_resource_invocation_order ( & self . config , & mut self . statement_parser , & self . context ) ?;
226
238
let pb_span = get_progress_bar_span ( resources. len ( ) as u64 ) ?;
227
239
let pb_span_enter = pb_span. enter ( ) ;
228
240
for resource in resources {
@@ -279,9 +291,8 @@ impl Configurator {
279
291
///
280
292
/// This function will return an error if the underlying resource fails.
281
293
pub fn invoke_set ( & mut self , skip_test : bool ) -> Result < ConfigurationSetResult , DscError > {
282
- let config = self . validate_config ( ) ?;
283
294
let mut result = ConfigurationSetResult :: new ( ) ;
284
- let resources = get_resource_invocation_order ( & config, & mut self . statement_parser , & self . context ) ?;
295
+ let resources = get_resource_invocation_order ( & self . config , & mut self . statement_parser , & self . context ) ?;
285
296
let pb_span = get_progress_bar_span ( resources. len ( ) as u64 ) ?;
286
297
let pb_span_enter = pb_span. enter ( ) ;
287
298
for resource in resources {
@@ -388,9 +399,8 @@ impl Configurator {
388
399
///
389
400
/// This function will return an error if the underlying resource fails.
390
401
pub fn invoke_test ( & mut self ) -> Result < ConfigurationTestResult , DscError > {
391
- let config = self . validate_config ( ) ?;
392
402
let mut result = ConfigurationTestResult :: new ( ) ;
393
- let resources = get_resource_invocation_order ( & config, & mut self . statement_parser , & self . context ) ?;
403
+ let resources = get_resource_invocation_order ( & self . config , & mut self . statement_parser , & self . context ) ?;
394
404
let pb_span = get_progress_bar_span ( resources. len ( ) as u64 ) ?;
395
405
let pb_span_enter = pb_span. enter ( ) ;
396
406
for resource in resources {
@@ -443,14 +453,13 @@ impl Configurator {
443
453
///
444
454
/// This function will return an error if the underlying resource fails.
445
455
pub fn invoke_export ( & mut self ) -> Result < ConfigurationExportResult , DscError > {
446
- let config = self . validate_config ( ) ?;
447
-
448
456
let mut result = ConfigurationExportResult :: new ( ) ;
449
457
let mut conf = config_doc:: Configuration :: new ( ) ;
450
458
451
- let pb_span = get_progress_bar_span ( config. resources . len ( ) as u64 ) ?;
459
+ let pb_span = get_progress_bar_span ( self . config . resources . len ( ) as u64 ) ?;
452
460
let pb_span_enter = pb_span. enter ( ) ;
453
- for resource in config. resources {
461
+ let resources = self . config . resources . clone ( ) ;
462
+ for resource in & resources {
454
463
Span :: current ( ) . pb_inc ( 1 ) ;
455
464
pb_span. pb_set_message ( format ! ( "Export '{}'" , resource. name) . as_str ( ) ) ;
456
465
let properties = self . invoke_property_expressions ( & resource. properties ) ?;
@@ -480,7 +489,7 @@ impl Configurator {
480
489
/// This function will return an error if the parameters are invalid.
481
490
pub fn set_parameters ( & mut self , parameters_input : & Option < Value > ) -> Result < ( ) , DscError > {
482
491
// set default parameters first
483
- let config = serde_json:: from_str :: < Configuration > ( self . config . as_str ( ) ) ?;
492
+ let config = serde_json:: from_str :: < Configuration > ( self . json . as_str ( ) ) ?;
484
493
let Some ( parameters) = & config. parameters else {
485
494
if parameters_input. is_none ( ) {
486
495
debug ! ( "No parameters defined in configuration and no parameters input" ) ;
@@ -534,6 +543,12 @@ impl Configurator {
534
543
info ! ( "Set parameter '{name}' to '{value}'" ) ;
535
544
}
536
545
self . context . parameters . insert ( name. clone ( ) , ( value. clone ( ) , constraint. parameter_type . clone ( ) ) ) ;
546
+ // also update the configuration with the parameter value
547
+ if let Some ( parameters) = & mut self . config . parameters {
548
+ if let Some ( parameter) = parameters. get_mut ( & name) {
549
+ parameter. default_value = Some ( value) ;
550
+ }
551
+ }
537
552
}
538
553
else {
539
554
return Err ( DscError :: Validation ( format ! ( "Parameter '{name}' not defined in configuration" ) ) ) ;
@@ -592,14 +607,15 @@ impl Configurator {
592
607
Ok ( ( ) )
593
608
}
594
609
595
- fn validate_config ( & mut self ) -> Result < Configuration , DscError > {
596
- let config: Configuration = serde_json:: from_str ( self . config . as_str ( ) ) ?;
610
+ fn validate_config ( & mut self ) -> Result < ( ) , DscError > {
611
+ let config: Configuration = serde_json:: from_str ( self . json . as_str ( ) ) ?;
597
612
check_security_context ( & config. metadata ) ?;
598
613
599
614
// Perform discovery of resources used in config
600
615
let required_resources = config. resources . iter ( ) . map ( |p| p. resource_type . clone ( ) ) . collect :: < Vec < String > > ( ) ;
601
616
self . discovery . find_resources ( & required_resources) ;
602
- Ok ( config)
617
+ self . config = config;
618
+ Ok ( ( ) )
603
619
}
604
620
605
621
fn invoke_property_expressions ( & mut self , properties : & Option < Map < String , Value > > ) -> Result < Option < Map < String , Value > > , DscError > {
0 commit comments