Skip to content

Commit 776a45d

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
if adapter required, construct dynamic config
1 parent d769405 commit 776a45d

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

dsc_lib/locales/en-us.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ parameterNotArray = "Parameter '%{name}' is not an array"
6767
parameterNotObject = "Parameter '%{name}' is not an object"
6868
invokePropertyExpressions = "Invoke property expressions"
6969
invokeExpression = "Invoke property expression for %{name}: %{value}"
70+
adapterNotFound = "Adapter '%{adapter}' not found for resource '%{resource}'"
7071

7172
[discovery.commandDiscovery]
7273
couldNotReadSetting = "Could not read 'resourcePath' setting"

dsc_lib/src/dscresources/dscresource.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use crate::{configure::config_doc::ExecutionKind, dscresources::resource_manifest::Kind};
4+
use crate::{configure::{config_doc::{Configuration, ExecutionKind, Resource}, Configurator}, dscresources::resource_manifest::Kind};
55
use dscerror::DscError;
66
use rust_i18n::t;
77
use schemars::JsonSchema;
88
use serde::{Deserialize, Serialize};
9-
use serde_json::Value;
9+
use serde_json::{Map, Value};
1010
use std::collections::HashMap;
1111
use tracing::{debug, info};
1212

@@ -191,6 +191,31 @@ pub trait Invoke {
191191
impl Invoke for DscResource {
192192
fn get(&self, filter: &str) -> Result<GetResult, DscError> {
193193
debug!("{}", t!("dscresources.dscresource.invokeGet", resource = self.type_name));
194+
if let Some(adapter) = &self.require_adapter {
195+
// create new configuration with adapter and use this as the resource
196+
let mut configuration = Configuration::new();
197+
let mut property_map = Map::new();
198+
property_map.insert("name".to_string(), Value::String(self.type_name.clone()));
199+
property_map.insert("type".to_string(), Value::String(self.type_name.clone()));
200+
let mut resource_properties = Map::new();
201+
for property in &self.properties {
202+
resource_properties.insert(property.clone(), Value::Null);
203+
}
204+
property_map.insert("properties".to_string(), Value::Object(resource_properties));
205+
let adapter_resource = Resource {
206+
name: self.type_name.clone(),
207+
resource_type: adapter.clone(),
208+
depends_on: None,
209+
metadata: None,
210+
properties: Some(property_map),
211+
};
212+
configuration.resources.push(adapter_resource);
213+
let config_json = serde_json::to_string(&configuration)?;
214+
let mut configurator = Configurator::new(&config_json, crate::progress::ProgressFormat::None)?;
215+
let result = configurator.invoke_get()?;
216+
return Ok(result.results[0].result.clone());
217+
}
218+
194219
match &self.implemented_as {
195220
ImplementedAs::Custom(_custom) => {
196221
Err(DscError::NotImplemented(t!("dscresources.dscresource.customResourceNotSupported").to_string()))

powershell-adapter/psDscAdapter/powershell.resource.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ switch ($Operation) {
153153
# check if all the desired modules are in the cache
154154
$moduleInput | ForEach-Object {
155155
if ($dscResourceCache.type -notcontains $_) {
156-
('DSC resource {0} module not found.' -f $_) | Write-DscTrace -Operation Error
156+
("DSC resource '{0}' module not found." -f $_) | Write-DscTrace -Operation Error
157157
exit 1
158158
}
159159
}

0 commit comments

Comments
 (0)