2016-09-16 22:56:10 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2016-11-05 02:58:03 +01:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
2016-09-16 22:56:10 +02:00
|
|
|
)
|
|
|
|
|
2016-09-16 23:00:21 +02:00
|
|
|
// ModuleVariableTransformer is a GraphTransformer that adds all the variables
|
2016-09-16 22:56:10 +02:00
|
|
|
// in the configuration to the graph.
|
|
|
|
//
|
2016-11-05 02:58:03 +01:00
|
|
|
// This only adds variables that are referenced by other thigns in the graph.
|
|
|
|
// If a module variable is not referenced, it won't be added to the graph.
|
2016-09-16 23:00:21 +02:00
|
|
|
type ModuleVariableTransformer struct {
|
2016-09-16 22:56:10 +02:00
|
|
|
Module *module.Tree
|
2016-11-05 02:58:03 +01:00
|
|
|
|
|
|
|
DisablePrune bool // True if pruning unreferenced should be disabled
|
2016-09-16 22:56:10 +02:00
|
|
|
}
|
|
|
|
|
2016-09-16 23:00:21 +02:00
|
|
|
func (t *ModuleVariableTransformer) Transform(g *Graph) error {
|
2016-09-16 22:56:10 +02:00
|
|
|
return t.transform(g, nil, t.Module)
|
|
|
|
}
|
|
|
|
|
2016-09-16 23:00:21 +02:00
|
|
|
func (t *ModuleVariableTransformer) transform(g *Graph, parent, m *module.Tree) error {
|
2016-09-16 22:56:10 +02:00
|
|
|
// If no config, no variables
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-05 02:58:03 +01:00
|
|
|
// Transform all the children. This must be done BEFORE the transform
|
|
|
|
// above since child module variables can reference parent module variables.
|
|
|
|
for _, c := range m.Children() {
|
|
|
|
if err := t.transform(g, m, c); err != nil {
|
2016-09-24 02:20:25 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-09-23 18:19:09 +02:00
|
|
|
}
|
|
|
|
|
2016-11-05 02:58:03 +01:00
|
|
|
// If we have a parent, we can determine if a module variable is being
|
|
|
|
// used, so we transform this.
|
|
|
|
if parent != nil {
|
|
|
|
if err := t.transformSingle(g, parent, m); err != nil {
|
2016-09-16 22:56:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-23 18:19:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ModuleVariableTransformer) transformSingle(g *Graph, parent, m *module.Tree) error {
|
2016-09-16 22:56:10 +02:00
|
|
|
// If we have no vars, we're done!
|
|
|
|
vars := m.Config().Variables
|
|
|
|
if len(vars) == 0 {
|
2016-09-23 18:19:09 +02:00
|
|
|
log.Printf("[TRACE] Module %#v has no variables, skipping.", m.Path())
|
2016-09-16 22:56:10 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for usage of this module
|
|
|
|
var mod *config.Module
|
|
|
|
for _, modUse := range parent.Config().Modules {
|
|
|
|
if modUse.Name == m.Name() {
|
|
|
|
mod = modUse
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mod == nil {
|
2016-09-23 18:19:09 +02:00
|
|
|
log.Printf("[INFO] Module %#v not used, not adding variables", m.Path())
|
2016-09-16 22:56:10 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-05 02:58:03 +01:00
|
|
|
// Build the reference map so we can determine if we're referencing things.
|
|
|
|
refMap := NewReferenceMap(g.Vertices())
|
|
|
|
|
2016-09-16 22:56:10 +02:00
|
|
|
// Add all variables here
|
|
|
|
for _, v := range vars {
|
|
|
|
// Determine the value of the variable. If it isn't in the
|
|
|
|
// configuration then it was never set and that's not a problem.
|
|
|
|
var value *config.RawConfig
|
|
|
|
if raw, ok := mod.RawConfig.Raw[v.Name]; ok {
|
|
|
|
var err error
|
|
|
|
value, err = config.NewRawConfig(map[string]interface{}{
|
|
|
|
v.Name: raw,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// This shouldn't happen because it is already in
|
|
|
|
// a RawConfig above meaning it worked once before.
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the node.
|
|
|
|
//
|
|
|
|
// NOTE: For now this is just an "applyable" variable. As we build
|
|
|
|
// new graph builders for the other operations I suspect we'll
|
|
|
|
// find a way to parameterize this, require new transforms, etc.
|
2016-09-16 23:00:21 +02:00
|
|
|
node := &NodeApplyableModuleVariable{
|
2016-09-16 22:56:10 +02:00
|
|
|
PathValue: normalizeModulePath(m.Path()),
|
|
|
|
Config: v,
|
|
|
|
Value: value,
|
|
|
|
Module: t.Module,
|
|
|
|
}
|
|
|
|
|
2016-11-05 02:58:03 +01:00
|
|
|
if !t.DisablePrune {
|
|
|
|
// If the node is not referenced by anything, then we don't need
|
|
|
|
// to include it since it won't be used.
|
|
|
|
if matches := refMap.ReferencedBy(node); len(matches) == 0 {
|
|
|
|
log.Printf(
|
|
|
|
"[INFO] Not including %q in graph, nothing depends on it",
|
|
|
|
dag.VertexName(node))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 22:56:10 +02:00
|
|
|
// Add it!
|
|
|
|
g.Add(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|