github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/terraform/graph_builder_import.go (about)

     1  package terraform
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/addrs"
     5  	"github.com/hashicorp/terraform/configs"
     6  	"github.com/hashicorp/terraform/dag"
     7  	"github.com/hashicorp/terraform/tfdiags"
     8  )
     9  
    10  // ImportGraphBuilder implements GraphBuilder and is responsible for building
    11  // a graph for importing resources into Terraform. This is a much, much
    12  // simpler graph than a normal configuration graph.
    13  type ImportGraphBuilder struct {
    14  	// ImportTargets are the list of resources to import.
    15  	ImportTargets []*ImportTarget
    16  
    17  	// Module is a configuration to build the graph from. See ImportOpts.Config.
    18  	Config *configs.Config
    19  
    20  	// Components is the factory for our available plugin components.
    21  	Components contextComponentFactory
    22  
    23  	// Schemas is the repository of schemas we will draw from to analyse
    24  	// the configuration.
    25  	Schemas *Schemas
    26  }
    27  
    28  // Build builds the graph according to the steps returned by Steps.
    29  func (b *ImportGraphBuilder) Build(path addrs.ModuleInstance) (*Graph, tfdiags.Diagnostics) {
    30  	return (&BasicGraphBuilder{
    31  		Steps:    b.Steps(),
    32  		Validate: true,
    33  		Name:     "ImportGraphBuilder",
    34  	}).Build(path)
    35  }
    36  
    37  // Steps returns the ordered list of GraphTransformers that must be executed
    38  // to build a complete graph.
    39  func (b *ImportGraphBuilder) Steps() []GraphTransformer {
    40  	// Get the module. If we don't have one, we just use an empty tree
    41  	// so that the transform still works but does nothing.
    42  	config := b.Config
    43  	if config == nil {
    44  		config = configs.NewEmptyConfig()
    45  	}
    46  
    47  	// Custom factory for creating providers.
    48  	concreteProvider := func(a *NodeAbstractProvider) dag.Vertex {
    49  		return &NodeApplyableProvider{
    50  			NodeAbstractProvider: a,
    51  		}
    52  	}
    53  
    54  	steps := []GraphTransformer{
    55  		// Create all our resources from the configuration and state
    56  		&ConfigTransformer{Config: config},
    57  
    58  		// Attach the configuration to any resources
    59  		&AttachResourceConfigTransformer{Config: b.Config},
    60  
    61  		// Add the import steps
    62  		&ImportStateTransformer{Targets: b.ImportTargets},
    63  
    64  		// Add root variables
    65  		&RootVariableTransformer{Config: b.Config},
    66  
    67  		TransformProviders(b.Components.ResourceProviders(), concreteProvider, config),
    68  
    69  		// This validates that the providers only depend on variables
    70  		&ImportProviderValidateTransformer{},
    71  
    72  		// Add the local values
    73  		&LocalTransformer{Config: b.Config},
    74  
    75  		// Add the outputs
    76  		&OutputTransformer{Config: b.Config},
    77  
    78  		// Add module variables
    79  		&ModuleVariableTransformer{Config: b.Config},
    80  
    81  		// Must attach schemas before ReferenceTransformer so that we can
    82  		// analyze the configuration to find references.
    83  		&AttachSchemaTransformer{Schemas: b.Schemas},
    84  
    85  		// Connect so that the references are ready for targeting. We'll
    86  		// have to connect again later for providers and so on.
    87  		&ReferenceTransformer{},
    88  
    89  		// Close opened plugin connections
    90  		&CloseProviderTransformer{},
    91  
    92  		// Single root
    93  		&RootTransformer{},
    94  
    95  		// Optimize
    96  		&TransitiveReductionTransformer{},
    97  	}
    98  
    99  	return steps
   100  }