diff --git a/config/config_tree.go b/config/config_tree.go index 1486bbd6a..53bb2bf68 100644 --- a/config/config_tree.go +++ b/config/config_tree.go @@ -1,5 +1,9 @@ package config +import ( + "fmt" +) + // configTree represents a tree of configurations where the root is the // first file and its children are the configurations it has imported. type configTree struct { @@ -62,7 +66,22 @@ func mergeConfig(c1, c2 *Config) (*Config, error) { c.Variables[k] = v2 } - // TODO: merge resources + // Merge resources: If they collide, we just take the latest one + // for now. In the future, we might provide smarter merge functionality. + resources := make(map[string]Resource) + for _, r := range c1.Resources { + id := fmt.Sprintf("%s[%s]", r.Type, r.Name) + resources[id] = r + } + for _, r := range c2.Resources { + id := fmt.Sprintf("%s[%s]", r.Type, r.Name) + resources[id] = r + } + + c.Resources = make([]Resource, 0, len(resources)) + for _, r := range resources { + c.Resources = append(c.Resources, r) + } return c, nil } diff --git a/config/loader_test.go b/config/loader_test.go index 71d025041..0a8fbb42f 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -50,12 +50,10 @@ func TestLoadBasic_import(t *testing.T) { t.Fatalf("bad:\n%s", actual) } - /* actual = resourcesStr(c.Resources) - if actual != strings.TrimSpace(basicResourcesStr) { + if actual != strings.TrimSpace(importResourcesStr) { t.Fatalf("bad:\n%s", actual) } - */ } // This helper turns a resources field into a deterministic @@ -111,6 +109,11 @@ foo bar ` +const importResourcesStr = ` +aws_security_group[db] +aws_security_group[web] +` + const importVariablesStr = ` bar <> diff --git a/config/test-fixtures/import.tf b/config/test-fixtures/import.tf index af3434b60..2acc613a2 100644 --- a/config/test-fixtures/import.tf +++ b/config/test-fixtures/import.tf @@ -4,3 +4,5 @@ variable "foo" { default = "bar"; description = "bar"; } + +resource "aws_security_group" "web" {} diff --git a/config/test-fixtures/import/one.tf b/config/test-fixtures/import/one.tf index 326c1171d..92c78ae46 100644 --- a/config/test-fixtures/import/one.tf +++ b/config/test-fixtures/import/one.tf @@ -1 +1,3 @@ variable "bar" {} + +resource "aws_security_group" "db" {}