github.com/opentofu/opentofu@v1.7.1/internal/tofu/transform_variable.go (about)

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