11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4- use crate :: { discovery:: discovery_trait:: { DiscoveryFilter , DiscoveryKind , ResourceDiscovery } } ;
4+ use crate :: { discovery:: discovery_trait:: { DiscoveryFilter , DiscoveryKind , ResourceDiscovery } , parser :: Statement } ;
55use crate :: { locked_is_empty, locked_extend, locked_clone, locked_get} ;
6+ use crate :: configure:: context:: Context ;
67use crate :: dscresources:: dscresource:: { Capability , DscResource , ImplementedAs } ;
78use crate :: dscresources:: resource_manifest:: { import_manifest, validate_semver, Kind , ResourceManifest , SchemaKind } ;
89use crate :: dscresources:: command_resource:: invoke_command;
@@ -606,6 +607,18 @@ fn insert_resource(resources: &mut BTreeMap<String, Vec<DscResource>>, resource:
606607 }
607608}
608609
610+ fn evaluate_condition ( condition : Option < & str > ) -> Result < bool , DscError > {
611+ if let Some ( cond) = condition {
612+ let mut statement = Statement :: new ( ) ?;
613+ let result = statement. parse_and_execute ( cond, & Context :: new ( ) ) ?;
614+ if let Some ( bool_result) = result. as_bool ( ) {
615+ return Ok ( bool_result) ;
616+ }
617+ return Err ( DscError :: Validation ( t ! ( "discovery.commandDiscovery.conditionNotBoolean" , condition = cond) . to_string ( ) ) ) ;
618+ }
619+ Ok ( true )
620+ }
621+
609622/// Loads a manifest from the given path and returns a vector of `ImportedManifest`.
610623///
611624/// # Arguments
@@ -639,6 +652,10 @@ pub fn load_manifest(path: &Path) -> Result<Vec<ImportedManifest>, DscError> {
639652 }
640653 }
641654 } ;
655+ if !evaluate_condition ( manifest. condition . as_deref ( ) ) ? {
656+ debug ! ( "{}" , t!( "discovery.commandDiscovery.conditionNotMet" , path = path. to_string_lossy( ) , condition = manifest. condition. unwrap_or_default( ) ) ) ;
657+ return Ok ( vec ! [ ] ) ;
658+ }
642659 let resource = load_resource_manifest ( path, & manifest) ?;
643660 return Ok ( vec ! [ ImportedManifest :: Resource ( resource) ] ) ;
644661 }
@@ -658,10 +675,15 @@ pub fn load_manifest(path: &Path) -> Result<Vec<ImportedManifest>, DscError> {
658675 }
659676 }
660677 } ;
678+ if !evaluate_condition ( manifest. condition . as_deref ( ) ) ? {
679+ debug ! ( "{}" , t!( "discovery.commandDiscovery.conditionNotMet" , path = path. to_string_lossy( ) , condition = manifest. condition. unwrap_or_default( ) ) ) ;
680+ return Ok ( vec ! [ ] ) ;
681+ }
661682 let extension = load_extension_manifest ( path, & manifest) ?;
662683 return Ok ( vec ! [ ImportedManifest :: Extension ( extension) ] ) ;
663684 }
664685 if DSC_MANIFEST_LIST_EXTENSIONS . iter ( ) . any ( |ext| file_name_lowercase. ends_with ( ext) ) {
686+ let mut resources: Vec < ImportedManifest > = vec ! [ ] ;
665687 let manifest_list = if extension_is_json {
666688 match serde_json:: from_str :: < ManifestList > ( & contents) {
667689 Ok ( manifest) => manifest,
@@ -677,15 +699,22 @@ pub fn load_manifest(path: &Path) -> Result<Vec<ImportedManifest>, DscError> {
677699 }
678700 }
679701 } ;
680- let mut resources = vec ! [ ] ;
681702 if let Some ( resource_manifests) = & manifest_list. resources {
682703 for res_manifest in resource_manifests {
704+ if !evaluate_condition ( res_manifest. condition . as_deref ( ) ) ? {
705+ debug ! ( "{}" , t!( "discovery.commandDiscovery.conditionNotMet" , path = path. to_string_lossy( ) , condition = res_manifest. condition. as_ref( ) : { : ?} ) ) ;
706+ continue ;
707+ }
683708 let resource = load_resource_manifest ( path, res_manifest) ?;
684709 resources. push ( ImportedManifest :: Resource ( resource) ) ;
685710 }
686711 }
687712 if let Some ( extension_manifests) = & manifest_list. extensions {
688713 for ext_manifest in extension_manifests {
714+ if !evaluate_condition ( ext_manifest. condition . as_deref ( ) ) ? {
715+ debug ! ( "{}" , t!( "discovery.commandDiscovery.conditionNotMet" , path = path. to_string_lossy( ) , condition = ext_manifest. condition. as_ref( ) : { : ?} ) ) ;
716+ continue ;
717+ }
689718 let extension = load_extension_manifest ( path, ext_manifest) ?;
690719 resources. push ( ImportedManifest :: Extension ( extension) ) ;
691720 }
0 commit comments