Skip to content

Commit 85ea8a3

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
get working
1 parent 1c6884e commit 85ea8a3

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

dsc_lib/locales/en-us.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ invokeExport = "Invoking export for '%{resource}'"
147147
invokeExportReturnedNoResult = "Invoking export returned no result for '%{resource}'"
148148
invokeResolve = "Invoking resolve for '%{resource}'"
149149
invokeResolveNotSupported = "Invoking resolve is not supported for adapted resource '%{resource}'"
150+
invokeReturnedWrongResult = "Invoking '%{operation}' on '%{resource}' returned unexpected result"
151+
propertyIncorrectType = "Property '%{property}' is not of type '%{type}'"
152+
propertyNotFound = "Property '%{property}' not found"
150153
subDiff = "diff: sub diff for '%{key}'"
151154
diffArray = "diff: arrays differ for '%{key}'"
152155
diffNotArray = "diff: '%{key}' is not an array"

dsc_lib/src/dscresources/dscresource.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
use crate::{configure::{config_doc::{Configuration, ExecutionKind, Resource}, Configurator}, dscresources::resource_manifest::Kind};
5+
use crate::dscresources::invoke_result::ResourceGetResponse;
56
use dscerror::DscError;
67
use rust_i18n::t;
78
use schemars::JsonSchema;
@@ -93,19 +94,16 @@ impl DscResource {
9394
}
9495
}
9596

96-
fn create_config_for_adapter(self, adapter: &str) -> Result<Configurator, DscError> {
97+
fn create_config_for_adapter(self, adapter: &str, input: &str) -> Result<Configurator, DscError> {
9798
// create new configuration with adapter and use this as the resource
9899
let mut configuration = Configuration::new();
99100
let mut property_map = Map::new();
100101
property_map.insert("name".to_string(), Value::String(self.type_name.clone()));
101102
property_map.insert("type".to_string(), Value::String(self.type_name.clone()));
102-
let mut resource_properties = Map::new();
103-
for property in &self.properties {
104-
resource_properties.insert(property.clone(), Value::Null);
105-
}
103+
let resource_properties: Value = serde_json::from_str(input)?;
106104
let mut resources_map = Map::new();
107-
property_map.insert("properties".to_string(), Value::Object(resource_properties));
108-
resources_map.insert("resources".to_string(), Value::Object(property_map));
105+
property_map.insert("properties".to_string(), resource_properties);
106+
resources_map.insert("resources".to_string(), Value::Array(vec![Value::Object(property_map)]));
109107
let adapter_resource = Resource {
110108
name: self.type_name.clone(),
111109
resource_type: adapter.to_string(),
@@ -218,9 +216,21 @@ impl Invoke for DscResource {
218216
fn get(&self, filter: &str) -> Result<GetResult, DscError> {
219217
debug!("{}", t!("dscresources.dscresource.invokeGet", resource = self.type_name));
220218
if let Some(adapter) = &self.require_adapter {
221-
let mut configurator = self.clone().create_config_for_adapter(adapter)?;
219+
let mut configurator = self.clone().create_config_for_adapter(adapter, filter)?;
222220
let result = configurator.invoke_get()?;
223-
return Ok(result.results[0].result.clone());
221+
let GetResult::Resource(ref resource_result) = result.results[0].result else {
222+
return Err(DscError::Operation(t!("dscresources.dscresource.invokeGetReturnedNoResult", resource = self.type_name).to_string()));
223+
};
224+
let properties = resource_result.actual_state
225+
.as_object().ok_or(DscError::Operation(t!("dscresources.dscresource.propertyIncorrectType", property = "actual_state", r#type = "object").to_string()))?
226+
.get("result").ok_or(DscError::Operation(t!("dscresources.dscresource.propertyNotFound", property = "result").to_string()))?
227+
.as_array().ok_or(DscError::Operation(t!("dscresources.dscresource.propertyIncorrectType", property = "result", r#type = "array").to_string()))?[0]
228+
.as_object().ok_or(DscError::Operation(t!("dscresources.dscresource.propertyIncorrectType", property = "result", r#type = "object").to_string()))?
229+
.get("properties").ok_or(DscError::Operation(t!("dscresources.dscresource.propertyNotFound", property = "properties").to_string()))?.clone();
230+
let get_result = GetResult::Resource(ResourceGetResponse {
231+
actual_state: properties.clone(),
232+
});
233+
return Ok(get_result);
224234
}
225235

226236
match &self.implemented_as {
@@ -240,7 +250,7 @@ impl Invoke for DscResource {
240250
fn set(&self, desired: &str, skip_test: bool, execution_type: &ExecutionKind) -> Result<SetResult, DscError> {
241251
debug!("{}", t!("dscresources.dscresource.invokeSet", resource = self.type_name));
242252
if let Some(adapter) = &self.require_adapter {
243-
let mut configurator = self.clone().create_config_for_adapter(adapter)?;
253+
let mut configurator = self.clone().create_config_for_adapter(adapter, desired)?;
244254
let result = configurator.invoke_set(false)?;
245255
return Ok(result.results[0].result.clone());
246256
}
@@ -262,7 +272,7 @@ impl Invoke for DscResource {
262272
fn test(&self, expected: &str) -> Result<TestResult, DscError> {
263273
debug!("{}", t!("dscresources.dscresource.invokeTest", resource = self.type_name));
264274
if let Some(adapter) = &self.require_adapter {
265-
let mut configurator = self.clone().create_config_for_adapter(adapter)?;
275+
let mut configurator = self.clone().create_config_for_adapter(adapter, expected)?;
266276
let result = configurator.invoke_test()?;
267277
return Ok(result.results[0].result.clone());
268278
}
@@ -312,7 +322,7 @@ impl Invoke for DscResource {
312322
fn delete(&self, filter: &str) -> Result<(), DscError> {
313323
debug!("{}", t!("dscresources.dscresource.invokeDelete", resource = self.type_name));
314324
if let Some(adapter) = &self.require_adapter {
315-
let mut configurator = self.clone().create_config_for_adapter(adapter)?;
325+
let mut configurator = self.clone().create_config_for_adapter(adapter, filter)?;
316326
configurator.invoke_set(false)?;
317327
return Ok(());
318328
}
@@ -374,7 +384,7 @@ impl Invoke for DscResource {
374384
fn export(&self, input: &str) -> Result<ExportResult, DscError> {
375385
debug!("{}", t!("dscresources.dscresource.invokeExport", resource = self.type_name));
376386
if let Some(adapter) = &self.require_adapter {
377-
let mut configurator = self.clone().create_config_for_adapter(adapter)?;
387+
let mut configurator = self.clone().create_config_for_adapter(adapter, input)?;
378388
let result = configurator.invoke_export()?;
379389
let Some(configuration) = result.result else {
380390
return Err(DscError::Operation(t!("dscresources.dscresource.invokeExportReturnedNoResult", resource = self.type_name).to_string()));

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function Invoke-DscCacheRefresh {
231231

232232
# fill in resource files (and their last-write-times) that will be used for up-do-date checks
233233
$lastWriteTimes = @{}
234-
Get-ChildItem -Recurse -File -Path $dscResource.ParentPath -Include "*.ps1", "*.psd1", "*psm1", "*.mof" -ea Ignore | % {
234+
Get-ChildItem -Recurse -File -Path $dscResource.ParentPath -Include "*.ps1", "*.psd1", "*.psm1", "*.mof" -ea Ignore | % {
235235
$lastWriteTimes.Add($_.FullName, $_.LastWriteTime)
236236
}
237237

0 commit comments

Comments
 (0)