From 0f8c0eb981264889628e12fbb9889ee011b5586a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 21 Jan 2015 14:39:16 -0800 Subject: [PATCH] terraform: config graph has providers --- terraform/{graph2.go => graph_config.go} | 74 +----------- terraform/graph_config_node.go | 106 ++++++++++++++++++ terraform/graph_config_node_test.go | 13 +++ .../{graph2_test.go => graph_config_test.go} | 2 + 4 files changed, 127 insertions(+), 68 deletions(-) rename terraform/{graph2.go => graph_config.go} (54%) create mode 100644 terraform/graph_config_node.go create mode 100644 terraform/graph_config_node_test.go rename terraform/{graph2_test.go => graph_config_test.go} (95%) diff --git a/terraform/graph2.go b/terraform/graph_config.go similarity index 54% rename from terraform/graph2.go rename to terraform/graph_config.go index 1e2eadd4e..167bd8799 100644 --- a/terraform/graph2.go +++ b/terraform/graph_config.go @@ -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. diff --git a/terraform/graph_config_node.go b/terraform/graph_config_node.go new file mode 100644 index 000000000..6f33ed057 --- /dev/null +++ b/terraform/graph_config_node.go @@ -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() +} diff --git a/terraform/graph_config_node_test.go b/terraform/graph_config_node_test.go new file mode 100644 index 000000000..bcfd4539b --- /dev/null +++ b/terraform/graph_config_node_test.go @@ -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) +} diff --git a/terraform/graph2_test.go b/terraform/graph_config_test.go similarity index 95% rename from terraform/graph2_test.go rename to terraform/graph_config_test.go index a781b53b6..6f3751d83 100644 --- a/terraform/graph2_test.go +++ b/terraform/graph_config_test.go @@ -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 `