]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package terraform |
2 | ||
3 | import ( | |
107c1cdb ND |
4 | "github.com/hashicorp/terraform/addrs" |
5 | "github.com/hashicorp/terraform/configs" | |
bae9f6d2 | 6 | "github.com/hashicorp/terraform/dag" |
107c1cdb | 7 | "github.com/hashicorp/terraform/tfdiags" |
bae9f6d2 JC |
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 | ||
107c1cdb ND |
17 | // Module is a configuration to build the graph from. See ImportOpts.Config. |
18 | Config *configs.Config | |
bae9f6d2 | 19 | |
107c1cdb ND |
20 | // Components is the factory for our available plugin components. |
21 | Components contextComponentFactory | |
22 | ||
23 | // Schemas is the repository of schemas we will draw from to analyse | |
24 | // the configuration. | |
25 | Schemas *Schemas | |
bae9f6d2 JC |
26 | } |
27 | ||
28 | // Build builds the graph according to the steps returned by Steps. | |
107c1cdb | 29 | func (b *ImportGraphBuilder) Build(path addrs.ModuleInstance) (*Graph, tfdiags.Diagnostics) { |
bae9f6d2 JC |
30 | return (&BasicGraphBuilder{ |
31 | Steps: b.Steps(), | |
32 | Validate: true, | |
33 | Name: "ImportGraphBuilder", | |
34 | }).Build(path) | |
35 | } | |
36 | ||
37 | // Steps returns the ordered list of GraphTransformers that must be executed | |
38 | // to build a complete graph. | |
39 | func (b *ImportGraphBuilder) Steps() []GraphTransformer { | |
40 | // Get the module. If we don't have one, we just use an empty tree | |
41 | // so that the transform still works but does nothing. | |
107c1cdb ND |
42 | config := b.Config |
43 | if config == nil { | |
44 | config = configs.NewEmptyConfig() | |
bae9f6d2 JC |
45 | } |
46 | ||
47 | // Custom factory for creating providers. | |
48 | concreteProvider := func(a *NodeAbstractProvider) dag.Vertex { | |
49 | return &NodeApplyableProvider{ | |
50 | NodeAbstractProvider: a, | |
51 | } | |
52 | } | |
53 | ||
54 | steps := []GraphTransformer{ | |
55 | // Create all our resources from the configuration and state | |
107c1cdb | 56 | &ConfigTransformer{Config: config}, |
bae9f6d2 JC |
57 | |
58 | // Add the import steps | |
59 | &ImportStateTransformer{Targets: b.ImportTargets}, | |
60 | ||
107c1cdb ND |
61 | // Add root variables |
62 | &RootVariableTransformer{Config: b.Config}, | |
63 | ||
64 | TransformProviders(b.Components.ResourceProviders(), concreteProvider, config), | |
bae9f6d2 JC |
65 | |
66 | // This validates that the providers only depend on variables | |
67 | &ImportProviderValidateTransformer{}, | |
68 | ||
107c1cdb ND |
69 | // Add the local values |
70 | &LocalTransformer{Config: b.Config}, | |
71 | ||
72 | // Add the outputs | |
73 | &OutputTransformer{Config: b.Config}, | |
74 | ||
75 | // Add module variables | |
76 | &ModuleVariableTransformer{Config: b.Config}, | |
77 | ||
78 | // Must attach schemas before ReferenceTransformer so that we can | |
79 | // analyze the configuration to find references. | |
80 | &AttachSchemaTransformer{Schemas: b.Schemas}, | |
81 | ||
82 | // Connect so that the references are ready for targeting. We'll | |
83 | // have to connect again later for providers and so on. | |
84 | &ReferenceTransformer{}, | |
85 | ||
bae9f6d2 JC |
86 | // Close opened plugin connections |
87 | &CloseProviderTransformer{}, | |
88 | ||
89 | // Single root | |
90 | &RootTransformer{}, | |
91 | ||
92 | // Optimize | |
93 | &TransitiveReductionTransformer{}, | |
94 | } | |
95 | ||
96 | return steps | |
97 | } |