7 "github.com/hashicorp/terraform/config/module"
8 "github.com/hashicorp/terraform/dag"
11 // DiffTransformer is a GraphTransformer that adds the elements of
12 // the diff to the graph.
14 // This transform is used for example by the ApplyGraphBuilder to ensure
15 // that only resources that are being modified are represented in the graph.
17 // Module and State is still required for the DiffTransformer for annotations
18 // since the Diff doesn't contain all the information required to build the
19 // complete graph (such as create-before-destroy information). The graph
20 // is built based on the diff first, though, ensuring that only resources
21 // that are being modified are present in the graph.
22 type DiffTransformer struct {
23 Concrete ConcreteResourceNodeFunc
30 func (t *DiffTransformer) Transform(g *Graph) error {
31 // If the diff is nil or empty (nil is empty) then do nothing
36 // Go through all the modules in the diff.
37 log.Printf("[TRACE] DiffTransformer: starting")
38 var nodes []dag.Vertex
39 for _, m := range t.Diff.Modules {
40 log.Printf("[TRACE] DiffTransformer: Module: %s", m)
41 // TODO: If this is a destroy diff then add a module destroy node
43 // Go through all the resources in this module.
44 for name, inst := range m.Resources {
45 log.Printf("[TRACE] DiffTransformer: Resource %q: %#v", name, inst)
47 // We have changes! This is a create or update operation.
48 // First grab the address so we have a unique way to
49 // reference this resource.
50 addr, err := parseResourceAddressInternal(name)
53 "Error parsing internal name, this is a bug: %q", name))
56 // Very important: add the module path for this resource to
57 // the address. Remove "root" from it.
58 addr.Path = m.Path[1:]
60 // If we're destroying, add the destroy node
61 if inst.Destroy || inst.GetDestroyDeposed() {
62 abstract := &NodeAbstractResource{Addr: addr}
63 g.Add(&NodeDestroyResource{NodeAbstractResource: abstract})
66 // If we have changes, then add the applyable version
67 if len(inst.Attributes) > 0 {
68 // Add the resource to the graph
69 abstract := &NodeAbstractResource{Addr: addr}
70 var node dag.Vertex = abstract
71 if f := t.Concrete; f != nil {
75 nodes = append(nodes, node)
80 // Add all the nodes to the graph
81 for _, n := range nodes {