github.com/pulumi/terraform@v1.4.0/pkg/terraform/transform_attach_config_resource.go (about)

     1  package terraform
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/pulumi/terraform/pkg/configs"
     7  	"github.com/pulumi/terraform/pkg/dag"
     8  )
     9  
    10  // GraphNodeAttachResourceConfig is an interface that must be implemented by nodes
    11  // that want resource configurations attached.
    12  type GraphNodeAttachResourceConfig interface {
    13  	GraphNodeConfigResource
    14  
    15  	// Sets the configuration
    16  	AttachResourceConfig(*configs.Resource)
    17  }
    18  
    19  // AttachResourceConfigTransformer goes through the graph and attaches
    20  // resource configuration structures to nodes that implement
    21  // GraphNodeAttachManagedResourceConfig or GraphNodeAttachDataResourceConfig.
    22  //
    23  // The attached configuration structures are directly from the configuration.
    24  // If they're going to be modified, a copy should be made.
    25  type AttachResourceConfigTransformer struct {
    26  	Config *configs.Config // Config is the root node in the config tree
    27  }
    28  
    29  func (t *AttachResourceConfigTransformer) Transform(g *Graph) error {
    30  
    31  	// Go through and find GraphNodeAttachResource
    32  	for _, v := range g.Vertices() {
    33  		// Only care about GraphNodeAttachResource implementations
    34  		arn, ok := v.(GraphNodeAttachResourceConfig)
    35  		if !ok {
    36  			continue
    37  		}
    38  
    39  		// Determine what we're looking for
    40  		addr := arn.ResourceAddr()
    41  
    42  		// Get the configuration.
    43  		config := t.Config.Descendent(addr.Module)
    44  		if config == nil {
    45  			log.Printf("[TRACE] AttachResourceConfigTransformer: %q (%T) has no configuration available", dag.VertexName(v), v)
    46  			continue
    47  		}
    48  
    49  		for _, r := range config.Module.ManagedResources {
    50  			rAddr := r.Addr()
    51  
    52  			if rAddr != addr.Resource {
    53  				// Not the same resource
    54  				continue
    55  			}
    56  
    57  			log.Printf("[TRACE] AttachResourceConfigTransformer: attaching to %q (%T) config from %s", dag.VertexName(v), v, r.DeclRange)
    58  			arn.AttachResourceConfig(r)
    59  
    60  			// attach the provider_meta info
    61  			if gnapmc, ok := v.(GraphNodeAttachProviderMetaConfigs); ok {
    62  				log.Printf("[TRACE] AttachResourceConfigTransformer: attaching provider meta configs to %s", dag.VertexName(v))
    63  				if config == nil {
    64  					log.Printf("[TRACE] AttachResourceConfigTransformer: no config set on the transformer for %s", dag.VertexName(v))
    65  					continue
    66  				}
    67  				if config.Module == nil {
    68  					log.Printf("[TRACE] AttachResourceConfigTransformer: no module in config for %s", dag.VertexName(v))
    69  					continue
    70  				}
    71  				if config.Module.ProviderMetas == nil {
    72  					log.Printf("[TRACE] AttachResourceConfigTransformer: no provider metas defined for %s", dag.VertexName(v))
    73  					continue
    74  				}
    75  				gnapmc.AttachProviderMetaConfigs(config.Module.ProviderMetas)
    76  			}
    77  		}
    78  		for _, r := range config.Module.DataResources {
    79  			rAddr := r.Addr()
    80  
    81  			if rAddr != addr.Resource {
    82  				// Not the same resource
    83  				continue
    84  			}
    85  
    86  			log.Printf("[TRACE] AttachResourceConfigTransformer: attaching to %q (%T) config from %#v", dag.VertexName(v), v, r.DeclRange)
    87  			arn.AttachResourceConfig(r)
    88  
    89  			// attach the provider_meta info
    90  			if gnapmc, ok := v.(GraphNodeAttachProviderMetaConfigs); ok {
    91  				log.Printf("[TRACE] AttachResourceConfigTransformer: attaching provider meta configs to %s", dag.VertexName(v))
    92  				if config == nil {
    93  					log.Printf("[TRACE] AttachResourceConfigTransformer: no config set on the transformer for %s", dag.VertexName(v))
    94  					continue
    95  				}
    96  				if config.Module == nil {
    97  					log.Printf("[TRACE] AttachResourceConfigTransformer: no module in config for %s", dag.VertexName(v))
    98  					continue
    99  				}
   100  				if config.Module.ProviderMetas == nil {
   101  					log.Printf("[TRACE] AttachResourceConfigTransformer: no provider metas defined for %s", dag.VertexName(v))
   102  					continue
   103  				}
   104  				gnapmc.AttachProviderMetaConfigs(config.Module.ProviderMetas)
   105  			}
   106  		}
   107  	}
   108  
   109  	return nil
   110  }