11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4- use crate :: configure:: config_doc:: { ExecutionKind , Metadata , Resource , Parameter } ;
54use crate :: configure:: context:: { Context , ProcessMode } ;
6- use crate :: configure:: { config_doc:: { IntOrExpression , RestartRequired } , parameters:: Input } ;
5+ use crate :: configure:: { config_doc:: { ExecutionKind , IntOrExpression , Metadata , Parameter , Resource , RestartRequired , ValueOrCopy } , parameters:: Input } ;
76use crate :: discovery:: discovery_trait:: DiscoveryFilter ;
87use crate :: dscerror:: DscError ;
98use crate :: dscresources:: {
@@ -414,6 +413,10 @@ impl Configurator {
414413 result. metadata = Some (
415414 self . get_result_metadata ( Operation :: Get )
416415 ) ;
416+ self . process_output ( ) ?;
417+ if !self . context . outputs . is_empty ( ) {
418+ result. outputs = Some ( self . context . outputs . clone ( ) ) ;
419+ }
417420 Ok ( result)
418421 }
419422
@@ -585,6 +588,10 @@ impl Configurator {
585588 result. metadata = Some (
586589 self . get_result_metadata ( Operation :: Set )
587590 ) ;
591+ self . process_output ( ) ?;
592+ if !self . context . outputs . is_empty ( ) {
593+ result. outputs = Some ( self . context . outputs . clone ( ) ) ;
594+ }
588595 Ok ( result)
589596 }
590597
@@ -663,6 +670,10 @@ impl Configurator {
663670 result. metadata = Some (
664671 self . get_result_metadata ( Operation :: Test )
665672 ) ;
673+ self . process_output ( ) ?;
674+ if !self . context . outputs . is_empty ( ) {
675+ result. outputs = Some ( self . context . outputs . clone ( ) ) ;
676+ }
666677 Ok ( result)
667678 }
668679
@@ -725,6 +736,10 @@ impl Configurator {
725736 }
726737
727738 result. result = Some ( conf) ;
739+ self . process_output ( ) ?;
740+ if !self . context . outputs . is_empty ( ) {
741+ result. outputs = Some ( self . context . outputs . clone ( ) ) ;
742+ }
728743 Ok ( result)
729744 }
730745
@@ -739,6 +754,48 @@ impl Configurator {
739754 Ok ( false )
740755 }
741756
757+ /// Process the outputs defined in the configuration.
758+ ///
759+ /// # Errors
760+ ///
761+ /// This function will return an error if the output processing fails.
762+ pub fn process_output ( & mut self ) -> Result < ( ) , DscError > {
763+ if self . config . outputs . is_none ( ) || self . context . execution_type == ExecutionKind :: WhatIf {
764+ return Ok ( ( ) ) ;
765+ }
766+ if let Some ( outputs) = & self . config . outputs {
767+ for ( name, output) in outputs {
768+ if let Some ( condition) = & output. condition {
769+ let condition_result = self . statement_parser . parse_and_execute ( condition, & self . context ) ?;
770+ if condition_result != Value :: Bool ( true ) {
771+ info ! ( "{}" , t!( "configure.mod.skippingOutput" , name = name) ) ;
772+ continue ;
773+ }
774+ }
775+
776+ if let ValueOrCopy :: Value ( value) = & output. value_or_copy {
777+ let value_result = self . statement_parser . parse_and_execute ( value, & self . context ) ?;
778+ if output. r#type == DataType :: SecureString || output. r#type == DataType :: SecureObject {
779+ warn ! ( "{}" , t!( "configure.mod.secureOutputSkipped" , name = name) ) ;
780+ continue ;
781+ }
782+ // TODO: handle nullable when supported
783+ if value_result. is_string ( ) && output. r#type != DataType :: String ||
784+ value_result. is_i64 ( ) && output. r#type != DataType :: Int ||
785+ value_result. is_boolean ( ) && output. r#type != DataType :: Bool ||
786+ value_result. is_array ( ) && output. r#type != DataType :: Array ||
787+ value_result. is_object ( ) && output. r#type != DataType :: Object {
788+ return Err ( DscError :: Validation ( t ! ( "configure.mod.outputTypeNotMatch" , name = name, expected_type = output. r#type) . to_string ( ) ) ) ;
789+ }
790+ self . context . outputs . insert ( name. clone ( ) , value_result) ;
791+ } else {
792+ warn ! ( "{}" , t!( "configure.mod.copyNotSupported" , name = name) ) ;
793+ }
794+ }
795+ }
796+ Ok ( ( ) )
797+ }
798+
742799 /// Set the mounted path for the configuration.
743800 ///
744801 /// # Arguments
0 commit comments