2016-04-26 12:38:30 -05:00
|
|
|
package terraform
|
|
|
|
|
2016-05-10 11:15:19 -05:00
|
|
|
import (
|
|
|
|
"github.com/hashicorp/terraform/config/module"
|
|
|
|
)
|
|
|
|
|
2016-04-26 12:38:30 -05: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 11:15:19 -05:00
|
|
|
// Module is the module to add to the graph. See ImportOpts.Module.
|
|
|
|
Module *module.Tree
|
|
|
|
|
2016-04-26 12:38:30 -05: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 15:36:10 -06:00
|
|
|
Name: "ImportGraphBuilder",
|
2016-04-26 12:38:30 -05: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 11:15:19 -05: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 12:56:23 -05:00
|
|
|
// Custom factory for creating providers.
|
|
|
|
providerFactory := func(name string, path []string) GraphNodeProvider {
|
|
|
|
return &NodeApplyableProvider{
|
|
|
|
NameValue: name,
|
|
|
|
PathValue: path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 12:38:30 -05:00
|
|
|
steps := []GraphTransformer{
|
2016-05-10 11:15:19 -05:00
|
|
|
// Create all our resources from the configuration and state
|
2016-11-05 18:26:12 -05:00
|
|
|
&ConfigTransformerOld{Module: mod},
|
2016-05-10 11:15:19 -05:00
|
|
|
|
2016-04-28 04:46:46 -05:00
|
|
|
// Add the import steps
|
|
|
|
&ImportStateTransformer{Targets: b.ImportTargets},
|
|
|
|
|
2016-04-26 12:38:30 -05:00
|
|
|
// Provider-related transformations
|
2016-11-02 12:56:23 -05:00
|
|
|
&MissingProviderTransformer{Providers: b.Providers, Factory: providerFactory},
|
2016-04-26 12:38:30 -05:00
|
|
|
&ProviderTransformer{},
|
2016-10-19 17:07:00 -05:00
|
|
|
&DisableProviderTransformerOld{},
|
2016-04-26 12:38:30 -05:00
|
|
|
&PruneProviderTransformer{},
|
2016-11-02 12:56:23 -05:00
|
|
|
&AttachProviderConfigTransformer{Module: mod},
|
2016-04-26 12:38:30 -05:00
|
|
|
|
2016-11-02 13:08:16 -05:00
|
|
|
// This validates that the providers only depend on variables
|
|
|
|
&ImportProviderValidateTransformer{},
|
|
|
|
|
2016-11-02 12:39:04 -05:00
|
|
|
// Single root
|
|
|
|
&RootTransformer{},
|
|
|
|
|
2016-04-26 12:38:30 -05:00
|
|
|
// Optimize
|
|
|
|
&TransitiveReductionTransformer{},
|
|
|
|
}
|
|
|
|
|
|
|
|
return steps
|
|
|
|
}
|