2014-05-23 17:35:18 -05:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-05-24 14:51:31 -05:00
|
|
|
"io"
|
2014-05-23 17:35:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// configurable is an interface that must be implemented by any configuration
|
|
|
|
// formats of Terraform in order to return a *Config.
|
|
|
|
type configurable interface {
|
|
|
|
Config() (*Config, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// importTree is the result of the first-pass load of the configuration
|
|
|
|
// files. It is a tree of raw configurables and then any children (their
|
|
|
|
// imports).
|
|
|
|
//
|
|
|
|
// An importTree can be turned into a configTree.
|
|
|
|
type importTree struct {
|
|
|
|
Path string
|
|
|
|
Raw configurable
|
|
|
|
Children []*importTree
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the function type that must be implemented by the configuration
|
|
|
|
// file loader to turn a single file into a configurable and any additional
|
|
|
|
// imports.
|
|
|
|
type fileLoaderFunc func(path string) (configurable, []string, error)
|
|
|
|
|
|
|
|
// loadTree takes a single file and loads the entire importTree for that
|
|
|
|
// file. This function detects what kind of configuration file it is an
|
|
|
|
// executes the proper fileLoaderFunc.
|
|
|
|
func loadTree(root string) (*importTree, error) {
|
2014-05-23 17:42:29 -05:00
|
|
|
var f fileLoaderFunc
|
2014-07-20 19:52:46 -05:00
|
|
|
switch ext(root) {
|
|
|
|
case ".tf":
|
|
|
|
fallthrough
|
|
|
|
case ".tf.json":
|
2014-08-05 00:04:48 -05:00
|
|
|
f = loadFileHcl
|
2014-07-20 19:52:46 -05:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if f == nil {
|
2014-05-23 17:42:29 -05:00
|
|
|
return nil, fmt.Errorf(
|
2014-07-18 18:54:52 -05:00
|
|
|
"%s: unknown configuration format. Use '.tf' or '.tf.json' extension",
|
2014-05-23 17:42:29 -05:00
|
|
|
root)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, imps, err := f(root)
|
2014-05-23 17:35:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
children := make([]*importTree, len(imps))
|
|
|
|
for i, imp := range imps {
|
|
|
|
t, err := loadTree(imp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
children[i] = t
|
|
|
|
}
|
|
|
|
|
|
|
|
return &importTree{
|
|
|
|
Path: root,
|
|
|
|
Raw: c,
|
|
|
|
Children: children,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-05-23 18:30:28 -05:00
|
|
|
// Close releases any resources we might be holding open for the importTree.
|
|
|
|
//
|
|
|
|
// This can safely be called even while ConfigTree results are alive. The
|
|
|
|
// importTree is not bound to these.
|
|
|
|
func (t *importTree) Close() error {
|
|
|
|
if c, ok := t.Raw.(io.Closer); ok {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
for _, ct := range t.Children {
|
|
|
|
ct.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-23 17:35:18 -05:00
|
|
|
// ConfigTree traverses the importTree and turns each node into a *Config
|
|
|
|
// object, ultimately returning a *configTree.
|
|
|
|
func (t *importTree) ConfigTree() (*configTree, error) {
|
|
|
|
config, err := t.Raw.Config()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error loading %s: %s",
|
|
|
|
t.Path,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build our result
|
|
|
|
result := &configTree{
|
|
|
|
Path: t.Path,
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
|
2014-05-23 18:09:41 -05:00
|
|
|
// Build the config trees for the children
|
|
|
|
result.Children = make([]*configTree, len(t.Children))
|
|
|
|
for i, ct := range t.Children {
|
|
|
|
t, err := ct.ConfigTree()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Children[i] = t
|
|
|
|
}
|
2014-05-23 17:35:18 -05:00
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|