2016-04-26 19:38:30 +02:00
|
|
|
package terraform
|
|
|
|
|
2016-05-10 18:15:19 +02:00
|
|
|
import (
|
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2016-12-04 00:27:38 +01:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
2016-05-10 18:15:19 +02:00
|
|
|
)
|
|
|
|
|
2016-04-26 19:38:30 +02:00
|
|
|
// ImportGraphBuilder implements GraphBuilder and is responsible for building
|
|
|
|
// a graph for importing resources into Terraform. This is a much, much
|
|
|
|
// simpler graph than a normal configuration graph.
|
|
|
|
type ImportGraphBuilder struct {
|
|
|
|
// ImportTargets are the list of resources to import.
|
|
|
|
ImportTargets []*ImportTarget
|
|
|
|
|
2016-05-10 18:15:19 +02:00
|
|
|
// Module is the module to add to the graph. See ImportOpts.Module.
|
|
|
|
Module *module.Tree
|
|
|
|
|
2016-04-26 19:38:30 +02:00
|
|
|
// Providers is the list of providers supported.
|
|
|
|
Providers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build builds the graph according to the steps returned by Steps.
|
|
|
|
func (b *ImportGraphBuilder) Build(path []string) (*Graph, error) {
|
|
|
|
return (&BasicGraphBuilder{
|
|
|
|
Steps: b.Steps(),
|
|
|
|
Validate: true,
|
2016-11-15 22:36:10 +01:00
|
|
|
Name: "ImportGraphBuilder",
|
2016-04-26 19:38:30 +02:00
|
|
|
}).Build(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Steps returns the ordered list of GraphTransformers that must be executed
|
|
|
|
// to build a complete graph.
|
|
|
|
func (b *ImportGraphBuilder) Steps() []GraphTransformer {
|
2016-05-10 18:15:19 +02:00
|
|
|
// Get the module. If we don't have one, we just use an empty tree
|
|
|
|
// so that the transform still works but does nothing.
|
|
|
|
mod := b.Module
|
|
|
|
if mod == nil {
|
|
|
|
mod = module.NewEmptyTree()
|
|
|
|
}
|
|
|
|
|
2016-11-02 18:56:23 +01:00
|
|
|
// Custom factory for creating providers.
|
2016-12-04 00:27:38 +01:00
|
|
|
concreteProvider := func(a *NodeAbstractProvider) dag.Vertex {
|
2016-11-02 18:56:23 +01:00
|
|
|
return &NodeApplyableProvider{
|
2016-12-04 00:27:38 +01:00
|
|
|
NodeAbstractProvider: a,
|
2016-11-02 18:56:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 19:38:30 +02:00
|
|
|
steps := []GraphTransformer{
|
2016-05-10 18:15:19 +02:00
|
|
|
// Create all our resources from the configuration and state
|
2016-11-06 00:26:12 +01:00
|
|
|
&ConfigTransformerOld{Module: mod},
|
2016-05-10 18:15:19 +02:00
|
|
|
|
2016-04-28 11:46:46 +02:00
|
|
|
// Add the import steps
|
|
|
|
&ImportStateTransformer{Targets: b.ImportTargets},
|
|
|
|
|
2016-04-26 19:38:30 +02:00
|
|
|
// Provider-related transformations
|
2016-12-04 00:27:38 +01:00
|
|
|
&MissingProviderTransformer{Providers: b.Providers, Concrete: concreteProvider},
|
2016-04-26 19:38:30 +02:00
|
|
|
&ProviderTransformer{},
|
2016-10-20 00:07:00 +02:00
|
|
|
&DisableProviderTransformerOld{},
|
2016-04-26 19:38:30 +02:00
|
|
|
&PruneProviderTransformer{},
|
2016-11-02 18:56:23 +01:00
|
|
|
&AttachProviderConfigTransformer{Module: mod},
|
2016-04-26 19:38:30 +02:00
|
|
|
|
2016-11-02 19:08:16 +01:00
|
|
|
// This validates that the providers only depend on variables
|
|
|
|
&ImportProviderValidateTransformer{},
|
|
|
|
|
2016-11-02 18:39:04 +01:00
|
|
|
// Single root
|
|
|
|
&RootTransformer{},
|
|
|
|
|
2016-04-26 19:38:30 +02:00
|
|
|
// Optimize
|
|
|
|
&TransitiveReductionTransformer{},
|
|
|
|
}
|
|
|
|
|
|
|
|
return steps
|
|
|
|
}
|