7 "github.com/hashicorp/hcl2/hcl"
8 "github.com/hashicorp/terraform/addrs"
9 "github.com/hashicorp/terraform/configs"
10 "github.com/hashicorp/terraform/dag"
11 "github.com/hashicorp/terraform/tfdiags"
14 func TransformProviders(providers []string, concrete ConcreteProviderNodeFunc, config *configs.Config) GraphTransformer {
15 return GraphTransformMulti(
16 // Add providers from the config
17 &ProviderConfigTransformer{
22 // Add any remaining missing providers
23 &MissingProviderTransformer{
27 // Connect the providers
31 // Remove unused providers and proxies
32 &PruneProviderTransformer{},
33 // Connect provider to their parent provider nodes
34 &ParentProviderTransformer{},
38 // GraphNodeProvider is an interface that nodes that can be a provider
41 // ProviderAddr returns the address of the provider configuration this
42 // satisfies, which is relative to the path returned by method Path().
44 // Name returns the full name of the provider in the config.
45 type GraphNodeProvider interface {
47 ProviderAddr() addrs.AbsProviderConfig
51 // GraphNodeCloseProvider is an interface that nodes that can be a close
52 // provider must implement. The CloseProviderName returned is the name of
53 // the provider they satisfy.
54 type GraphNodeCloseProvider interface {
56 CloseProviderAddr() addrs.AbsProviderConfig
59 // GraphNodeProviderConsumer is an interface that nodes that require
60 // a provider must implement. ProvidedBy must return the address of the provider
61 // to use, which will be resolved to a configuration either in the same module
62 // or in an ancestor module, with the resulting absolute address passed to
64 type GraphNodeProviderConsumer interface {
65 // ProvidedBy returns the address of the provider configuration the node
66 // refers to. If the returned "exact" value is true, this address will
67 // be taken exactly. If "exact" is false, a provider configuration from
68 // an ancestor module may be selected instead.
69 ProvidedBy() (addr addrs.AbsProviderConfig, exact bool)
70 // Set the resolved provider address for this resource.
71 SetProvider(addrs.AbsProviderConfig)
74 // ProviderTransformer is a GraphTransformer that maps resources to
75 // providers within the graph. This will error if there are any resources
76 // that don't map to proper resources.
77 type ProviderTransformer struct {
78 Config *configs.Config
81 func (t *ProviderTransformer) Transform(g *Graph) error {
82 // We need to find a provider configuration address for each resource
83 // either directly represented by a node or referenced by a node in
84 // the graph, and then create graph edges from provider to provider user
85 // so that the providers will get initialized first.
87 var diags tfdiags.Diagnostics
89 // To start, we'll collect the _requested_ provider addresses for each
90 // node, which we'll then resolve (handling provider inheritence, etc) in
92 // Our "requested" map is from graph vertices to string representations of
93 // provider config addresses (for deduping) to requests.
94 type ProviderRequest struct {
95 Addr addrs.AbsProviderConfig
96 Exact bool // If true, inheritence from parent modules is not attempted
98 requested := map[dag.Vertex]map[string]ProviderRequest{}
99 needConfigured := map[string]addrs.AbsProviderConfig{}
100 for _, v := range g.Vertices() {
102 // Does the vertex _directly_ use a provider?
103 if pv, ok := v.(GraphNodeProviderConsumer); ok {
104 requested[v] = make(map[string]ProviderRequest)
106 p, exact := pv.ProvidedBy()
108 log.Printf("[TRACE] ProviderTransformer: %s is provided by %s exactly", dag.VertexName(v), p)
110 log.Printf("[TRACE] ProviderTransformer: %s is provided by %s or inherited equivalent", dag.VertexName(v), p)
113 requested[v][p.String()] = ProviderRequest{
118 // Direct references need the provider configured as well as initialized
119 needConfigured[p.String()] = p
123 // Now we'll go through all the requested addresses we just collected and
124 // figure out which _actual_ config address each belongs to, after resolving
125 // for provider inheritance and passing.
126 m := providerVertexMap(g)
127 for v, reqs := range requested {
128 for key, req := range reqs {
132 _, ok := v.(GraphNodeSubPath)
133 if !ok && target == nil {
134 // No target and no path to traverse up from
135 diags = diags.Append(fmt.Errorf("%s: provider %s couldn't be found", dag.VertexName(v), p))
140 log.Printf("[TRACE] ProviderTransformer: exact match for %s serving %s", p, dag.VertexName(v))
143 // if we don't have a provider at this level, walk up the path looking for one,
144 // unless we were told to be exact.
145 if target == nil && !req.Exact {
146 for pp, ok := p.Inherited(); ok; pp, ok = pp.Inherited() {
150 log.Printf("[TRACE] ProviderTransformer: %s uses inherited configuration %s", dag.VertexName(v), pp)
153 log.Printf("[TRACE] ProviderTransformer: looking for %s to serve %s", pp, dag.VertexName(v))
157 // If this provider doesn't need to be configured then we can just
158 // stub it out with an init-only provider node, which will just
159 // start up the provider and fetch its schema.
160 if _, exists := needConfigured[key]; target == nil && !exists {
161 stubAddr := p.ProviderConfig.Absolute(addrs.RootModuleInstance)
162 stub := &NodeEvalableProvider{
163 &NodeAbstractProvider{
167 m[stubAddr.String()] = stub
168 log.Printf("[TRACE] ProviderTransformer: creating init-only node for %s", stubAddr)
174 diags = diags.Append(tfdiags.Sourceless(
176 "Provider configuration not present",
178 "To work with %s its original provider configuration at %s is required, but it has been removed. This occurs when a provider configuration is removed while objects created by that provider still exist in the state. Re-add the provider configuration to destroy %s, after which you can remove the provider configuration again.",
179 dag.VertexName(v), p, dag.VertexName(v),
185 // see if this in an inherited provider
186 if p, ok := target.(*graphNodeProxyProvider); ok {
189 key = target.(GraphNodeProvider).ProviderAddr().String()
192 log.Printf("[DEBUG] ProviderTransformer: %q (%T) needs %s", dag.VertexName(v), v, dag.VertexName(target))
193 if pv, ok := v.(GraphNodeProviderConsumer); ok {
194 pv.SetProvider(target.ProviderAddr())
196 g.Connect(dag.BasicEdge(v, target))
203 // CloseProviderTransformer is a GraphTransformer that adds nodes to the
204 // graph that will close open provider connections that aren't needed anymore.
205 // A provider connection is not needed anymore once all depended resources
206 // in the graph are evaluated.
207 type CloseProviderTransformer struct{}
209 func (t *CloseProviderTransformer) Transform(g *Graph) error {
210 pm := providerVertexMap(g)
211 cpm := make(map[string]*graphNodeCloseProvider)
214 for _, v := range pm {
215 p := v.(GraphNodeProvider)
216 key := p.ProviderAddr().String()
218 // get the close provider of this type if we alread created it
222 // create a closer for this provider type
223 closer = &graphNodeCloseProvider{Addr: p.ProviderAddr()}
228 // Close node depends on the provider itself
229 // this is added unconditionally, so it will connect to all instances
230 // of the provider. Extra edges will be removed by transitive
232 g.Connect(dag.BasicEdge(closer, p))
234 // connect all the provider's resources to the close node
235 for _, s := range g.UpEdges(p).List() {
236 if _, ok := s.(GraphNodeProviderConsumer); ok {
237 g.Connect(dag.BasicEdge(closer, s))
245 // MissingProviderTransformer is a GraphTransformer that adds to the graph
246 // a node for each default provider configuration that is referenced by another
247 // node but not already present in the graph.
249 // These "default" nodes are always added to the root module, regardless of
250 // where they are requested. This is important because our inheritance
251 // resolution behavior in ProviderTransformer will then treat these as a
252 // last-ditch fallback after walking up the tree, rather than preferring them
253 // as it would if they were placed in the same module as the requester.
255 // This transformer may create extra nodes that are not needed in practice,
256 // due to overriding provider configurations in child modules.
257 // PruneProviderTransformer can then remove these once ProviderTransformer
258 // has resolved all of the inheritence, etc.
259 type MissingProviderTransformer struct {
260 // Providers is the list of providers we support.
263 // Concrete, if set, overrides how the providers are made.
264 Concrete ConcreteProviderNodeFunc
267 func (t *MissingProviderTransformer) Transform(g *Graph) error {
268 // Initialize factory
269 if t.Concrete == nil {
270 t.Concrete = func(a *NodeAbstractProvider) dag.Vertex {
276 m := providerVertexMap(g)
277 for _, v := range g.Vertices() {
278 pv, ok := v.(GraphNodeProviderConsumer)
283 // For our work here we actually care only about the provider type and
284 // we plan to place all default providers in the root module, and so
285 // it's safe for us to rely on ProvidedBy here rather than waiting for
286 // the later proper resolution of provider inheritance done by
287 // ProviderTransformer.
288 p, _ := pv.ProvidedBy()
289 if p.ProviderConfig.Alias != "" {
290 // We do not create default aliased configurations.
291 log.Println("[TRACE] MissingProviderTransformer: skipping implication of aliased config", p)
295 // We're going to create an implicit _default_ configuration for the
296 // referenced provider type in the _root_ module, ignoring all other
297 // aspects of the resource's declared provider address.
298 defaultAddr := addrs.RootModuleInstance.ProviderConfigDefault(p.ProviderConfig.Type)
299 key := defaultAddr.String()
303 // There's already an explicit default configuration for this
304 // provider type in the root module, so we have nothing to do.
308 log.Printf("[DEBUG] adding implicit provider configuration %s, implied first by %s", defaultAddr, dag.VertexName(v))
310 // create the missing top-level provider
311 provider = t.Concrete(&NodeAbstractProvider{
313 }).(GraphNodeProvider)
322 // ParentProviderTransformer connects provider nodes to their parents.
324 // This works by finding nodes that are both GraphNodeProviders and
325 // GraphNodeSubPath. It then connects the providers to their parent
326 // path. The parent provider is always at the root level.
327 type ParentProviderTransformer struct{}
329 func (t *ParentProviderTransformer) Transform(g *Graph) error {
330 pm := providerVertexMap(g)
331 for _, v := range g.Vertices() {
332 // Only care about providers
333 pn, ok := v.(GraphNodeProvider)
338 // Also require non-empty path, since otherwise we're in the root
339 // module and so cannot have a parent.
340 if len(pn.Path()) <= 1 {
344 // this provider may be disabled, but we can only get it's name from
345 // the ProviderName string
346 addr := pn.ProviderAddr()
347 parentAddr, ok := addr.Inherited()
349 parent := pm[parentAddr.String()]
351 g.Connect(dag.BasicEdge(v, parent))
358 // PruneProviderTransformer removes any providers that are not actually used by
359 // anything, and provider proxies. This avoids the provider being initialized
360 // and configured. This both saves resources but also avoids errors since
361 // configuration may imply initialization which may require auth.
362 type PruneProviderTransformer struct{}
364 func (t *PruneProviderTransformer) Transform(g *Graph) error {
365 for _, v := range g.Vertices() {
366 // We only care about providers
367 _, ok := v.(GraphNodeProvider)
372 // ProxyProviders will have up edges, but we're now done with them in the graph
373 if _, ok := v.(*graphNodeProxyProvider); ok {
374 log.Printf("[DEBUG] pruning proxy %s", dag.VertexName(v))
378 // Remove providers with no dependencies.
379 if g.UpEdges(v).Len() == 0 {
380 log.Printf("[DEBUG] pruning unused %s", dag.VertexName(v))
388 func providerVertexMap(g *Graph) map[string]GraphNodeProvider {
389 m := make(map[string]GraphNodeProvider)
390 for _, v := range g.Vertices() {
391 if pv, ok := v.(GraphNodeProvider); ok {
392 addr := pv.ProviderAddr()
393 m[addr.String()] = pv
400 func closeProviderVertexMap(g *Graph) map[string]GraphNodeCloseProvider {
401 m := make(map[string]GraphNodeCloseProvider)
402 for _, v := range g.Vertices() {
403 if pv, ok := v.(GraphNodeCloseProvider); ok {
404 addr := pv.CloseProviderAddr()
405 m[addr.String()] = pv
412 type graphNodeCloseProvider struct {
413 Addr addrs.AbsProviderConfig
417 _ GraphNodeCloseProvider = (*graphNodeCloseProvider)(nil)
420 func (n *graphNodeCloseProvider) Name() string {
421 return n.Addr.String() + " (close)"
424 // GraphNodeSubPath impl.
425 func (n *graphNodeCloseProvider) Path() addrs.ModuleInstance {
429 // GraphNodeEvalable impl.
430 func (n *graphNodeCloseProvider) EvalTree() EvalNode {
431 return CloseProviderEvalTree(n.Addr)
434 // GraphNodeDependable impl.
435 func (n *graphNodeCloseProvider) DependableName() []string {
436 return []string{n.Name()}
439 func (n *graphNodeCloseProvider) CloseProviderAddr() addrs.AbsProviderConfig {
443 // GraphNodeDotter impl.
444 func (n *graphNodeCloseProvider) DotNode(name string, opts *dag.DotOpts) *dag.DotNode {
450 Attrs: map[string]string{
457 // RemovableIfNotTargeted
458 func (n *graphNodeCloseProvider) RemoveIfNotTargeted() bool {
459 // We need to add this so that this node will be removed if
460 // it isn't targeted or a dependency of a target.
464 // graphNodeProxyProvider is a GraphNodeProvider implementation that is used to
465 // store the name and value of a provider node for inheritance between modules.
466 // These nodes are only used to store the data while loading the provider
467 // configurations, and are removed after all the resources have been connected
468 // to their providers.
469 type graphNodeProxyProvider struct {
470 addr addrs.AbsProviderConfig
471 target GraphNodeProvider
475 _ GraphNodeProvider = (*graphNodeProxyProvider)(nil)
478 func (n *graphNodeProxyProvider) ProviderAddr() addrs.AbsProviderConfig {
482 func (n *graphNodeProxyProvider) Path() addrs.ModuleInstance {
486 func (n *graphNodeProxyProvider) Name() string {
487 return n.addr.String() + " (proxy)"
490 // find the concrete provider instance
491 func (n *graphNodeProxyProvider) Target() GraphNodeProvider {
492 switch t := n.target.(type) {
493 case *graphNodeProxyProvider:
500 // ProviderConfigTransformer adds all provider nodes from the configuration and
501 // attaches the configs.
502 type ProviderConfigTransformer struct {
504 Concrete ConcreteProviderNodeFunc
506 // each provider node is stored here so that the proxy nodes can look up
507 // their targets by name.
508 providers map[string]GraphNodeProvider
509 // record providers that can be overriden with a proxy
510 proxiable map[string]bool
512 // Config is the root node of the configuration tree to add providers from.
513 Config *configs.Config
516 func (t *ProviderConfigTransformer) Transform(g *Graph) error {
517 // If no configuration is given, we don't do anything
522 t.providers = make(map[string]GraphNodeProvider)
523 t.proxiable = make(map[string]bool)
525 // Start the transformation process
526 if err := t.transform(g, t.Config); err != nil {
530 // finally attach the configs to the new nodes
531 return t.attachProviderConfigs(g)
534 func (t *ProviderConfigTransformer) transform(g *Graph, c *configs.Config) error {
535 // If no config, do nothing
541 if err := t.transformSingle(g, c); err != nil {
545 // Transform all the children.
546 for _, cc := range c.Children {
547 if err := t.transform(g, cc); err != nil {
554 func (t *ProviderConfigTransformer) transformSingle(g *Graph, c *configs.Config) error {
555 // Get the module associated with this configuration tree node
559 // We actually need a dynamic module path here, but we've not yet updated
560 // our graph builders enough to support expansion of module calls with
561 // "count" and "for_each" set, so for now we'll shim this by converting to
562 // a dynamic path with no keys. At the time of writing this is the only
563 // possible kind of dynamic path anyway.
564 path := make(addrs.ModuleInstance, len(staticPath))
565 for i, name := range staticPath {
566 path[i] = addrs.ModuleInstanceStep{
571 // add all providers from the configuration
572 for _, p := range mod.ProviderConfigs {
574 addr := relAddr.Absolute(path)
576 abstract := &NodeAbstractProvider{
580 if t.Concrete != nil {
581 v = t.Concrete(abstract)
586 // Add it to the graph
589 t.providers[key] = v.(GraphNodeProvider)
591 // A provider configuration is "proxyable" if its configuration is
592 // entirely empty. This means it's standing in for a provider
593 // configuration that must be passed in from the parent module.
594 // We decide this by evaluating the config with an empty schema;
595 // if this succeeds, then we know there's nothing in the body.
596 _, diags := p.Config.Content(&hcl.BodySchema{})
597 t.proxiable[key] = !diags.HasErrors()
600 // Now replace the provider nodes with proxy nodes if a provider was being
601 // passed in, and create implicit proxies if there was no config. Any extra
602 // proxies will be removed in the prune step.
603 return t.addProxyProviders(g, c)
606 func (t *ProviderConfigTransformer) addProxyProviders(g *Graph, c *configs.Config) error {
609 // can't add proxies at the root
614 parentPath, callAddr := path.Call()
620 callName := callAddr.Name
621 var parentCfg *configs.ModuleCall
622 for name, mod := range parent.Module.ModuleCalls {
623 if name == callName {
629 // We currently don't support count/for_each for modules and so we must
630 // shim our path and parentPath into module instances here so that the
631 // rest of Terraform can behave as if we do. This shimming should be
632 // removed later as part of implementing count/for_each for modules.
633 instPath := make(addrs.ModuleInstance, len(path))
634 for i, name := range path {
635 instPath[i] = addrs.ModuleInstanceStep{Name: name}
637 parentInstPath := make(addrs.ModuleInstance, len(parentPath))
638 for i, name := range parentPath {
639 parentInstPath[i] = addrs.ModuleInstanceStep{Name: name}
642 if parentCfg == nil {
643 // this can't really happen during normal execution.
644 return fmt.Errorf("parent module config not found for %s", c.Path.String())
647 // Go through all the providers the parent is passing in, and add proxies to
648 // the parent provider nodes.
649 for _, pair := range parentCfg.Providers {
650 fullAddr := pair.InChild.Addr().Absolute(instPath)
651 fullParentAddr := pair.InParent.Addr().Absolute(parentInstPath)
652 fullName := fullAddr.String()
653 fullParentName := fullParentAddr.String()
655 parentProvider := t.providers[fullParentName]
657 if parentProvider == nil {
658 return fmt.Errorf("missing provider %s", fullParentName)
661 proxy := &graphNodeProxyProvider{
663 target: parentProvider,
666 concreteProvider := t.providers[fullName]
668 // replace the concrete node with the provider passed in
669 if concreteProvider != nil && t.proxiable[fullName] {
670 g.Replace(concreteProvider, proxy)
671 t.providers[fullName] = proxy
675 // aliased configurations can't be implicitly passed in
676 if fullAddr.ProviderConfig.Alias != "" {
680 // There was no concrete provider, so add this as an implicit provider.
681 // The extra proxy will be pruned later if it's unused.
683 t.providers[fullName] = proxy
688 func (t *ProviderConfigTransformer) attachProviderConfigs(g *Graph) error {
689 for _, v := range g.Vertices() {
690 // Only care about GraphNodeAttachProvider implementations
691 apn, ok := v.(GraphNodeAttachProvider)
696 // Determine what we're looking for
697 addr := apn.ProviderAddr()
699 // Get the configuration.
700 mc := t.Config.DescendentForInstance(addr.Module)
702 log.Printf("[TRACE] ProviderConfigTransformer: no configuration available for %s", addr.String())
706 // Go through the provider configs to find the matching config
707 for _, p := range mc.Module.ProviderConfigs {
708 if p.Name == addr.ProviderConfig.Type && p.Alias == addr.ProviderConfig.Alias {
709 log.Printf("[TRACE] ProviderConfigTransformer: attaching to %q provider configuration from %s", dag.VertexName(v), p.DeclRange)
710 apn.AttachProvider(p)