From 5e767f63b997d3f8b893f34d2d05751dfb42409d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 30 Apr 2015 19:23:52 -0700 Subject: [PATCH] terraform: move module to its own file --- terraform/graph_config_node.go | 149 ------------------- terraform/graph_config_node_module.go | 158 +++++++++++++++++++++ terraform/graph_config_node_module_test.go | 41 ++++++ terraform/graph_config_node_test.go | 33 ----- terraform/transform_flatten.go | 12 +- 5 files changed, 204 insertions(+), 189 deletions(-) create mode 100644 terraform/graph_config_node_module.go create mode 100644 terraform/graph_config_node_module_test.go diff --git a/terraform/graph_config_node.go b/terraform/graph_config_node.go index 84882eae3..3e21e8052 100644 --- a/terraform/graph_config_node.go +++ b/terraform/graph_config_node.go @@ -5,7 +5,6 @@ import ( "strings" "github.com/hashicorp/terraform/config" - "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/dag" "github.com/hashicorp/terraform/dot" ) @@ -46,92 +45,6 @@ type GraphNodeTargetable interface { SetTargets([]ResourceAddress) } -// GraphNodeConfigModule represents a module within the configuration graph. -type GraphNodeConfigModule struct { - Path []string - Module *config.Module - Tree *module.Tree -} - -func (n *GraphNodeConfigModule) ConfigType() GraphNodeConfigType { - return GraphNodeConfigTypeModule -} - -func (n *GraphNodeConfigModule) DependableName() []string { - config := n.Tree.Config() - - result := make([]string, 1, len(config.Outputs)+1) - result[0] = n.Name() - for _, o := range config.Outputs { - result = append(result, fmt.Sprintf("%s.%s", n.Name(), o.Name)) - } - - return result -} - -func (n *GraphNodeConfigModule) DependentOn() []string { - vars := n.Module.RawConfig.Variables - result := make([]string, 0, len(vars)) - for _, v := range vars { - if vn := varNameForVar(v); vn != "" { - result = append(result, vn) - } - } - - return result -} - -func (n *GraphNodeConfigModule) Name() string { - return fmt.Sprintf("module.%s", n.Module.Name) -} - -// GraphNodeExpandable -func (n *GraphNodeConfigModule) Expand(b GraphBuilder) (GraphNodeSubgraph, error) { - // Build the graph first - graph, err := b.Build(n.Path) - if err != nil { - return nil, err - } - - // Add the parameters node to the module - t := &ModuleInputTransformer{Variables: make(map[string]string)} - if err := t.Transform(graph); err != nil { - return nil, err - } - - // Build the actual subgraph node - return &graphNodeModuleExpanded{ - Original: n, - Graph: graph, - InputConfig: n.Module.RawConfig, - Variables: t.Variables, - }, nil -} - -// GraphNodeExpandable -func (n *GraphNodeConfigModule) ProvidedBy() []string { - // Build up the list of providers by simply going over our configuration - // to find the providers that are configured there as well as the - // providers that the resources use. - config := n.Tree.Config() - providers := make(map[string]struct{}) - for _, p := range config.ProviderConfigs { - providers[p.Name] = struct{}{} - } - for _, r := range config.Resources { - providers[resourceProvider(r.Type, r.Provider)] = struct{}{} - } - - // Turn the map into a string. This makes sure that the list is - // de-dupped since we could be going over potentially many resources. - result := make([]string, 0, len(providers)) - for p, _ := range providers { - result = append(result, p) - } - - return result -} - // GraphNodeConfigOutput represents an output configured within the // configuration. type GraphNodeConfigOutput struct { @@ -622,65 +535,3 @@ func (n *graphNodeResourceDestroy) destroyIncludePrimary( return false } - -// graphNodeModuleExpanded represents a module where the graph has -// been expanded. It stores the graph of the module as well as a reference -// to the map of variables. -type graphNodeModuleExpanded struct { - Original dag.Vertex - Graph *Graph - InputConfig *config.RawConfig - - // Variables is a map of the input variables. This reference should - // be shared with ModuleInputTransformer in order to create a connection - // where the variables are set properly. - Variables map[string]string -} - -func (n *graphNodeModuleExpanded) Name() string { - return fmt.Sprintf("%s (expanded)", dag.VertexName(n.Original)) -} - -func (n *graphNodeModuleExpanded) ConfigType() GraphNodeConfigType { - return GraphNodeConfigTypeModule -} - -// GraphNodeDotter impl. -func (n *graphNodeModuleExpanded) DotNode(name string, opts *GraphDotOpts) *dot.Node { - return dot.NewNode(name, map[string]string{ - "label": dag.VertexName(n.Original), - "shape": "component", - }) -} - -// GraphNodeEvalable impl. -func (n *graphNodeModuleExpanded) EvalTree() EvalNode { - var resourceConfig *ResourceConfig - return &EvalSequence{ - Nodes: []EvalNode{ - &EvalInterpolate{ - Config: n.InputConfig, - Output: &resourceConfig, - }, - - &EvalVariableBlock{ - Config: &resourceConfig, - Variables: n.Variables, - }, - - &EvalOpFilter{ - Ops: []walkOperation{walkPlanDestroy}, - Node: &EvalSequence{ - Nodes: []EvalNode{ - &EvalDiffDestroyModule{Path: n.Graph.Path}, - }, - }, - }, - }, - } -} - -// GraphNodeSubgraph impl. -func (n *graphNodeModuleExpanded) Subgraph() *Graph { - return n.Graph -} diff --git a/terraform/graph_config_node_module.go b/terraform/graph_config_node_module.go new file mode 100644 index 000000000..22ed3493f --- /dev/null +++ b/terraform/graph_config_node_module.go @@ -0,0 +1,158 @@ +package terraform + +import ( + "fmt" + + "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/config/module" + "github.com/hashicorp/terraform/dag" + "github.com/hashicorp/terraform/dot" +) + +// GraphNodeConfigModule represents a module within the configuration graph. +type GraphNodeConfigModule struct { + Path []string + Module *config.Module + Tree *module.Tree +} + +func (n *GraphNodeConfigModule) ConfigType() GraphNodeConfigType { + return GraphNodeConfigTypeModule +} + +func (n *GraphNodeConfigModule) DependableName() []string { + config := n.Tree.Config() + + result := make([]string, 1, len(config.Outputs)+1) + result[0] = n.Name() + for _, o := range config.Outputs { + result = append(result, fmt.Sprintf("%s.%s", n.Name(), o.Name)) + } + + return result +} + +func (n *GraphNodeConfigModule) DependentOn() []string { + vars := n.Module.RawConfig.Variables + result := make([]string, 0, len(vars)) + for _, v := range vars { + if vn := varNameForVar(v); vn != "" { + result = append(result, vn) + } + } + + return result +} + +func (n *GraphNodeConfigModule) Name() string { + return fmt.Sprintf("module.%s", n.Module.Name) +} + +// GraphNodeExpandable +func (n *GraphNodeConfigModule) Expand(b GraphBuilder) (GraphNodeSubgraph, error) { + // Build the graph first + graph, err := b.Build(n.Path) + if err != nil { + return nil, err + } + + // Add the parameters node to the module + t := &ModuleInputTransformer{Variables: make(map[string]string)} + if err := t.Transform(graph); err != nil { + return nil, err + } + + // Build the actual subgraph node + return &graphNodeModuleExpanded{ + Original: n, + Graph: graph, + InputConfig: n.Module.RawConfig, + Variables: t.Variables, + }, nil +} + +// GraphNodeExpandable +func (n *GraphNodeConfigModule) ProvidedBy() []string { + // Build up the list of providers by simply going over our configuration + // to find the providers that are configured there as well as the + // providers that the resources use. + config := n.Tree.Config() + providers := make(map[string]struct{}) + for _, p := range config.ProviderConfigs { + providers[p.Name] = struct{}{} + } + for _, r := range config.Resources { + providers[resourceProvider(r.Type, r.Provider)] = struct{}{} + } + + // Turn the map into a string. This makes sure that the list is + // de-dupped since we could be going over potentially many resources. + result := make([]string, 0, len(providers)) + for p, _ := range providers { + result = append(result, p) + } + + return result +} + +// graphNodeModuleExpanded represents a module where the graph has +// been expanded. It stores the graph of the module as well as a reference +// to the map of variables. +type graphNodeModuleExpanded struct { + Original dag.Vertex + Graph *Graph + InputConfig *config.RawConfig + + // Variables is a map of the input variables. This reference should + // be shared with ModuleInputTransformer in order to create a connection + // where the variables are set properly. + Variables map[string]string +} + +func (n *graphNodeModuleExpanded) Name() string { + return fmt.Sprintf("%s (expanded)", dag.VertexName(n.Original)) +} + +func (n *graphNodeModuleExpanded) ConfigType() GraphNodeConfigType { + return GraphNodeConfigTypeModule +} + +// GraphNodeDotter impl. +func (n *graphNodeModuleExpanded) DotNode(name string, opts *GraphDotOpts) *dot.Node { + return dot.NewNode(name, map[string]string{ + "label": dag.VertexName(n.Original), + "shape": "component", + }) +} + +// GraphNodeEvalable impl. +func (n *graphNodeModuleExpanded) EvalTree() EvalNode { + var resourceConfig *ResourceConfig + return &EvalSequence{ + Nodes: []EvalNode{ + &EvalInterpolate{ + Config: n.InputConfig, + Output: &resourceConfig, + }, + + &EvalVariableBlock{ + Config: &resourceConfig, + Variables: n.Variables, + }, + + &EvalOpFilter{ + Ops: []walkOperation{walkPlanDestroy}, + Node: &EvalSequence{ + Nodes: []EvalNode{ + &EvalDiffDestroyModule{Path: n.Graph.Path}, + }, + }, + }, + }, + } +} + +// GraphNodeSubgraph impl. +func (n *graphNodeModuleExpanded) Subgraph() *Graph { + return n.Graph +} diff --git a/terraform/graph_config_node_module_test.go b/terraform/graph_config_node_module_test.go new file mode 100644 index 000000000..f30f422a1 --- /dev/null +++ b/terraform/graph_config_node_module_test.go @@ -0,0 +1,41 @@ +package terraform + +import ( + "strings" + "testing" + + "github.com/hashicorp/terraform/config" + "github.com/hashicorp/terraform/dag" +) + +func TestGraphNodeConfigModule_impl(t *testing.T) { + var _ dag.Vertex = new(GraphNodeConfigModule) + var _ dag.NamedVertex = new(GraphNodeConfigModule) + var _ graphNodeConfig = new(GraphNodeConfigModule) + var _ GraphNodeExpandable = new(GraphNodeConfigModule) +} + +func TestGraphNodeConfigModuleExpand(t *testing.T) { + mod := testModule(t, "graph-node-module-expand") + + node := &GraphNodeConfigModule{ + Path: []string{RootModuleName, "child"}, + Module: &config.Module{}, + Tree: nil, + } + + g, err := node.Expand(&BasicGraphBuilder{ + Steps: []GraphTransformer{ + &ConfigTransformer{Module: mod}, + }, + }) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(g.Subgraph().String()) + expected := strings.TrimSpace(testGraphNodeModuleExpandStr) + if actual != expected { + t.Fatalf("bad:\n\n%s", actual) + } +} diff --git a/terraform/graph_config_node_test.go b/terraform/graph_config_node_test.go index 22653dffd..279313516 100644 --- a/terraform/graph_config_node_test.go +++ b/terraform/graph_config_node_test.go @@ -2,45 +2,12 @@ package terraform import ( "reflect" - "strings" "testing" "github.com/hashicorp/terraform/config" "github.com/hashicorp/terraform/dag" ) -func TestGraphNodeConfigModule_impl(t *testing.T) { - var _ dag.Vertex = new(GraphNodeConfigModule) - var _ dag.NamedVertex = new(GraphNodeConfigModule) - var _ graphNodeConfig = new(GraphNodeConfigModule) - var _ GraphNodeExpandable = new(GraphNodeConfigModule) -} - -func TestGraphNodeConfigModuleExpand(t *testing.T) { - mod := testModule(t, "graph-node-module-expand") - - node := &GraphNodeConfigModule{ - Path: []string{RootModuleName, "child"}, - Module: &config.Module{}, - Tree: nil, - } - - g, err := node.Expand(&BasicGraphBuilder{ - Steps: []GraphTransformer{ - &ConfigTransformer{Module: mod}, - }, - }) - if err != nil { - t.Fatalf("err: %s", err) - } - - actual := strings.TrimSpace(g.Subgraph().String()) - expected := strings.TrimSpace(testGraphNodeModuleExpandStr) - if actual != expected { - t.Fatalf("bad:\n\n%s", actual) - } -} - func TestGraphNodeConfigOutput_impl(t *testing.T) { var _ dag.Vertex = new(GraphNodeConfigOutput) var _ dag.NamedVertex = new(GraphNodeConfigOutput) diff --git a/terraform/transform_flatten.go b/terraform/transform_flatten.go index 93161ceab..ecdef22fe 100644 --- a/terraform/transform_flatten.go +++ b/terraform/transform_flatten.go @@ -3,10 +3,7 @@ package terraform // GraphNodeFlattenable must be implemented by nodes that can be flattened // into the graph. type GraphNodeFlattenable interface { - GraphNodeSubgraph - - // Flatten should return true if this should be flattened. - Flatten() bool + FlattenGraph() *Graph } // FlattenTransform is a transformer that goes through the graph, finds @@ -22,12 +19,13 @@ func (t *FlattenTransform) Transform(g *Graph) error { } // If we don't want to be flattened, don't do it - if !fn.Flatten() { + subgraph := fn.FlattenGraph() + if subgraph == nil { continue } - // Get the subgraph and flatten it into this one - subgraph := fn.Subgraph() + // Flatten the subgraph into this one. Keep any existing + // connections that existed. for _, sv := range subgraph.Vertices() { g.Add(sv) }