Skip to content

Commit 0eedb93

Browse files
authored
Merge pull request #1147 from Gijsreyn/gh-57/main/add-copy-function
Reopen add `copy()` and `copyIndex()` function reference documentation
2 parents bc5639b + cbae165 commit 0eedb93

File tree

3 files changed

+519
-0
lines changed

3 files changed

+519
-0
lines changed
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
---
2+
description: Reference for the 'copy' DSC configuration document resource loop
3+
ms.date: 02/28/2025
4+
ms.topic: reference
5+
title: copy
6+
---
7+
8+
# copy
9+
10+
## Synopsis
11+
12+
Defines a loop to create multiple instances of a resource.
13+
14+
## Syntax
15+
16+
```Syntax
17+
copy:
18+
name: <loopName>
19+
count: <numberOfIterations>
20+
```
21+
22+
## Description
23+
24+
The `copy` property enables you to create multiple instances of a resource in a
25+
DSC configuration. This is the equivalent implementation of the copy
26+
functionality from Azure Resource Manager (ARM) templates, but without support
27+
for variables and properties, which will be added in future releases.
28+
29+
When you use `copy` on a resource, DSC creates multiple instances of that
30+
resource based on the specified count. You can use the [`copyIndex()`][01]
31+
function within the resource definition to access the current iteration index
32+
and create unique names or property values for each instance.
33+
34+
> [!NOTE]
35+
> The `mode` and `batchSize` properties are not currently supported and will
36+
> result in an error if used.
37+
38+
## Examples
39+
40+
### Example 1 - Create multiple Echo resources
41+
42+
This example demonstrates the basic usage of `copy` to create three instances
43+
of a Debug Echo resource.
44+
45+
```yaml
46+
# copy.example.1.dsc.config.yaml
47+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
48+
resources:
49+
- name: "[format('Echo-{0}', copyIndex())]"
50+
copy:
51+
name: echoLoop
52+
count: 3
53+
type: Microsoft.DSC.Debug/Echo
54+
properties:
55+
output: "Hello DSC"
56+
```
57+
58+
```bash
59+
dsc config get --file copy.example.1.dsc.config.yaml
60+
```
61+
62+
```yaml
63+
results:
64+
- name: Echo-0
65+
type: Microsoft.DSC.Debug/Echo
66+
result:
67+
actualState:
68+
output: Hello DSC
69+
- name: Echo-1
70+
type: Microsoft.DSC.Debug/Echo
71+
result:
72+
actualState:
73+
output: Hello DSC
74+
- name: Echo-2
75+
type: Microsoft.DSC.Debug/Echo
76+
result:
77+
actualState:
78+
output: Hello DSC
79+
messages: []
80+
hadErrors: false
81+
```
82+
83+
### Example 2 - Using copyIndex with offset
84+
85+
This example demonstrates using [`copyIndex()`][01] with an offset to start
86+
numbering from a different value.
87+
88+
```yaml
89+
# copy.example.2.dsc.config.yaml
90+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
91+
resources:
92+
- name: "[format('Service-{0}', copyIndex(100))]"
93+
copy:
94+
name: serviceLoop
95+
count: 3
96+
type: Microsoft.DSC.Debug/Echo
97+
properties:
98+
output: "Service instance"
99+
```
100+
101+
```bash
102+
dsc config get --file copy.example.2.dsc.config.yaml
103+
```
104+
105+
```yaml
106+
results:
107+
- name: Service-100
108+
type: Microsoft.DSC.Debug/Echo
109+
result:
110+
actualState:
111+
output: Service instance
112+
- name: Service-101
113+
type: Microsoft.DSC.Debug/Echo
114+
result:
115+
actualState:
116+
output: Service instance
117+
- name: Service-102
118+
type: Microsoft.DSC.Debug/Echo
119+
result:
120+
actualState:
121+
output: Service instance
122+
messages: []
123+
hadErrors: false
124+
```
125+
126+
### Example 3 - Using named loop references
127+
128+
This example shows how to reference a specific loop by name when using
129+
[`copyIndex()`][01].
130+
131+
```yaml
132+
# copy.example.3.dsc.config.yaml
133+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
134+
resources:
135+
- name: "[format('Resource-{0}', copyIndex('mainLoop'))]"
136+
copy:
137+
name: mainLoop
138+
count: 2
139+
type: Microsoft.DSC.Debug/Echo
140+
properties:
141+
output: "From main loop"
142+
```
143+
144+
```bash
145+
dsc config get --file copy.example.3.dsc.config.yaml
146+
```
147+
148+
```yaml
149+
results:
150+
- name: Resource-0
151+
type: Microsoft.DSC.Debug/Echo
152+
result:
153+
actualState:
154+
output: From main loop
155+
- name: Resource-1
156+
type: Microsoft.DSC.Debug/Echo
157+
result:
158+
actualState:
159+
output: From main loop
160+
messages: []
161+
hadErrors: false
162+
```
163+
164+
### Example 4 - Using expressions for count with parameters
165+
166+
This example demonstrates using an expression for the `count` property, which
167+
allows you to dynamically determine the number of resource instances to create
168+
based on a parameter value. This is commonly used to make configurations
169+
flexible and reusable.
170+
171+
```yaml
172+
# copy.example.4.dsc.config.yaml
173+
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
174+
parameters:
175+
instanceCount:
176+
type: int
177+
defaultValue: 2
178+
resources:
179+
- name: "[format('Dynamic-{0}', copyIndex())]"
180+
copy:
181+
name: dynamicLoop
182+
count: "[parameters('instanceCount')]"
183+
type: Microsoft.DSC.Debug/Echo
184+
properties:
185+
output: "[format('Instance {0} of {1}', copyIndex(), parameters('instanceCount'))]"
186+
```
187+
188+
```bash
189+
dsc config get --file copy.example.4.dsc.config.yaml --parameters '{"instanceCount": 4}'
190+
```
191+
192+
```yaml
193+
results:
194+
- metadata:
195+
Microsoft.DSC:
196+
duration: PT0.2173106S
197+
name: Dynamic-0
198+
type: Microsoft.DSC.Debug/Echo
199+
result:
200+
actualState:
201+
output: Instance 0 of 2
202+
- metadata:
203+
Microsoft.DSC:
204+
duration: PT0.0161486S
205+
name: Dynamic-1
206+
type: Microsoft.DSC.Debug/Echo
207+
result:
208+
actualState:
209+
output: Instance 1 of 2
210+
messages: []
211+
hadErrors: false
212+
```
213+
214+
## Properties
215+
216+
### name
217+
218+
The name of the copy loop. This name can be used with the [`copyIndex()`][01]
219+
function to reference the current iteration index of this specific loop.
220+
221+
```yaml
222+
Type: string
223+
Required: true
224+
```
225+
226+
### count
227+
228+
The number of iterations to perform. Must be a non-negative integer. If set to
229+
0, no instances of the resource are created.
230+
231+
The `count` property accepts both literal integer values and expressions that
232+
evaluate to an integer, such as parameter references using the
233+
[`parameters()`][02] function. This allows you to dynamically control the
234+
number of resource instances based on configuration parameters.
235+
236+
```yaml
237+
Type: integer
238+
Required: true
239+
Minimum: 0
240+
```
241+
242+
### mode
243+
244+
> [!WARNING]
245+
> The `mode` property is not currently supported and will result in an error
246+
> if used.
247+
248+
This property is reserved for future implementation to specify whether resources
249+
should be created serially or in parallel.
250+
251+
### batchSize
252+
253+
> [!WARNING]
254+
> The `batchSize` property is not currently supported and will result in an
255+
> error if used.
256+
257+
This property is reserved for future implementation to specify how many
258+
resources to create in each batch when using parallel mode.
259+
260+
## Limitations
261+
262+
The current implementation has the following limitations:
263+
264+
- **Variables and properties**: Copy loops for variables and properties are not
265+
yet supported.
266+
- **Mode control**: The `mode` property (serial/parallel) is not implemented.
267+
- **Batch processing**: The `batchSize` property is not implemented.
268+
- **Name expressions**: The resource name expression must evaluate to a string.
269+
270+
## Related Functions
271+
272+
- [`copyIndex()`][01] - Returns the current iteration index of a copy loop.
273+
- [`parameters()`][02] - Returns the value of a configuration parameter.
274+
275+
<!-- Link reference definitions -->
276+
[01]: ./copyIndex.md
277+
[02]: ./parameters.md

0 commit comments

Comments
 (0)