summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/terraform/terraform/transform_config_flat.go
blob: 92f9888d6d5c4e4e5fce878fbd64fa67314928aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package terraform

import (
	"errors"

	"github.com/hashicorp/terraform/config/module"
	"github.com/hashicorp/terraform/dag"
)

// FlatConfigTransformer is a GraphTransformer that adds the configuration
// to the graph. The module used to configure this transformer must be
// the root module.
//
// This transform adds the nodes but doesn't connect any of the references.
// The ReferenceTransformer should be used for that.
//
// NOTE: In relation to ConfigTransformer: this is a newer generation config
// transformer. It puts the _entire_ config into the graph (there is no
// "flattening" step as before).
type FlatConfigTransformer struct {
	Concrete ConcreteResourceNodeFunc // What to turn resources into

	Module *module.Tree
}

func (t *FlatConfigTransformer) Transform(g *Graph) error {
	// If no module, we do nothing
	if t.Module == nil {
		return nil
	}

	// If the module is not loaded, that is an error
	if !t.Module.Loaded() {
		return errors.New("module must be loaded")
	}

	return t.transform(g, t.Module)
}

func (t *FlatConfigTransformer) transform(g *Graph, m *module.Tree) error {
	// If no module, no problem
	if m == nil {
		return nil
	}

	// Transform all the children.
	for _, c := range m.Children() {
		if err := t.transform(g, c); err != nil {
			return err
		}
	}

	// Get the configuration for this module
	config := m.Config()

	// Write all the resources out
	for _, r := range config.Resources {
		// Grab the address for this resource
		addr, err := parseResourceAddressConfig(r)
		if err != nil {
			return err
		}
		addr.Path = m.Path()

		// Build the abstract resource. We have the config already so
		// we'll just pre-populate that.
		abstract := &NodeAbstractResource{
			Addr:   addr,
			Config: r,
		}
		var node dag.Vertex = abstract
		if f := t.Concrete; f != nil {
			node = f(abstract)
		}

		g.Add(node)
	}

	return nil
}