github.com/trawler/terraform@v0.10.8-0.20171106022149-4b1c7a1d9b48/terraform/graph_builder_import.go (about)

     1  package terraform
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/config/module"
     5  	"github.com/hashicorp/terraform/dag"
     6  )
     7  
     8  // ImportGraphBuilder implements GraphBuilder and is responsible for building
     9  // a graph for importing resources into Terraform. This is a much, much
    10  // simpler graph than a normal configuration graph.
    11  type ImportGraphBuilder struct {
    12  	// ImportTargets are the list of resources to import.
    13  	ImportTargets []*ImportTarget
    14  
    15  	// Module is the module to add to the graph. See ImportOpts.Module.
    16  	Module *module.Tree
    17  
    18  	// Providers is the list of providers supported.
    19  	Providers []string
    20  }
    21  
    22  // Build builds the graph according to the steps returned by Steps.
    23  func (b *ImportGraphBuilder) Build(path []string) (*Graph, error) {
    24  	return (&BasicGraphBuilder{
    25  		Steps:    b.Steps(),
    26  		Validate: true,
    27  		Name:     "ImportGraphBuilder",
    28  	}).Build(path)
    29  }
    30  
    31  // Steps returns the ordered list of GraphTransformers that must be executed
    32  // to build a complete graph.
    33  func (b *ImportGraphBuilder) Steps() []GraphTransformer {
    34  	// Get the module. If we don't have one, we just use an empty tree
    35  	// so that the transform still works but does nothing.
    36  	mod := b.Module
    37  	if mod == nil {
    38  		mod = module.NewEmptyTree()
    39  	}
    40  
    41  	// Custom factory for creating providers.
    42  	concreteProvider := func(a *NodeAbstractProvider) dag.Vertex {
    43  		return &NodeApplyableProvider{
    44  			NodeAbstractProvider: a,
    45  		}
    46  	}
    47  
    48  	steps := []GraphTransformer{
    49  		// Create all our resources from the configuration and state
    50  		&ConfigTransformer{Module: mod},
    51  
    52  		// Add the import steps
    53  		&ImportStateTransformer{Targets: b.ImportTargets},
    54  
    55  		TransformProviders(b.Providers, concreteProvider, mod),
    56  
    57  		// This validates that the providers only depend on variables
    58  		&ImportProviderValidateTransformer{},
    59  
    60  		// Close opened plugin connections
    61  		&CloseProviderTransformer{},
    62  
    63  		// Single root
    64  		&RootTransformer{},
    65  
    66  		// Optimize
    67  		&TransitiveReductionTransformer{},
    68  	}
    69  
    70  	return steps
    71  }