github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/terraform/transform_attach_config_resource.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/config"
     8  	"github.com/hashicorp/terraform/config/module"
     9  )
    10  
    11  // GraphNodeAttachResourceConfig is an interface that must be implemented by nodes
    12  // that want resource configurations attached.
    13  type GraphNodeAttachResourceConfig interface {
    14  	// ResourceAddr is the address to the resource
    15  	ResourceAddr() *ResourceAddress
    16  
    17  	// Sets the configuration
    18  	AttachResourceConfig(*config.Resource)
    19  }
    20  
    21  // AttachResourceConfigTransformer goes through the graph and attaches
    22  // resource configuration structures to nodes that implement the interfaces
    23  // above.
    24  //
    25  // The attached configuration structures are directly from the configuration.
    26  // If they're going to be modified, a copy should be made.
    27  type AttachResourceConfigTransformer struct {
    28  	Module *module.Tree // Module is the root module for the config
    29  }
    30  
    31  func (t *AttachResourceConfigTransformer) Transform(g *Graph) error {
    32  	log.Printf("[TRACE] AttachResourceConfigTransformer: Beginning...")
    33  
    34  	// Go through and find GraphNodeAttachResource
    35  	for _, v := range g.Vertices() {
    36  		// Only care about GraphNodeAttachResource implementations
    37  		arn, ok := v.(GraphNodeAttachResourceConfig)
    38  		if !ok {
    39  			continue
    40  		}
    41  
    42  		// Determine what we're looking for
    43  		addr := arn.ResourceAddr()
    44  		log.Printf(
    45  			"[TRACE] AttachResourceConfigTransformer: Attach resource "+
    46  				"config request: %s", addr)
    47  
    48  		// Get the configuration.
    49  		path := normalizeModulePath(addr.Path)
    50  		path = path[1:]
    51  		tree := t.Module.Child(path)
    52  		if tree == nil {
    53  			continue
    54  		}
    55  
    56  		// Go through the resource configs to find the matching config
    57  		for _, r := range tree.Config().Resources {
    58  			// Get a resource address so we can compare
    59  			a, err := parseResourceAddressConfig(r)
    60  			if err != nil {
    61  				panic(fmt.Sprintf(
    62  					"Error parsing config address, this is a bug: %#v", r))
    63  			}
    64  			a.Path = addr.Path
    65  
    66  			// If this is not the same resource, then continue
    67  			if !a.Equals(addr) {
    68  				continue
    69  			}
    70  
    71  			log.Printf("[TRACE] Attaching resource config: %#v", r)
    72  			arn.AttachResourceConfig(r)
    73  			break
    74  		}
    75  	}
    76  
    77  	return nil
    78  }