kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/terraform/graph_builder_import.go (about)

     1  package terraform
     2  
     3  import (
     4  	"kubeform.dev/terraform-backend-sdk/addrs"
     5  	"kubeform.dev/terraform-backend-sdk/configs"
     6  	"kubeform.dev/terraform-backend-sdk/dag"
     7  	"kubeform.dev/terraform-backend-sdk/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  	// Plugins is a library of plug-in components (providers and
    21  	// provisioners) available for use.
    22  	Plugins *contextPlugins
    23  }
    24  
    25  // Build builds the graph according to the steps returned by Steps.
    26  func (b *ImportGraphBuilder) Build(path addrs.ModuleInstance) (*Graph, tfdiags.Diagnostics) {
    27  	return (&BasicGraphBuilder{
    28  		Steps:    b.Steps(),
    29  		Validate: true,
    30  		Name:     "ImportGraphBuilder",
    31  	}).Build(path)
    32  }
    33  
    34  // Steps returns the ordered list of GraphTransformers that must be executed
    35  // to build a complete graph.
    36  func (b *ImportGraphBuilder) Steps() []GraphTransformer {
    37  	// Get the module. If we don't have one, we just use an empty tree
    38  	// so that the transform still works but does nothing.
    39  	config := b.Config
    40  	if config == nil {
    41  		config = configs.NewEmptyConfig()
    42  	}
    43  
    44  	// Custom factory for creating providers.
    45  	concreteProvider := func(a *NodeAbstractProvider) dag.Vertex {
    46  		return &NodeApplyableProvider{
    47  			NodeAbstractProvider: a,
    48  		}
    49  	}
    50  
    51  	steps := []GraphTransformer{
    52  		// Create all our resources from the configuration and state
    53  		&ConfigTransformer{Config: config},
    54  
    55  		// Add dynamic values
    56  		&RootVariableTransformer{Config: b.Config},
    57  		&ModuleVariableTransformer{Config: b.Config},
    58  		&LocalTransformer{Config: b.Config},
    59  		&OutputTransformer{Config: b.Config},
    60  
    61  		// Attach the configuration to any resources
    62  		&AttachResourceConfigTransformer{Config: b.Config},
    63  
    64  		// Add the import steps
    65  		&ImportStateTransformer{Targets: b.ImportTargets, Config: b.Config},
    66  
    67  		transformProviders(concreteProvider, config),
    68  
    69  		// Must attach schemas before ReferenceTransformer so that we can
    70  		// analyze the configuration to find references.
    71  		&AttachSchemaTransformer{Plugins: b.Plugins, Config: b.Config},
    72  
    73  		// Create expansion nodes for all of the module calls. This must
    74  		// come after all other transformers that create nodes representing
    75  		// objects that can belong to modules.
    76  		&ModuleExpansionTransformer{Config: b.Config},
    77  
    78  		// Connect so that the references are ready for targeting. We'll
    79  		// have to connect again later for providers and so on.
    80  		&ReferenceTransformer{},
    81  
    82  		// Make sure data sources are aware of any depends_on from the
    83  		// configuration
    84  		&attachDataResourceDependsOnTransformer{},
    85  
    86  		// Close opened plugin connections
    87  		&CloseProviderTransformer{},
    88  
    89  		// Close root module
    90  		&CloseRootModuleTransformer{},
    91  
    92  		// Optimize
    93  		&TransitiveReductionTransformer{},
    94  	}
    95  
    96  	return steps
    97  }