opentofu/terraform/graph_config.go

86 lines
2.3 KiB
Go
Raw Normal View History

package terraform
import (
"errors"
"fmt"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
2015-01-22 19:12:32 -06:00
"github.com/hashicorp/terraform/dag"
)
// Graph takes a module tree and builds a logical graph of all the nodes
// in that module.
2015-01-22 19:12:32 -06:00
func Graph2(mod *module.Tree) (*dag.Graph, error) {
// A module is required and also must be completely loaded.
if mod == nil {
return nil, errors.New("module must not be nil")
}
if !mod.Loaded() {
return nil, errors.New("module must be loaded")
}
// Get the configuration for this module
config := mod.Config()
// Create the node list we'll use for the graph
nodes := make([]graphNodeConfig, 0,
(len(config.ProviderConfigs)+len(config.Modules)+len(config.Resources))*2)
2015-01-21 16:39:16 -06:00
// Write all the provider configs out
for _, pc := range config.ProviderConfigs {
nodes = append(nodes, &GraphNodeConfigProvider{Provider: pc})
}
// Write all the resources out
for _, r := range config.Resources {
2015-01-21 16:39:16 -06:00
nodes = append(nodes, &GraphNodeConfigResource{Resource: r})
}
// Write all the modules out
for _, m := range config.Modules {
nodes = append(nodes, &GraphNodeConfigModule{Module: m})
}
// Build the full map of the var names to the nodes.
2015-01-22 19:12:32 -06:00
fullMap := make(map[string]dag.Node)
for _, n := range nodes {
fullMap[n.VarName()] = n
}
// Go through all the nodes and build up the actual dependency map. We
// do this by getting the variables that each node depends on and then
// building the dep map based on the fullMap which contains the mapping
// of var names to the actual node with that name.
for _, n := range nodes {
2015-01-22 19:12:32 -06:00
m := make(map[string]dag.Node)
for _, id := range n.Variables() {
m[id] = fullMap[id]
}
n.setDepMap(m)
}
// Build the graph and return it
2015-01-22 19:12:32 -06:00
g := &dag.Graph{Nodes: make([]dag.Node, 0, len(nodes))}
for _, n := range nodes {
g.Nodes = append(g.Nodes, n)
}
return g, nil
}
// varNameForVar returns the VarName value for an interpolated variable.
// This value is compared to the VarName() value for the nodes within the
// graph to build the graph edges.
func varNameForVar(raw config.InterpolatedVariable) string {
switch v := raw.(type) {
case *config.ModuleVariable:
return fmt.Sprintf("module.%s", v.Name)
case *config.ResourceVariable:
return v.ResourceId()
default:
return ""
}
}