github.com/r3labs/terraform@v0.8.4/terraform/transform_config.go (about)

     1  package terraform
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  
     8  	"github.com/hashicorp/terraform/config/module"
     9  	"github.com/hashicorp/terraform/dag"
    10  )
    11  
    12  // ConfigTransformer is a GraphTransformer that adds all the resources
    13  // from the configuration to the graph.
    14  //
    15  // The module used to configure this transformer must be the root module.
    16  //
    17  // Only resources are added to the graph. Variables, outputs, and
    18  // providers must be added via other transforms.
    19  //
    20  // Unlike ConfigTransformerOld, this transformer creates a graph with
    21  // all resources including module resources, rather than creating module
    22  // nodes that are then "flattened".
    23  type ConfigTransformer struct {
    24  	Concrete ConcreteResourceNodeFunc
    25  
    26  	Module *module.Tree
    27  }
    28  
    29  func (t *ConfigTransformer) Transform(g *Graph) error {
    30  	// If no module is given, we don't do anything
    31  	if t.Module == nil {
    32  		return nil
    33  	}
    34  
    35  	// If the module isn't loaded, that is simply an error
    36  	if !t.Module.Loaded() {
    37  		return errors.New("module must be loaded for ConfigTransformer")
    38  	}
    39  
    40  	// Start the transformation process
    41  	return t.transform(g, t.Module)
    42  }
    43  
    44  func (t *ConfigTransformer) transform(g *Graph, m *module.Tree) error {
    45  	// If no config, do nothing
    46  	if m == nil {
    47  		return nil
    48  	}
    49  
    50  	// Add our resources
    51  	if err := t.transformSingle(g, m); err != nil {
    52  		return err
    53  	}
    54  
    55  	// Transform all the children.
    56  	for _, c := range m.Children() {
    57  		if err := t.transform(g, c); err != nil {
    58  			return err
    59  		}
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  func (t *ConfigTransformer) transformSingle(g *Graph, m *module.Tree) error {
    66  	log.Printf("[TRACE] ConfigTransformer: Starting for path: %v", m.Path())
    67  
    68  	// Get the configuration for this module
    69  	config := m.Config()
    70  
    71  	// Build the path we're at
    72  	path := m.Path()
    73  
    74  	// Write all the resources out
    75  	for _, r := range config.Resources {
    76  		// Build the resource address
    77  		addr, err := parseResourceAddressConfig(r)
    78  		if err != nil {
    79  			panic(fmt.Sprintf(
    80  				"Error parsing config address, this is a bug: %#v", r))
    81  		}
    82  		addr.Path = path
    83  
    84  		// Build the abstract node and the concrete one
    85  		abstract := &NodeAbstractResource{Addr: addr}
    86  		var node dag.Vertex = abstract
    87  		if f := t.Concrete; f != nil {
    88  			node = f(abstract)
    89  		}
    90  
    91  		// Add it to the graph
    92  		g.Add(node)
    93  	}
    94  
    95  	return nil
    96  }