diff --git a/config/config.go b/config/config.go index 8c8b80fa5..63c5651a5 100644 --- a/config/config.go +++ b/config/config.go @@ -31,6 +31,7 @@ type ProviderConfig struct { type Resource struct { Name string Type string + Count int RawConfig *RawConfig } diff --git a/config/loader_libucl.go b/config/loader_libucl.go index 0f80da50a..4d5644a26 100644 --- a/config/loader_libucl.go +++ b/config/loader_libucl.go @@ -253,6 +253,9 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) { err) } + // Remove the "count" from the config, since we treat that special + delete(config, "count") + rawConfig, err := NewRawConfig(config) if err != nil { return nil, fmt.Errorf( @@ -262,9 +265,24 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) { err) } + // If we have a count, then figure it out + var count int = 1 + if o := r.Get("count"); o != nil { + err = o.Decode(&count) + o.Close() + if err != nil { + return nil, fmt.Errorf( + "Error parsing count for %s[%s]: %s", + t.Key(), + r.Key(), + err) + } + } + result = append(result, &Resource{ Name: r.Key(), Type: t.Key(), + Count: count, RawConfig: rawConfig, }) } diff --git a/config/loader_test.go b/config/loader_test.go index 6742d44c2..4a9a6f939 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -145,9 +145,10 @@ func resourcesStr(rs []*Resource) string { result := "" for _, r := range rs { result += fmt.Sprintf( - "%s[%s]\n", + "%s[%s] (x%d)\n", r.Type, - r.Name) + r.Name, + r.Count) ks := make([]string, 0, len(r.RawConfig.Raw)) for k, _ := range r.RawConfig.Raw { @@ -229,8 +230,8 @@ do ` const basicResourcesStr = ` -aws_security_group[firewall] -aws_instance[web] +aws_security_group[firewall] (x5) +aws_instance[web] (x1) ami network_interface security_groups @@ -251,8 +252,8 @@ aws ` const importResourcesStr = ` -aws_security_group[db] -aws_security_group[web] +aws_security_group[db] (x1) +aws_security_group[web] (x1) ` const importVariablesStr = ` diff --git a/config/test-fixtures/basic.tf b/config/test-fixtures/basic.tf index 04d71bbbf..f389b1bf0 100644 --- a/config/test-fixtures/basic.tf +++ b/config/test-fixtures/basic.tf @@ -13,6 +13,7 @@ provider "do" { } resource "aws_security_group" "firewall" { + count = 5 } resource aws_instance "web" {