config: change module syntax

This commit is contained in:
Mitchell Hashimoto 2014-09-14 14:43:54 -07:00
parent 5e4c2b4f49
commit 8dc8eac4bf
4 changed files with 35 additions and 43 deletions

View File

@ -32,7 +32,6 @@ type Config struct {
// call-site within an existing configuration. // call-site within an existing configuration.
type Module struct { type Module struct {
Name string Name string
Type string
Source string Source string
RawConfig *RawConfig RawConfig *RawConfig
} }
@ -106,7 +105,7 @@ func ProviderConfigName(t string, pcs []*ProviderConfig) string {
// A unique identifier for this module. // A unique identifier for this module.
func (r *Module) Id() string { 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. // A unique identifier for this resource.
@ -318,7 +317,6 @@ func (m *Module) mergerMerge(other merger) merger {
result := *m result := *m
result.Name = m2.Name result.Name = m2.Name
result.Type = m2.Type
result.RawConfig = result.RawConfig.merge(m2.RawConfig) result.RawConfig = result.RawConfig.merge(m2.RawConfig)
if m2.Source != "" { if m2.Source != "" {

View File

@ -194,7 +194,7 @@ func loadFileHcl(root string) (configurable, []string, error) {
// represents exactly one module definition in the HCL configuration. // represents exactly one module definition in the HCL configuration.
// We leave it up to another pass to merge them together. // We leave it up to another pass to merge them together.
func loadModulesHcl(os *hclobj.Object) ([]*Module, error) { 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. // See loadResourcesHcl for why this exists. Don't touch this.
for _, o1 := range os.Elem(false) { for _, o1 := range os.Elem(false) {
@ -202,7 +202,7 @@ func loadModulesHcl(os *hclobj.Object) ([]*Module, error) {
for _, o2 := range o1.Elem(true) { for _, o2 := range o1.Elem(true) {
// Iterate all of this type to get _all_ the types // Iterate all of this type to get _all_ the types
for _, o3 := range o2.Elem(false) { 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 // Now go over all the types and their children in order to get
// all of the actual resources. // all of the actual resources.
for _, t := range allTypes { for _, obj := range allNames {
for _, obj := range t.Elem(true) { k := obj.Key
k := obj.Key
var config map[string]interface{} var config map[string]interface{}
if err := hcl.DecodeObject(&config, obj); err != nil { if err := hcl.DecodeObject(&config, obj); err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"Error reading config for %s[%s]: %s", "Error reading config for %s: %s",
t.Key, k,
k, err)
err) }
}
// Remove the fields we handle specially // Remove the fields we handle specially
delete(config, "source") 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 { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"Error reading config for %s[%s]: %s", "Error parsing source for %s: %s",
t.Key,
k, k,
err) 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 return result, nil

View File

@ -663,7 +663,7 @@ foo
` `
const modulesModulesStr = ` const modulesModulesStr = `
foo.bar module.bar
source = baz source = baz
memory memory
` `

View File

@ -1,4 +1,4 @@
module "foo" "bar" { module "bar" {
memory = "1G" memory = "1G"
source = "baz" source = "baz"
} }