config: merge resources

This commit is contained in:
Mitchell Hashimoto 2014-05-23 16:25:54 -07:00
parent 14a25e6b58
commit 50830e429a
4 changed files with 30 additions and 4 deletions

View File

@ -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
}

View File

@ -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
<>

View File

@ -4,3 +4,5 @@ variable "foo" {
default = "bar";
description = "bar";
}
resource "aws_security_group" "web" {}

View File

@ -1 +1,3 @@
variable "bar" {}
resource "aws_security_group" "db" {}