1package terraform
2
3import (
4	"github.com/hashicorp/terraform-plugin-sdk/internal/addrs"
5	"github.com/hashicorp/terraform-plugin-sdk/internal/configs"
6)
7
8// RootVariableTransformer is a GraphTransformer that adds all the root
9// variables to the graph.
10//
11// Root variables are currently no-ops but they must be added to the
12// graph since downstream things that depend on them must be able to
13// reach them.
14type RootVariableTransformer struct {
15	Config *configs.Config
16}
17
18func (t *RootVariableTransformer) Transform(g *Graph) error {
19	// We can have no variables if we have no config.
20	if t.Config == nil {
21		return nil
22	}
23
24	// We're only considering root module variables here, since child
25	// module variables are handled by ModuleVariableTransformer.
26	vars := t.Config.Module.Variables
27
28	// Add all variables here
29	for _, v := range vars {
30		node := &NodeRootVariable{
31			Addr: addrs.InputVariable{
32				Name: v.Name,
33			},
34			Config: v,
35		}
36		g.Add(node)
37	}
38
39	return nil
40}
41