Skip to content

Commit fd6ce62

Browse files
authored
Merge branch 'main' into config-result
2 parents c8d8d94 + 07c646e commit fd6ce62

File tree

18 files changed

+359
-176
lines changed

18 files changed

+359
-176
lines changed

dsc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ serde_json = { version = "1.0", features = ["preserve_order"] }
2424
serde_yaml = { version = "0.9.3" }
2525
syntect = { version = "5.0", features = ["default-fancy"], default-features = false }
2626
sysinfo = { version = "0.29.10" }
27-
thiserror = "1.0"
27+
thiserror = "1.0.52"
2828
tracing = { version = "0.1.37" }
2929
tracing-subscriber = { version = "0.3.17", features = ["ansi", "env-filter", "json"] }

dsc/tests/dsc_config_get.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Describe 'dsc config get tests' {
3838
- name: Echo
3939
type: Test/Echo
4040
properties:
41-
text: hello
41+
output: hello
4242
"@
4343
$null = $config_yaml | dsc config get --format pretty-json | Out-String
4444
$LASTEXITCODE | Should -Be 0

dsc/tests/dsc_functions.tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Describe 'tests for function expressions' {
55
It 'function works: <text>' -TestCases @(
66
@{ text = "[concat('a', 'b')]"; expected = 'ab' }
77
@{ text = "[concat('a', 'b', 'c')]"; expected = 'abc' }
8-
@{ text = "[concat('a', 1, concat(2, 'b'))]"; expected = 'a12b' }
8+
@{ text = "[concat('a', concat('b', 'c'))]"; expected = 'abc' }
99
@{ text = "[base64('ab')]"; expected = 'YWI=' }
1010
@{ text = "[base64(concat('a','b'))]"; expected = 'YWI=' }
1111
@{ text = "[base64(base64(concat('a','b')))]"; expected = 'WVdJPQ==' }
@@ -19,9 +19,9 @@ Describe 'tests for function expressions' {
1919
- name: Echo
2020
type: Test/Echo
2121
properties:
22-
text: '$escapedText'
22+
output: '$escapedText'
2323
"@
2424
$out = $config_yaml | dsc config get | ConvertFrom-Json
25-
$out.results[0].result.actualState.text | Should -Be $expected
25+
$out.results[0].result.actualState.output | Should -Be $expected
2626
}
2727
}

dsc/tests/dsc_parameters.tests.ps1

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Describe 'Parameters tests' {
1717
- name: Echo
1818
type: Test/Echo
1919
properties:
20-
text: '[parameters(''param1'')]'
20+
output: '[parameters(''param1'')]'
2121
"@
2222
$params_json = @{ parameters = @{ param1 = 'hello' }} | ConvertTo-Json
2323

@@ -31,16 +31,16 @@ Describe 'Parameters tests' {
3131
}
3232

3333
$LASTEXITCODE | Should -Be 0
34-
$out.results[0].result.actualState.text | Should -BeExactly 'hello'
34+
$out.results[0].result.actualState.output | Should -BeExactly 'hello'
3535
}
3636

3737
It 'Input is <type>' -TestCases @(
38-
@{ type = 'string'; value = 'hello'; expected = 'hello' }
39-
@{ type = 'int'; value = 42; expected = 42 }
40-
@{ type = 'bool'; value = $true; expected = $true }
41-
@{ type = 'array'; value = @('hello', 'world'); expected = '["hello","world"]' }
38+
@{ type = 'string'; value = 'hello' }
39+
@{ type = 'int'; value = 42}
40+
@{ type = 'bool'; value = $true}
41+
@{ type = 'array'; value = @('hello', 'world')}
4242
) {
43-
param($type, $value, $expected)
43+
param($type, $value)
4444

4545
$config_yaml = @"
4646
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
@@ -51,13 +51,13 @@ Describe 'Parameters tests' {
5151
- name: Echo
5252
type: Test/Echo
5353
properties:
54-
text: '[parameters(''param1'')]'
54+
output: '[parameters(''param1'')]'
5555
"@
5656
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json
5757

5858
$out = $config_yaml | dsc config -p $params_json get | ConvertFrom-Json
5959
$LASTEXITCODE | Should -Be 0
60-
$out.results[0].result.actualState.text | Should -BeExactly $expected
60+
$out.results[0].result.actualState.output | Should -BeExactly $value
6161
}
6262

6363
It 'Input is incorrect type <type>' -TestCases @(
@@ -77,7 +77,7 @@ Describe 'Parameters tests' {
7777
- name: Echo
7878
type: Test/Echo
7979
properties:
80-
text: '[parameters(''param1'')]'
80+
output: '[parameters(''param1'')]'
8181
"@
8282
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json
8383

@@ -104,7 +104,7 @@ Describe 'Parameters tests' {
104104
- name: Echo
105105
type: Test/Echo
106106
properties:
107-
text: '[parameters(''param1'')]'
107+
output: '[parameters(''param1'')]'
108108
"@
109109
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json
110110

@@ -130,7 +130,7 @@ Describe 'Parameters tests' {
130130
- name: Echo
131131
type: Test/Echo
132132
properties:
133-
text: '[parameters(''param1'')]'
133+
output: '[parameters(''param1'')]'
134134
"@
135135
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json
136136

@@ -154,7 +154,7 @@ Describe 'Parameters tests' {
154154
- name: Echo
155155
type: Test/Echo
156156
properties:
157-
text: '[parameters(''param1'')]'
157+
output: '[parameters(''param1'')]'
158158
"@
159159
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json
160160

@@ -180,7 +180,7 @@ Describe 'Parameters tests' {
180180
- name: Echo
181181
type: Test/Echo
182182
properties:
183-
text: '[parameters(''param1'')]'
183+
output: '[parameters(''param1'')]'
184184
"@
185185
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json
186186

@@ -192,28 +192,43 @@ Describe 'Parameters tests' {
192192
$config_yaml = @"
193193
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
194194
parameters:
195-
param1:
195+
paramString:
196196
type: string
197197
defaultValue: 'hello'
198-
param2:
198+
paramInt:
199199
type: int
200200
defaultValue: 7
201-
param3:
201+
paramBool:
202202
type: bool
203203
defaultValue: false
204-
param4:
204+
paramArray:
205205
type: array
206206
defaultValue: ['hello', 'world']
207207
resources:
208-
- name: Echo
208+
- name: String
209+
type: Test/Echo
210+
properties:
211+
output: '[parameters(''paramString'')]'
212+
- name: Int
213+
type: Test/Echo
214+
properties:
215+
output: '[parameters(''paramInt'')]'
216+
- name: Bool
217+
type: Test/Echo
218+
properties:
219+
output: '[parameters(''paramBool'')]'
220+
- name: Array
209221
type: Test/Echo
210222
properties:
211-
text: '[concat(parameters(''param1''),'','',parameters(''param2''),'','',parameters(''param3''),'','',parameters(''param4''))]'
223+
output: '[parameters(''paramArray'')]'
212224
"@
213225

214226
$out = $config_yaml | dsc config get | ConvertFrom-Json
215227
$LASTEXITCODE | Should -Be 0
216-
$out.results[0].result.actualState.text | Should -BeExactly 'hello,7,false,["hello","world"]'
228+
$out.results[0].result.actualState.output | Should -BeExactly 'hello'
229+
$out.results[1].result.actualState.output | Should -BeExactly 7
230+
$out.results[2].result.actualState.output | Should -BeExactly $false
231+
$out.results[3].result.actualState.output | Should -BeExactly @('hello', 'world')
217232
}
218233

219234
It 'property value uses parameter value' {

dsc/tests/dsc_test.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Describe 'config test tests' {
5656
}
5757

5858
It 'can accept the use of --format as a subcommand' {
59-
$null = "text: hello" | dsc resource test -r Test/Echo --format pretty-json
59+
$null = "output: hello" | dsc resource test -r Test/Echo --format pretty-json
6060
$LASTEXITCODE | Should -Be 0
6161
}
6262
}

dsc_lib/src/configure/depends_on.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ pub fn get_resource_invocation_order(config: &Configuration, parser: &mut Statem
3636
if let Some(depends_on) = resource.depends_on.clone() {
3737
for dependency in depends_on {
3838
let statement = parser.parse_and_execute(&dependency, context)?;
39-
let (resource_type, resource_name) = get_type_and_name(&statement)?;
39+
let Some(string_result) = statement.as_str() else {
40+
return Err(DscError::Validation(format!("'dependsOn' syntax is incorrect: {dependency}")));
41+
};
42+
let (resource_type, resource_name) = get_type_and_name(string_result)?;
4043

4144
// find the resource by name
4245
let Some(dependency_resource) = config.resources.iter().find(|r| r.name.eq(resource_name)) else {
@@ -67,7 +70,10 @@ pub fn get_resource_invocation_order(config: &Configuration, parser: &mut Statem
6770
let resource_index = order.iter().position(|r| r.name == resource.name && r.resource_type == resource.resource_type).ok_or(DscError::Validation("Resource not found in order".to_string()))?;
6871
for dependency in depends_on {
6972
let statement = parser.parse_and_execute(dependency, context)?;
70-
let (resource_type, resource_name) = get_type_and_name(&statement)?;
73+
let Some(string_result) = statement.as_str() else {
74+
return Err(DscError::Validation(format!("'dependsOn' syntax is incorrect: {dependency}")));
75+
};
76+
let (resource_type, resource_name) = get_type_and_name(string_result)?;
7177
let dependency_index = order.iter().position(|r| r.name == resource_name && r.resource_type == resource_type).ok_or(DscError::Validation("Dependency not found in order".to_string()))?;
7278
if resource_index < dependency_index {
7379
return Err(DscError::Validation(format!("Circular dependency detected for resource named '{0}'", resource.name)));

dsc_lib/src/configure/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub fn add_resource_export_results_to_configuration(resource: &DscResource, prov
7373
// for values returned by resources, they may look like expressions, so we make sure to escape them in case
7474
// they are re-used to apply configuration
7575
fn escape_property_values(properties: &Map<String, Value>) -> Result<Option<Map<String, Value>>, DscError> {
76+
debug!("Escape returned property values");
7677
let mut result: Map<String, Value> = Map::new();
7778
for (name, value) in properties {
7879
match value {
@@ -401,13 +402,15 @@ impl Configurator {
401402
}
402403

403404
fn invoke_property_expressions(&mut self, properties: &Option<Map<String, Value>>) -> Result<Option<Map<String, Value>>, DscError> {
405+
debug!("Invoke property expressions");
404406
if properties.is_none() {
405407
return Ok(None);
406408
}
407409

408410
let mut result: Map<String, Value> = Map::new();
409411
if let Some(properties) = properties {
410412
for (name, value) in properties {
413+
trace!("Invoke property expression for {name}: {value}");
411414
match value {
412415
Value::Object(object) => {
413416
let value = self.invoke_property_expressions(&Some(object.clone()))?;
@@ -432,7 +435,10 @@ impl Configurator {
432435
return Err(DscError::Parser("Array element could not be transformed as string".to_string()));
433436
};
434437
let statement_result = self.statement_parser.parse_and_execute(statement, &self.context)?;
435-
result_array.push(Value::String(statement_result));
438+
let Some(string_result) = statement_result.as_str() else {
439+
return Err(DscError::Parser("Array element could not be transformed as string".to_string()));
440+
};
441+
result_array.push(Value::String(string_result.to_string()));
436442
}
437443
_ => {
438444
result_array.push(element.clone());
@@ -447,7 +453,11 @@ impl Configurator {
447453
return Err(DscError::Parser(format!("Property value '{value}' could not be transformed as string")));
448454
};
449455
let statement_result = self.statement_parser.parse_and_execute(statement, &self.context)?;
450-
result.insert(name.clone(), Value::String(statement_result));
456+
if let Some(string_result) = statement_result.as_str() {
457+
result.insert(name.clone(), Value::String(string_result.to_string()));
458+
} else {
459+
result.insert(name.clone(), statement_result);
460+
};
451461
},
452462
_ => {
453463
result.insert(name.clone(), value.clone());

dsc_lib/src/functions/base64.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use base64::{Engine as _, engine::general_purpose};
55

66
use crate::DscError;
77
use crate::configure::context::Context;
8-
use crate::parser::functions::{FunctionArg, FunctionResult};
9-
use super::{Function, AcceptedArgKind};
8+
use crate::functions::AcceptedArgKind;
9+
use serde_json::Value;
10+
use super::Function;
1011

1112
#[derive(Debug, Default)]
1213
pub struct Base64 {}
@@ -24,11 +25,8 @@ impl Function for Base64 {
2425
1
2526
}
2627

27-
fn invoke(&self, args: &[FunctionArg], _context: &Context) -> Result<FunctionResult, DscError> {
28-
let FunctionArg::String(arg) = args.first().unwrap() else {
29-
return Err(DscError::Parser("Invalid argument type".to_string()));
30-
};
31-
Ok(FunctionResult::String(general_purpose::STANDARD.encode(arg)))
28+
fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {
29+
Ok(Value::String(general_purpose::STANDARD.encode(args[0].as_str().unwrap_or_default())))
3230
}
3331
}
3432

0 commit comments

Comments
 (0)