2014-09-14 02:45:56 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
2014-09-15 05:14:37 +02:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2014-09-15 01:17:29 +02:00
|
|
|
"fmt"
|
2014-09-15 05:14:37 +02:00
|
|
|
"strings"
|
2014-09-15 01:17:29 +02:00
|
|
|
"sync"
|
|
|
|
|
2014-09-14 02:45:56 +02:00
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Tree represents the module import tree of configurations.
|
|
|
|
//
|
|
|
|
// This Tree structure can be used to get (download) new modules, load
|
|
|
|
// all the modules without getting, flatten the tree into something
|
|
|
|
// Terraform can use, etc.
|
|
|
|
type Tree struct {
|
2014-09-15 05:14:37 +02:00
|
|
|
name string
|
2014-09-15 01:17:29 +02:00
|
|
|
config *config.Config
|
2014-09-15 18:53:29 +02:00
|
|
|
children map[string]*Tree
|
2014-09-15 05:14:37 +02:00
|
|
|
lock sync.RWMutex
|
2014-09-14 02:45:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMode is an enum that describes how modules are loaded.
|
|
|
|
//
|
|
|
|
// GetModeLoad says that modules will not be downloaded or updated, they will
|
|
|
|
// only be loaded from the storage.
|
|
|
|
//
|
|
|
|
// GetModeGet says that modules can be initially downloaded if they don't
|
|
|
|
// exist, but otherwise to just load from the current version in storage.
|
|
|
|
//
|
|
|
|
// GetModeUpdate says that modules should be checked for updates and
|
|
|
|
// downloaded prior to loading. If there are no updates, we load the version
|
|
|
|
// from disk, otherwise we download first and then load.
|
|
|
|
type GetMode byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
GetModeNone GetMode = iota
|
|
|
|
GetModeGet
|
|
|
|
GetModeUpdate
|
|
|
|
)
|
|
|
|
|
2014-09-14 23:46:45 +02:00
|
|
|
// NewTree returns a new Tree for the given config structure.
|
|
|
|
func NewTree(c *config.Config) *Tree {
|
2014-09-15 01:17:29 +02:00
|
|
|
return &Tree{config: c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Children returns the children of this tree (the modules that are
|
|
|
|
// imported by this root).
|
|
|
|
//
|
|
|
|
// This will only return a non-nil value after Load is called.
|
2014-09-15 18:53:29 +02:00
|
|
|
func (t *Tree) Children() map[string]*Tree {
|
2014-09-15 05:14:37 +02:00
|
|
|
t.lock.RLock()
|
|
|
|
defer t.lock.RUnlock()
|
|
|
|
return t.children
|
2014-09-14 23:46:45 +02:00
|
|
|
}
|
|
|
|
|
2014-09-14 02:45:56 +02:00
|
|
|
// Flatten takes the entire module tree and flattens it into a single
|
|
|
|
// namespace in *config.Config with no module imports.
|
|
|
|
//
|
|
|
|
// Validate is called here implicitly, since it is important that semantic
|
|
|
|
// checks pass before flattening the configuration. Otherwise, encapsulation
|
|
|
|
// breaks in horrible ways and the errors that come out the other side
|
|
|
|
// will be surprising.
|
|
|
|
func (t *Tree) Flatten() (*config.Config, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:37:40 +02:00
|
|
|
// Loaded says whether or not this tree has been loaded or not yet.
|
|
|
|
func (t *Tree) Loaded() bool {
|
|
|
|
t.lock.RLock()
|
|
|
|
defer t.lock.RUnlock()
|
|
|
|
return t.children != nil
|
|
|
|
}
|
|
|
|
|
2014-09-14 02:45:56 +02:00
|
|
|
// Modules returns the list of modules that this tree imports.
|
2014-09-14 23:46:45 +02:00
|
|
|
//
|
|
|
|
// This is only the imports of _this_ level of the tree. To retrieve the
|
|
|
|
// full nested imports, you'll have to traverse the tree.
|
2014-09-14 02:45:56 +02:00
|
|
|
func (t *Tree) Modules() []*Module {
|
2014-09-15 01:17:29 +02:00
|
|
|
result := make([]*Module, len(t.config.Modules))
|
|
|
|
for i, m := range t.config.Modules {
|
2014-09-14 23:46:45 +02:00
|
|
|
result[i] = &Module{
|
2014-09-15 05:00:17 +02:00
|
|
|
Name: m.Name,
|
2014-09-14 23:46:45 +02:00
|
|
|
Source: m.Source,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2014-09-14 02:45:56 +02:00
|
|
|
}
|
|
|
|
|
2014-09-15 05:14:37 +02:00
|
|
|
// Name returns the name of the tree. This will be "<root>" for the root
|
|
|
|
// tree and then the module name given for any children.
|
|
|
|
func (t *Tree) Name() string {
|
|
|
|
if t.name == "" {
|
|
|
|
return "<root>"
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.name
|
|
|
|
}
|
|
|
|
|
2014-09-14 02:45:56 +02:00
|
|
|
// Load loads the configuration of the entire tree.
|
|
|
|
//
|
|
|
|
// The parameters are used to tell the tree where to find modules and
|
|
|
|
// whether it can download/update modules along the way.
|
|
|
|
//
|
2014-09-15 01:17:29 +02:00
|
|
|
// Calling this multiple times will reload the tree.
|
|
|
|
//
|
2014-09-14 02:45:56 +02:00
|
|
|
// Various semantic-like checks are made along the way of loading since
|
|
|
|
// module trees inherently require the configuration to be in a reasonably
|
|
|
|
// sane state: no circular dependencies, proper module sources, etc. A full
|
|
|
|
// suite of validations can be done by running Validate (after loading).
|
|
|
|
func (t *Tree) Load(s Storage, mode GetMode) error {
|
2014-09-15 01:17:29 +02:00
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
|
|
|
// Reset the children if we have any
|
|
|
|
t.children = nil
|
|
|
|
|
|
|
|
modules := t.Modules()
|
2014-09-15 18:53:29 +02:00
|
|
|
children := make(map[string]*Tree)
|
2014-09-15 01:17:29 +02:00
|
|
|
|
|
|
|
// Go through all the modules and get the directory for them.
|
|
|
|
update := mode == GetModeUpdate
|
2014-09-15 18:53:29 +02:00
|
|
|
for _, m := range modules {
|
|
|
|
if _, ok := children[m.Name]; ok {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"module %s: duplicated. module names must be unique", m.Name)
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:00:17 +02:00
|
|
|
source, err := Detect(m.Source, t.config.Dir)
|
2014-09-15 01:17:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("module %s: %s", m.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mode > GetModeNone {
|
|
|
|
// Get the module since we specified we should
|
|
|
|
if err := s.Get(source, update); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the directory where this module is so we can load it
|
|
|
|
dir, ok, err := s.Dir(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"module %s: not found, may need to be downloaded", m.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the configuration
|
|
|
|
c, err := config.LoadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"module %s: %s", m.Name, err)
|
|
|
|
}
|
2014-09-15 18:53:29 +02:00
|
|
|
|
|
|
|
children[m.Name] = NewTree(c)
|
|
|
|
children[m.Name].name = m.Name
|
2014-09-15 01:17:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Go through all the children and load them.
|
|
|
|
for _, c := range children {
|
|
|
|
if err := c.Load(s, mode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set our tree up
|
|
|
|
t.children = children
|
|
|
|
|
2014-09-14 02:45:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:14:37 +02:00
|
|
|
// String gives a nice output to describe the tree.
|
|
|
|
func (t *Tree) String() string {
|
|
|
|
var result bytes.Buffer
|
|
|
|
result.WriteString(t.Name() + "\n")
|
|
|
|
|
|
|
|
cs := t.Children()
|
|
|
|
if cs == nil {
|
|
|
|
result.WriteString(" not loaded")
|
|
|
|
} else {
|
|
|
|
// Go through each child and get its string value, then indent it
|
|
|
|
// by two.
|
|
|
|
for _, c := range cs {
|
|
|
|
r := strings.NewReader(c.String())
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
result.WriteString(" ")
|
|
|
|
result.WriteString(scanner.Text())
|
|
|
|
result.WriteString("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.String()
|
|
|
|
}
|
|
|
|
|
2014-09-14 02:45:56 +02:00
|
|
|
// Validate does semantic checks on the entire tree of configurations.
|
|
|
|
//
|
|
|
|
// This will call the respective config.Config.Validate() functions as well
|
|
|
|
// as verifying things such as parameters/outputs between the various modules.
|
2014-09-15 18:37:40 +02:00
|
|
|
//
|
|
|
|
// Load must be called prior to calling Validate or an error will be returned.
|
2014-09-14 02:45:56 +02:00
|
|
|
func (t *Tree) Validate() error {
|
2014-09-15 18:37:40 +02:00
|
|
|
if !t.Loaded() {
|
|
|
|
return fmt.Errorf("tree must be loaded before calling Validate")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate our configuration first.
|
|
|
|
if err := t.config.Validate(); err != nil {
|
|
|
|
return &ValidateError{
|
|
|
|
Name: []string{t.Name()},
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate all our children
|
|
|
|
for _, c := range t.Children() {
|
|
|
|
err := c.Validate()
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
verr, ok := err.(*ValidateError)
|
|
|
|
if !ok {
|
|
|
|
// Unknown error, just return...
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append ourselves to the error and then return
|
|
|
|
verr.Name = append(verr.Name, t.Name())
|
|
|
|
return verr
|
|
|
|
}
|
|
|
|
|
2014-09-14 02:45:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-09-15 18:37:40 +02:00
|
|
|
|
|
|
|
// ValidateError is an error returned by Tree.Validate if an error occurs
|
|
|
|
// with validation.
|
|
|
|
type ValidateError struct {
|
|
|
|
Name []string
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ValidateError) Error() string {
|
|
|
|
// Build up the name
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, n := range e.Name {
|
|
|
|
buf.WriteString(n)
|
|
|
|
buf.WriteString(".")
|
|
|
|
}
|
2014-09-15 18:53:29 +02:00
|
|
|
buf.Truncate(buf.Len() - 1)
|
2014-09-15 18:37:40 +02:00
|
|
|
|
|
|
|
// Format the value
|
|
|
|
return fmt.Sprintf("module %s: %s", buf.String(), e.Err)
|
|
|
|
}
|