github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/terraform/transform_config.go (about)

     1  package terraform
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/hashicorp/terraform/internal/addrs"
     7  	"github.com/hashicorp/terraform/internal/configs"
     8  	"github.com/hashicorp/terraform/internal/dag"
     9  )
    10  
    11  // ConfigTransformer is a GraphTransformer that adds all the resources
    12  // from the configuration to the graph.
    13  //
    14  // The module used to configure this transformer must be the root module.
    15  //
    16  // Only resources are added to the graph. Variables, outputs, and
    17  // providers must be added via other transforms.
    18  //
    19  // Unlike ConfigTransformerOld, this transformer creates a graph with
    20  // all resources including module resources, rather than creating module
    21  // nodes that are then "flattened".
    22  type ConfigTransformer struct {
    23  	Concrete ConcreteResourceNodeFunc
    24  
    25  	// Module is the module to add resources from.
    26  	Config *configs.Config
    27  
    28  	// Mode will only add resources that match the given mode
    29  	ModeFilter bool
    30  	Mode       addrs.ResourceMode
    31  
    32  	// Do not apply this transformer.
    33  	skip bool
    34  
    35  	// configuration resources that are to be imported
    36  	importTargets []*ImportTarget
    37  }
    38  
    39  func (t *ConfigTransformer) Transform(g *Graph) error {
    40  	if t.skip {
    41  		return nil
    42  	}
    43  
    44  	// If no configuration is available, we don't do anything
    45  	if t.Config == nil {
    46  		return nil
    47  	}
    48  
    49  	// Start the transformation process
    50  	return t.transform(g, t.Config)
    51  }
    52  
    53  func (t *ConfigTransformer) transform(g *Graph, config *configs.Config) error {
    54  	// If no config, do nothing
    55  	if config == nil {
    56  		return nil
    57  	}
    58  
    59  	// Add our resources
    60  	if err := t.transformSingle(g, config); err != nil {
    61  		return err
    62  	}
    63  
    64  	// Transform all the children.
    65  	for _, c := range config.Children {
    66  		if err := t.transform(g, c); err != nil {
    67  			return err
    68  		}
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config) error {
    75  	path := config.Path
    76  	module := config.Module
    77  	log.Printf("[TRACE] ConfigTransformer: Starting for path: %v", path)
    78  
    79  	allResources := make([]*configs.Resource, 0, len(module.ManagedResources)+len(module.DataResources))
    80  	for _, r := range module.ManagedResources {
    81  		allResources = append(allResources, r)
    82  	}
    83  	for _, r := range module.DataResources {
    84  		allResources = append(allResources, r)
    85  	}
    86  
    87  	for _, r := range allResources {
    88  		relAddr := r.Addr()
    89  
    90  		if t.ModeFilter && relAddr.Mode != t.Mode {
    91  			// Skip non-matching modes
    92  			continue
    93  		}
    94  
    95  		// If any of the import targets can apply to this node's instances,
    96  		// filter them down to the applicable addresses.
    97  		var imports []*ImportTarget
    98  		configAddr := relAddr.InModule(path)
    99  		for _, i := range t.importTargets {
   100  			if target := i.Addr.ContainingResource().Config(); target.Equal(configAddr) {
   101  				imports = append(imports, i)
   102  			}
   103  		}
   104  
   105  		abstract := &NodeAbstractResource{
   106  			Addr: addrs.ConfigResource{
   107  				Resource: relAddr,
   108  				Module:   path,
   109  			},
   110  			importTargets: imports,
   111  		}
   112  
   113  		var node dag.Vertex = abstract
   114  		if f := t.Concrete; f != nil {
   115  			node = f(abstract)
   116  		}
   117  
   118  		g.Add(node)
   119  	}
   120  
   121  	return nil
   122  }