Skip to content

Commit 9bd295f

Browse files
committed
Add support for invoking property expressions in resource configuration
1 parent 33d2f64 commit 9bd295f

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

dsc/tests/dsc_copy.tests.ps1

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,75 @@ resources:
263263
$out.results[3].name | Should -Be 'Server-3'
264264
$out.results[3].result.actualState.output | Should -Be 'Hello'
265265
}
266+
267+
It 'Copy works with copyIndex() in properties' {
268+
$configYaml = @'
269+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
270+
resources:
271+
- name: "[format('Server-{0}', copyIndex())]"
272+
copy:
273+
name: testLoop
274+
count: 3
275+
type: Microsoft.DSC.Debug/Echo
276+
properties:
277+
output: "[format('Instance-{0}', copyIndex())]"
278+
'@
279+
$out = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json
280+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $testdrive/error.log -Raw | Out-String)
281+
$out.results.Count | Should -Be 3
282+
$out.results[0].name | Should -Be 'Server-0'
283+
$out.results[0].result.actualState.output | Should -Be 'Instance-0'
284+
$out.results[1].name | Should -Be 'Server-1'
285+
$out.results[1].result.actualState.output | Should -Be 'Instance-1'
286+
$out.results[2].name | Should -Be 'Server-2'
287+
$out.results[2].result.actualState.output | Should -Be 'Instance-2'
288+
}
289+
290+
It 'Copy works with copyIndex() with offset in properties' {
291+
$configYaml = @'
292+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
293+
resources:
294+
- name: "[format('Server-{0}', copyIndex())]"
295+
copy:
296+
name: testLoop
297+
count: 3
298+
type: Microsoft.DSC.Debug/Echo
299+
properties:
300+
output: "[format('Port-{0}', copyIndex(8080))]"
301+
'@
302+
$out = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json
303+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $testdrive/error.log -Raw | Out-String)
304+
$out.results.Count | Should -Be 3
305+
$out.results[0].name | Should -Be 'Server-0'
306+
$out.results[0].result.actualState.output | Should -Be 'Port-8080'
307+
$out.results[1].name | Should -Be 'Server-1'
308+
$out.results[1].result.actualState.output | Should -Be 'Port-8081'
309+
$out.results[2].name | Should -Be 'Server-2'
310+
$out.results[2].result.actualState.output | Should -Be 'Port-8082'
311+
}
312+
313+
It 'Copy works with parameters and copyIndex() combined in properties' {
314+
$configYaml = @'
315+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
316+
parameters:
317+
prefix:
318+
type: string
319+
defaultValue: web
320+
resources:
321+
- name: "[format('Server-{0}', copyIndex())]"
322+
copy:
323+
name: testLoop
324+
count: 2
325+
type: Microsoft.DSC.Debug/Echo
326+
properties:
327+
output: "[concat(parameters('prefix'), '-', string(copyIndex(1)))]"
328+
'@
329+
$out = dsc -l trace config get -i $configYaml 2>$testdrive/error.log | ConvertFrom-Json
330+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $testdrive/error.log -Raw | Out-String)
331+
$out.results.Count | Should -Be 2
332+
$out.results[0].name | Should -Be 'Server-0'
333+
$out.results[0].result.actualState.output | Should -Be 'web-1'
334+
$out.results[1].name | Should -Be 'Server-1'
335+
$out.results[1].result.actualState.output | Should -Be 'web-2'
336+
}
266337
}

lib/dsc-lib/src/configure/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,10 @@ impl Configurator {
10371037
};
10381038
new_resource.name = new_name.to_string();
10391039

1040+
if let Some(properties) = &resource.properties {
1041+
new_resource.properties = self.invoke_property_expressions(Some(properties))?;
1042+
}
1043+
10401044
new_resource.copy = None;
10411045
copy_resources.push(new_resource);
10421046
}

0 commit comments

Comments
 (0)