github.com/magodo/terraform@v0.11.12-beta1/terraform/transform_config_flat.go (about)

     1  package terraform
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/hashicorp/terraform/config/module"
     7  	"github.com/hashicorp/terraform/dag"
     8  )
     9  
    10  // FlatConfigTransformer is a GraphTransformer that adds the configuration
    11  // to the graph. The module used to configure this transformer must be
    12  // the root module.
    13  //
    14  // This transform adds the nodes but doesn't connect any of the references.
    15  // The ReferenceTransformer should be used for that.
    16  //
    17  // NOTE: In relation to ConfigTransformer: this is a newer generation config
    18  // transformer. It puts the _entire_ config into the graph (there is no
    19  // "flattening" step as before).
    20  type FlatConfigTransformer struct {
    21  	Concrete ConcreteResourceNodeFunc // What to turn resources into
    22  
    23  	Module *module.Tree
    24  }
    25  
    26  func (t *FlatConfigTransformer) Transform(g *Graph) error {
    27  	// If no module, we do nothing
    28  	if t.Module == nil {
    29  		return nil
    30  	}
    31  
    32  	// If the module is not loaded, that is an error
    33  	if !t.Module.Loaded() {
    34  		return errors.New("module must be loaded")
    35  	}
    36  
    37  	return t.transform(g, t.Module)
    38  }
    39  
    40  func (t *FlatConfigTransformer) transform(g *Graph, m *module.Tree) error {
    41  	// If no module, no problem
    42  	if m == nil {
    43  		return nil
    44  	}
    45  
    46  	// Transform all the children.
    47  	for _, c := range m.Children() {
    48  		if err := t.transform(g, c); err != nil {
    49  			return err
    50  		}
    51  	}
    52  
    53  	// Get the configuration for this module
    54  	config := m.Config()
    55  
    56  	// Write all the resources out
    57  	for _, r := range config.Resources {
    58  		// Grab the address for this resource
    59  		addr, err := parseResourceAddressConfig(r)
    60  		if err != nil {
    61  			return err
    62  		}
    63  		addr.Path = m.Path()
    64  
    65  		// Build the abstract resource. We have the config already so
    66  		// we'll just pre-populate that.
    67  		abstract := &NodeAbstractResource{
    68  			Addr:   addr,
    69  			Config: r,
    70  		}
    71  		var node dag.Vertex = abstract
    72  		if f := t.Concrete; f != nil {
    73  			node = f(abstract)
    74  		}
    75  
    76  		g.Add(node)
    77  	}
    78  
    79  	return nil
    80  }