2016-09-13 19:56:37 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-17 01:30:29 +02:00
|
|
|
"log"
|
2016-09-14 02:11:34 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2016-09-21 23:30:41 +02:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
2016-09-13 19:56:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// DiffTransformer is a GraphTransformer that adds the elements of
|
|
|
|
// the diff to the graph.
|
|
|
|
//
|
|
|
|
// This transform is used for example by the ApplyGraphBuilder to ensure
|
|
|
|
// that only resources that are being modified are represented in the graph.
|
2016-09-14 02:11:34 +02:00
|
|
|
//
|
2016-09-14 20:44:41 +02:00
|
|
|
// Module and State is still required for the DiffTransformer for annotations
|
2016-09-14 02:11:34 +02:00
|
|
|
// since the Diff doesn't contain all the information required to build the
|
|
|
|
// complete graph (such as create-before-destroy information). The graph
|
|
|
|
// is built based on the diff first, though, ensuring that only resources
|
|
|
|
// that are being modified are present in the graph.
|
2016-09-13 19:56:37 +02:00
|
|
|
type DiffTransformer struct {
|
2016-09-21 23:30:41 +02:00
|
|
|
Concrete ConcreteResourceNodeFunc
|
|
|
|
|
2016-09-14 02:11:34 +02:00
|
|
|
Diff *Diff
|
2016-09-14 20:44:41 +02:00
|
|
|
Module *module.Tree
|
2016-09-14 02:11:34 +02:00
|
|
|
State *State
|
2016-09-13 19:56:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *DiffTransformer) Transform(g *Graph) error {
|
|
|
|
// If the diff is nil or empty (nil is empty) then do nothing
|
|
|
|
if t.Diff.Empty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through all the modules in the diff.
|
2016-09-17 01:30:29 +02:00
|
|
|
log.Printf("[TRACE] DiffTransformer: starting")
|
2016-09-21 23:30:41 +02:00
|
|
|
var nodes []dag.Vertex
|
2016-09-13 19:56:37 +02:00
|
|
|
for _, m := range t.Diff.Modules {
|
2016-09-17 01:30:29 +02:00
|
|
|
log.Printf("[TRACE] DiffTransformer: Module: %s", m)
|
2016-09-13 19:56:37 +02:00
|
|
|
// TODO: If this is a destroy diff then add a module destroy node
|
|
|
|
|
|
|
|
// Go through all the resources in this module.
|
|
|
|
for name, inst := range m.Resources {
|
2016-09-17 01:30:29 +02:00
|
|
|
log.Printf("[TRACE] DiffTransformer: Resource %q: %#v", name, inst)
|
2016-09-17 02:29:36 +02:00
|
|
|
|
2016-09-13 19:56:37 +02:00
|
|
|
// We have changes! This is a create or update operation.
|
|
|
|
// First grab the address so we have a unique way to
|
|
|
|
// reference this resource.
|
|
|
|
addr, err := parseResourceAddressInternal(name)
|
|
|
|
if err != nil {
|
2016-09-14 02:11:34 +02:00
|
|
|
panic(fmt.Sprintf(
|
|
|
|
"Error parsing internal name, this is a bug: %q", name))
|
2016-09-13 19:56:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-14 02:11:34 +02:00
|
|
|
// Very important: add the module path for this resource to
|
|
|
|
// the address. Remove "root" from it.
|
|
|
|
addr.Path = m.Path[1:]
|
|
|
|
|
2016-09-17 05:26:10 +02:00
|
|
|
// If we're destroying, add the destroy node
|
2016-11-28 22:41:22 +01:00
|
|
|
if inst.Destroy || inst.GetDestroyDeposed() {
|
2016-10-21 08:12:07 +02:00
|
|
|
abstract := &NodeAbstractResource{Addr: addr}
|
2016-09-22 03:48:21 +02:00
|
|
|
g.Add(&NodeDestroyResource{NodeAbstractResource: abstract})
|
2016-09-17 05:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have changes, then add the applyable version
|
|
|
|
if len(inst.Attributes) > 0 {
|
|
|
|
// Add the resource to the graph
|
2016-09-21 23:30:41 +02:00
|
|
|
abstract := &NodeAbstractResource{Addr: addr}
|
|
|
|
var node dag.Vertex = abstract
|
|
|
|
if f := t.Concrete; f != nil {
|
|
|
|
node = f(abstract)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes = append(nodes, node)
|
2016-09-17 05:26:10 +02:00
|
|
|
}
|
2016-09-13 19:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 02:11:34 +02:00
|
|
|
// Add all the nodes to the graph
|
|
|
|
for _, n := range nodes {
|
|
|
|
g.Add(n)
|
|
|
|
}
|
|
|
|
|
2016-09-13 19:56:37 +02:00
|
|
|
return nil
|
|
|
|
}
|