From e9e8304e95da3a71541c25511b5b9bfda59d6124 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 15 Sep 2016 11:39:11 -0700 Subject: [PATCH] terraform: new output transform that isn't used yet --- terraform/transform_output.go | 99 ++----------------- terraform/transform_output_orphan.go | 98 ++++++++++++++++++ ...est.go => transform_output_orphan_test.go} | 0 3 files changed, 108 insertions(+), 89 deletions(-) create mode 100644 terraform/transform_output_orphan.go rename terraform/{transform_output_test.go => transform_output_orphan_test.go} (100%) diff --git a/terraform/transform_output.go b/terraform/transform_output.go index d3e839ce1c..0aed7b017c 100644 --- a/terraform/transform_output.go +++ b/terraform/transform_output.go @@ -1,98 +1,19 @@ package terraform import ( - "fmt" - - "github.com/hashicorp/terraform/dag" + "github.com/hashicorp/terraform/config/module" ) -// GraphNodeOutput is an interface that nodes that are outputs must -// implement. The OutputName returned is the name of the output key -// that they manage. -type GraphNodeOutput interface { - OutputName() string +// OutputTransformer is a GraphTransformer that adds all the outputs +// in the configuration to the graph. +// +// This is done for the apply graph builder even if dependent nodes +// aren't changing since there is no downside: the state will be available +// even if the dependent items aren't changing. +type OutputTransformer struct { + Module *module.Tree } -// AddOutputOrphanTransformer is a transformer that adds output orphans -// to the graph. Output orphans are outputs that are no longer in the -// configuration and therefore need to be removed from the state. -type AddOutputOrphanTransformer struct { - State *State -} - -func (t *AddOutputOrphanTransformer) Transform(g *Graph) error { - // Get the state for this module. If we have no state, we have no orphans - state := t.State.ModuleByPath(g.Path) - if state == nil { - return nil - } - - // Create the set of outputs we do have in the graph - found := make(map[string]struct{}) - for _, v := range g.Vertices() { - on, ok := v.(GraphNodeOutput) - if !ok { - continue - } - - found[on.OutputName()] = struct{}{} - } - - // Go over all the outputs. If we don't have a graph node for it, - // create it. It doesn't need to depend on anything, since its just - // setting it empty. - for k, _ := range state.Outputs { - if _, ok := found[k]; ok { - continue - } - - g.Add(&graphNodeOrphanOutput{OutputName: k}) - } - +func (t *OutputTransformer) Transform(g *Graph) error { return nil } - -type graphNodeOrphanOutput struct { - OutputName string -} - -func (n *graphNodeOrphanOutput) Name() string { - return fmt.Sprintf("output.%s (orphan)", n.OutputName) -} - -func (n *graphNodeOrphanOutput) EvalTree() EvalNode { - return &EvalOpFilter{ - Ops: []walkOperation{walkApply, walkDestroy, walkRefresh}, - Node: &EvalDeleteOutput{ - Name: n.OutputName, - }, - } -} - -// GraphNodeFlattenable impl. -func (n *graphNodeOrphanOutput) Flatten(p []string) (dag.Vertex, error) { - return &graphNodeOrphanOutputFlat{ - graphNodeOrphanOutput: n, - PathValue: p, - }, nil -} - -type graphNodeOrphanOutputFlat struct { - *graphNodeOrphanOutput - - PathValue []string -} - -func (n *graphNodeOrphanOutputFlat) Name() string { - return fmt.Sprintf( - "%s.%s", modulePrefixStr(n.PathValue), n.graphNodeOrphanOutput.Name()) -} - -func (n *graphNodeOrphanOutputFlat) EvalTree() EvalNode { - return &EvalOpFilter{ - Ops: []walkOperation{walkApply, walkDestroy, walkRefresh}, - Node: &EvalDeleteOutput{ - Name: n.OutputName, - }, - } -} diff --git a/terraform/transform_output_orphan.go b/terraform/transform_output_orphan.go new file mode 100644 index 0000000000..d3e839ce1c --- /dev/null +++ b/terraform/transform_output_orphan.go @@ -0,0 +1,98 @@ +package terraform + +import ( + "fmt" + + "github.com/hashicorp/terraform/dag" +) + +// GraphNodeOutput is an interface that nodes that are outputs must +// implement. The OutputName returned is the name of the output key +// that they manage. +type GraphNodeOutput interface { + OutputName() string +} + +// AddOutputOrphanTransformer is a transformer that adds output orphans +// to the graph. Output orphans are outputs that are no longer in the +// configuration and therefore need to be removed from the state. +type AddOutputOrphanTransformer struct { + State *State +} + +func (t *AddOutputOrphanTransformer) Transform(g *Graph) error { + // Get the state for this module. If we have no state, we have no orphans + state := t.State.ModuleByPath(g.Path) + if state == nil { + return nil + } + + // Create the set of outputs we do have in the graph + found := make(map[string]struct{}) + for _, v := range g.Vertices() { + on, ok := v.(GraphNodeOutput) + if !ok { + continue + } + + found[on.OutputName()] = struct{}{} + } + + // Go over all the outputs. If we don't have a graph node for it, + // create it. It doesn't need to depend on anything, since its just + // setting it empty. + for k, _ := range state.Outputs { + if _, ok := found[k]; ok { + continue + } + + g.Add(&graphNodeOrphanOutput{OutputName: k}) + } + + return nil +} + +type graphNodeOrphanOutput struct { + OutputName string +} + +func (n *graphNodeOrphanOutput) Name() string { + return fmt.Sprintf("output.%s (orphan)", n.OutputName) +} + +func (n *graphNodeOrphanOutput) EvalTree() EvalNode { + return &EvalOpFilter{ + Ops: []walkOperation{walkApply, walkDestroy, walkRefresh}, + Node: &EvalDeleteOutput{ + Name: n.OutputName, + }, + } +} + +// GraphNodeFlattenable impl. +func (n *graphNodeOrphanOutput) Flatten(p []string) (dag.Vertex, error) { + return &graphNodeOrphanOutputFlat{ + graphNodeOrphanOutput: n, + PathValue: p, + }, nil +} + +type graphNodeOrphanOutputFlat struct { + *graphNodeOrphanOutput + + PathValue []string +} + +func (n *graphNodeOrphanOutputFlat) Name() string { + return fmt.Sprintf( + "%s.%s", modulePrefixStr(n.PathValue), n.graphNodeOrphanOutput.Name()) +} + +func (n *graphNodeOrphanOutputFlat) EvalTree() EvalNode { + return &EvalOpFilter{ + Ops: []walkOperation{walkApply, walkDestroy, walkRefresh}, + Node: &EvalDeleteOutput{ + Name: n.OutputName, + }, + } +} diff --git a/terraform/transform_output_test.go b/terraform/transform_output_orphan_test.go similarity index 100% rename from terraform/transform_output_test.go rename to terraform/transform_output_orphan_test.go