Skip to content

Commit 3337a62

Browse files
committed
config: support count meta-parameter
1 parent 17074e5 commit 3337a62

4 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎config/config.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type ProviderConfig struct {
3131
type Resource struct {
3232
Name string
3333
Type string
34+
Count int
3435
RawConfig *RawConfig
3536
}
3637

‎config/loader_libucl.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) {
253253
err)
254254
}
255255

256+
// Remove the "count" from the config, since we treat that special
257+
delete(config, "count")
258+
256259
rawConfig, err := NewRawConfig(config)
257260
if err != nil {
258261
return nil, fmt.Errorf(
@@ -262,9 +265,24 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) {
262265
err)
263266
}
264267

268+
// If we have a count, then figure it out
269+
var count int = 1
270+
if o := r.Get("count"); o != nil {
271+
err = o.Decode(&count)
272+
o.Close()
273+
if err != nil {
274+
return nil, fmt.Errorf(
275+
"Error parsing count for %s[%s]: %s",
276+
t.Key(),
277+
r.Key(),
278+
err)
279+
}
280+
}
281+
265282
result = append(result, &Resource{
266283
Name: r.Key(),
267284
Type: t.Key(),
285+
Count: count,
268286
RawConfig: rawConfig,
269287
})
270288
}

‎config/loader_test.go‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ func resourcesStr(rs []*Resource) string {
145145
result := ""
146146
for _, r := range rs {
147147
result += fmt.Sprintf(
148-
"%s[%s]\n",
148+
"%s[%s] (x%d)\n",
149149
r.Type,
150-
r.Name)
150+
r.Name,
151+
r.Count)
151152

152153
ks := make([]string, 0, len(r.RawConfig.Raw))
153154
for k, _ := range r.RawConfig.Raw {
@@ -229,8 +230,8 @@ do
229230
`
230231

231232
const basicResourcesStr = `
232-
aws_security_group[firewall]
233-
aws_instance[web]
233+
aws_security_group[firewall] (x5)
234+
aws_instance[web] (x1)
234235
ami
235236
network_interface
236237
security_groups
@@ -251,8 +252,8 @@ aws
251252
`
252253

253254
const importResourcesStr = `
254-
aws_security_group[db]
255-
aws_security_group[web]
255+
aws_security_group[db] (x1)
256+
aws_security_group[web] (x1)
256257
`
257258

258259
const importVariablesStr = `

‎config/test-fixtures/basic.tf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ provider "do" {
1313
}
1414

1515
resource "aws_security_group" "firewall" {
16+
count = 5
1617
}
1718

1819
resource aws_instance "web" {

0 commit comments

Comments
 (0)