github.com/opentofu/opentofu@v1.7.1/internal/tofu/transform_output.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  	"log"
    10  
    11  	"github.com/opentofu/opentofu/internal/addrs"
    12  	"github.com/opentofu/opentofu/internal/configs"
    13  )
    14  
    15  // OutputTransformer is a GraphTransformer that adds all the outputs
    16  // in the configuration to the graph.
    17  //
    18  // This is done for the apply graph builder even if dependent nodes
    19  // aren't changing since there is no downside: the state will be available
    20  // even if the dependent items aren't changing.
    21  type OutputTransformer struct {
    22  	Config *configs.Config
    23  
    24  	// Refresh-only mode means that any failing output preconditions are
    25  	// reported as warnings rather than errors
    26  	RefreshOnly bool
    27  
    28  	// Planning must be set to true only when we're building a planning graph.
    29  	// It must be set to false whenever we're building an apply graph.
    30  	Planning bool
    31  
    32  	// If this is a planned destroy, root outputs are still in the configuration
    33  	// so we need to record that we wish to remove them.
    34  	Destroying bool
    35  }
    36  
    37  func (t *OutputTransformer) Transform(g *Graph) error {
    38  	return t.transform(g, t.Config)
    39  }
    40  
    41  func (t *OutputTransformer) transform(g *Graph, c *configs.Config) error {
    42  	// If we have no config then there can be no outputs.
    43  	if c == nil {
    44  		return nil
    45  	}
    46  
    47  	// Transform all the children. We must do this first because
    48  	// we can reference module outputs and they must show up in the
    49  	// reference map.
    50  	for _, cc := range c.Children {
    51  		if err := t.transform(g, cc); err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	for _, o := range c.Module.Outputs {
    57  		addr := addrs.OutputValue{Name: o.Name}
    58  
    59  		node := &nodeExpandOutput{
    60  			Addr:        addr,
    61  			Module:      c.Path,
    62  			Config:      o,
    63  			Destroying:  t.Destroying,
    64  			RefreshOnly: t.RefreshOnly,
    65  			Planning:    t.Planning,
    66  		}
    67  
    68  		log.Printf("[TRACE] OutputTransformer: adding %s as %T", o.Name, node)
    69  		g.Add(node)
    70  	}
    71  
    72  	return nil
    73  }