kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/terraform/transform_config.go (about)

     1  package terraform
     2  
     3  import (
     4  	"log"
     5  
     6  	"kubeform.dev/terraform-backend-sdk/addrs"
     7  	"kubeform.dev/terraform-backend-sdk/configs"
     8  	"kubeform.dev/terraform-backend-sdk/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  
    33  func (t *ConfigTransformer) Transform(g *Graph) error {
    34  	// If no configuration is available, we don't do anything
    35  	if t.Config == nil {
    36  		return nil
    37  	}
    38  
    39  	// Start the transformation process
    40  	return t.transform(g, t.Config)
    41  }
    42  
    43  func (t *ConfigTransformer) transform(g *Graph, config *configs.Config) error {
    44  	// If no config, do nothing
    45  	if config == nil {
    46  		return nil
    47  	}
    48  
    49  	// Add our resources
    50  	if err := t.transformSingle(g, config); err != nil {
    51  		return err
    52  	}
    53  
    54  	// Transform all the children.
    55  	for _, c := range config.Children {
    56  		if err := t.transform(g, c); err != nil {
    57  			return err
    58  		}
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func (t *ConfigTransformer) transformSingle(g *Graph, config *configs.Config) error {
    65  	path := config.Path
    66  	module := config.Module
    67  	log.Printf("[TRACE] ConfigTransformer: Starting for path: %v", path)
    68  
    69  	allResources := make([]*configs.Resource, 0, len(module.ManagedResources)+len(module.DataResources))
    70  	for _, r := range module.ManagedResources {
    71  		allResources = append(allResources, r)
    72  	}
    73  	for _, r := range module.DataResources {
    74  		allResources = append(allResources, r)
    75  	}
    76  
    77  	for _, r := range allResources {
    78  		relAddr := r.Addr()
    79  
    80  		if t.ModeFilter && relAddr.Mode != t.Mode {
    81  			// Skip non-matching modes
    82  			continue
    83  		}
    84  
    85  		abstract := &NodeAbstractResource{
    86  			Addr: addrs.ConfigResource{
    87  				Resource: relAddr,
    88  				Module:   path,
    89  			},
    90  		}
    91  
    92  		var node dag.Vertex = abstract
    93  		if f := t.Concrete; f != nil {
    94  			node = f(abstract)
    95  		}
    96  
    97  		g.Add(node)
    98  	}
    99  
   100  	return nil
   101  }