From 8dc8eac4bf90c52159146acd89f20540842246fa Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 14 Sep 2014 14:43:54 -0700 Subject: [PATCH] config: change module syntax --- config/config.go | 4 +- config/loader_hcl.go | 70 +++++++++++++++------------------ config/loader_test.go | 2 +- config/test-fixtures/modules.tf | 2 +- 4 files changed, 35 insertions(+), 43 deletions(-) diff --git a/config/config.go b/config/config.go index 51a033427..60cb8818a 100644 --- a/config/config.go +++ b/config/config.go @@ -32,7 +32,6 @@ type Config struct { // call-site within an existing configuration. type Module struct { Name string - Type string Source string RawConfig *RawConfig } @@ -106,7 +105,7 @@ func ProviderConfigName(t string, pcs []*ProviderConfig) string { // A unique identifier for this module. func (r *Module) Id() string { - return fmt.Sprintf("%s.%s", r.Type, r.Name) + return fmt.Sprintf("module.%s", r.Name) } // A unique identifier for this resource. @@ -318,7 +317,6 @@ func (m *Module) mergerMerge(other merger) merger { result := *m result.Name = m2.Name - result.Type = m2.Type result.RawConfig = result.RawConfig.merge(m2.RawConfig) if m2.Source != "" { diff --git a/config/loader_hcl.go b/config/loader_hcl.go index 13337006a..babc34ca6 100644 --- a/config/loader_hcl.go +++ b/config/loader_hcl.go @@ -194,7 +194,7 @@ func loadFileHcl(root string) (configurable, []string, error) { // represents exactly one module definition in the HCL configuration. // We leave it up to another pass to merge them together. func loadModulesHcl(os *hclobj.Object) ([]*Module, error) { - var allTypes []*hclobj.Object + var allNames []*hclobj.Object // See loadResourcesHcl for why this exists. Don't touch this. for _, o1 := range os.Elem(false) { @@ -202,7 +202,7 @@ func loadModulesHcl(os *hclobj.Object) ([]*Module, error) { for _, o2 := range o1.Elem(true) { // Iterate all of this type to get _all_ the types for _, o3 := range o2.Elem(false) { - allTypes = append(allTypes, o3) + allNames = append(allNames, o3) } } } @@ -212,51 +212,45 @@ func loadModulesHcl(os *hclobj.Object) ([]*Module, error) { // Now go over all the types and their children in order to get // all of the actual resources. - for _, t := range allTypes { - for _, obj := range t.Elem(true) { - k := obj.Key + for _, obj := range allNames { + k := obj.Key - var config map[string]interface{} - if err := hcl.DecodeObject(&config, obj); err != nil { - return nil, fmt.Errorf( - "Error reading config for %s[%s]: %s", - t.Key, - k, - err) - } + var config map[string]interface{} + if err := hcl.DecodeObject(&config, obj); err != nil { + return nil, fmt.Errorf( + "Error reading config for %s: %s", + k, + err) + } - // Remove the fields we handle specially - delete(config, "source") + // Remove the fields we handle specially + delete(config, "source") - rawConfig, err := NewRawConfig(config) + rawConfig, err := NewRawConfig(config) + if err != nil { + return nil, fmt.Errorf( + "Error reading config for %s: %s", + k, + err) + } + + // If we have a count, then figure it out + var source string + if o := obj.Get("source", false); o != nil { + err = hcl.DecodeObject(&source, o) if err != nil { return nil, fmt.Errorf( - "Error reading config for %s[%s]: %s", - t.Key, + "Error parsing source for %s: %s", k, err) } - - // If we have a count, then figure it out - var source string - if o := obj.Get("source", false); o != nil { - err = hcl.DecodeObject(&source, o) - if err != nil { - return nil, fmt.Errorf( - "Error parsing source for %s[%s]: %s", - t.Key, - k, - err) - } - } - - result = append(result, &Module{ - Name: k, - Type: t.Key, - Source: source, - RawConfig: rawConfig, - }) } + + result = append(result, &Module{ + Name: k, + Source: source, + RawConfig: rawConfig, + }) } return result, nil diff --git a/config/loader_test.go b/config/loader_test.go index e4843e5c6..3dad74940 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -663,7 +663,7 @@ foo ` const modulesModulesStr = ` -foo.bar +module.bar source = baz memory ` diff --git a/config/test-fixtures/modules.tf b/config/test-fixtures/modules.tf index 48e7ad09e..dc1fb6a79 100644 --- a/config/test-fixtures/modules.tf +++ b/config/test-fixtures/modules.tf @@ -1,4 +1,4 @@ -module "foo" "bar" { +module "bar" { memory = "1G" source = "baz" }