github.com/rhenning/terraform@v0.8.0-beta2/terraform/graph_builder_import.go (about)

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