6 "github.com/hashicorp/terraform/config"
7 "github.com/hashicorp/terraform/config/module"
10 // GraphNodeAttachProvider is an interface that must be implemented by nodes
11 // that want provider configurations attached.
12 type GraphNodeAttachProvider interface {
13 // Must be implemented to determine the path for the configuration
16 // ProviderName with no module prefix. Example: "aws".
19 // Sets the configuration
20 AttachProvider(*config.ProviderConfig)
23 // AttachProviderConfigTransformer goes through the graph and attaches
24 // provider configuration structures to nodes that implement the interfaces
27 // The attached configuration structures are directly from the configuration.
28 // If they're going to be modified, a copy should be made.
29 type AttachProviderConfigTransformer struct {
30 Module *module.Tree // Module is the root module for the config
33 func (t *AttachProviderConfigTransformer) Transform(g *Graph) error {
34 if err := t.attachProviders(g); err != nil {
41 func (t *AttachProviderConfigTransformer) attachProviders(g *Graph) error {
42 // Go through and find GraphNodeAttachProvider
43 for _, v := range g.Vertices() {
44 // Only care about GraphNodeAttachProvider implementations
45 apn, ok := v.(GraphNodeAttachProvider)
50 // Determine what we're looking for
51 path := normalizeModulePath(apn.Path())
53 name := apn.ProviderName()
54 log.Printf("[TRACE] Attach provider request: %#v %s", path, name)
56 // Get the configuration.
57 tree := t.Module.Child(path)
62 // Go through the provider configs to find the matching config
63 for _, p := range tree.Config().ProviderConfigs {
64 // Build the name, which is "name.alias" if an alias exists
67 current += "." + p.Alias
70 // If the configs match then attach!
72 log.Printf("[TRACE] Attaching provider config: %#v", p)