terraform: config graph has providers

This commit is contained in:
Mitchell Hashimoto 2015-01-21 14:39:16 -08:00
parent 6b00633ed1
commit 0f8c0eb981
4 changed files with 127 additions and 68 deletions

View File

@ -27,11 +27,14 @@ func Graph2(mod *module.Tree) (*depgraph.Graph, error) {
nodes := make([]graphNodeConfig, 0,
(len(config.Modules)+len(config.Resources))*2)
// 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 {
nodes = append(nodes, &GraphNodeConfigResource{
Resource: r,
})
nodes = append(nodes, &GraphNodeConfigResource{Resource: r})
}
// Write all the modules out
@ -66,71 +69,6 @@ func Graph2(mod *module.Tree) (*depgraph.Graph, error) {
return g, nil
}
// graphNodeConfig is an interface that all graph nodes for the
// configuration graph need to implement in order to build the variable
// dependencies properly.
type graphNodeConfig interface {
depgraph.Node
// Variables returns the full list of variables that this node
// depends on.
Variables() map[string]config.InterpolatedVariable
// VarName returns the name that is used to identify a variable
// maps to this node. It should match the result of the
// `VarName` function.
VarName() string
// setDepMap sets the dependency map for this node. If the node is
// nil, then it wasn't found.
setDepMap(map[string]depgraph.Node)
}
// GraphNodeConfigResource represents a resource within the configuration
// graph.
type GraphNodeConfigResource struct {
Resource *config.Resource
DepMap map[string]depgraph.Node
}
func (n *GraphNodeConfigResource) Deps() []depgraph.Node {
r := make([]depgraph.Node, 0, len(n.DepMap))
for _, v := range n.DepMap {
if v != nil {
r = append(r, v)
}
}
return r
}
func (n *GraphNodeConfigResource) Name() string {
return n.Resource.Id()
}
func (n *GraphNodeConfigResource) Variables() map[string]config.InterpolatedVariable {
var m map[string]config.InterpolatedVariable
if n.Resource != nil {
m = make(map[string]config.InterpolatedVariable)
for k, v := range n.Resource.RawCount.Variables {
m[k] = v
}
for k, v := range n.Resource.RawConfig.Variables {
m[k] = v
}
}
return m
}
func (n *GraphNodeConfigResource) VarName() string {
return n.Resource.Id()
}
func (n *GraphNodeConfigResource) setDepMap(m map[string]depgraph.Node) {
n.DepMap = m
}
// 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.

View File

@ -0,0 +1,106 @@
package terraform
import (
"fmt"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/depgraph2"
)
// graphNodeConfig is an interface that all graph nodes for the
// configuration graph need to implement in order to build the variable
// dependencies properly.
type graphNodeConfig interface {
depgraph.Node
// Variables returns the full list of variables that this node
// depends on.
Variables() map[string]config.InterpolatedVariable
// VarName returns the name that is used to identify a variable
// maps to this node. It should match the result of the
// `VarName` function.
VarName() string
// depMap and setDepMap are used to get and set the dependency map
// for this node. This is used to modify the dependencies. The key of
// this map should be the VarName() of graphNodeConfig.
depMap() map[string]depgraph.Node
setDepMap(map[string]depgraph.Node)
}
// graphNodeConfigBasicDepMap is a struct that provides the Deps(),
// depMap(), and setDepMap() functions to help satisfy the graphNodeConfig
// interface. This struct is meant to be embedded into other nodes to get
// these features for free.
type graphNodeConfigBasicDepMap struct {
DepMap map[string]depgraph.Node
}
func (n *graphNodeConfigBasicDepMap) Deps() []depgraph.Node {
r := make([]depgraph.Node, 0, len(n.DepMap))
for _, v := range n.DepMap {
if v != nil {
r = append(r, v)
}
}
return r
}
func (n *graphNodeConfigBasicDepMap) depMap() map[string]depgraph.Node {
return n.DepMap
}
func (n *graphNodeConfigBasicDepMap) setDepMap(m map[string]depgraph.Node) {
n.DepMap = m
}
// GraphNodeConfigProvider represents a resource within the config graph.
type GraphNodeConfigProvider struct {
graphNodeConfigBasicDepMap
Provider *config.ProviderConfig
}
func (n *GraphNodeConfigProvider) Name() string {
return fmt.Sprintf("provider.%s", n.Provider.Name)
}
func (n *GraphNodeConfigProvider) Variables() map[string]config.InterpolatedVariable {
return n.Provider.RawConfig.Variables
}
func (n *GraphNodeConfigProvider) VarName() string {
return "never valid"
}
// GraphNodeConfigResource represents a resource within the config graph.
type GraphNodeConfigResource struct {
graphNodeConfigBasicDepMap
Resource *config.Resource
}
func (n *GraphNodeConfigResource) Name() string {
return n.Resource.Id()
}
func (n *GraphNodeConfigResource) Variables() map[string]config.InterpolatedVariable {
var m map[string]config.InterpolatedVariable
if n.Resource != nil {
m = make(map[string]config.InterpolatedVariable)
for k, v := range n.Resource.RawCount.Variables {
m[k] = v
}
for k, v := range n.Resource.RawConfig.Variables {
m[k] = v
}
}
return m
}
func (n *GraphNodeConfigResource) VarName() string {
return n.Resource.Id()
}

View File

@ -0,0 +1,13 @@
package terraform
import (
"testing"
"github.com/hashicorp/terraform/depgraph2"
)
func TestGraphNodeConfigResource_impl(t *testing.T) {
var _ depgraph.Node = new(GraphNodeConfigResource)
var _ depgraph.NamedNode = new(GraphNodeConfigResource)
var _ graphNodeConfig = new(GraphNodeConfigResource)
}

View File

@ -47,4 +47,6 @@ aws_load_balancer.weblb
aws_instance.web
aws_security_group.firewall
openstack_floating_ip.random
provider.aws
openstack_floating_ip.random
`