add providers directly from the configuration

The first step in only using the required provider nodes in a graph is
to be able to specifically add them from the configuration.

The MissingProviderTransformer was previously responsible for adding
all providers. Now it is really just adding any that are missing from
the config.
This commit is contained in:
James Bardin 2017-10-31 13:13:24 -04:00
parent da61627b9c
commit 0986d01223
4 changed files with 105 additions and 14 deletions

View File

@ -87,6 +87,13 @@ func (b *ApplyGraphBuilder) Steps() []GraphTransformer {
// Attach the state
&AttachStateTransformer{State: b.State},
// add configured providers
&ProviderConfigTransformer{
Module: b.Module,
Providers: b.Providers,
Concrete: concreteProvider,
},
// Create all the providers
&MissingProviderTransformer{Providers: b.Providers, Concrete: concreteProvider},
&ProviderTransformer{},

View File

@ -93,6 +93,13 @@ func (b *PlanGraphBuilder) Steps() []GraphTransformer {
// Add root variables
&RootVariableTransformer{Module: b.Module},
// add configured providers
&ProviderConfigTransformer{
Module: b.Module,
Providers: b.Providers,
Concrete: b.ConcreteProvider,
},
// Create all the providers
&MissingProviderTransformer{Providers: b.Providers, Concrete: b.ConcreteProvider},
&ProviderTransformer{},

View File

@ -133,3 +133,78 @@ func (t *ConfigTransformer) transformSingle(g *Graph, m *module.Tree) error {
return nil
}
type ProviderConfigTransformer struct {
Providers []string
Concrete ConcreteProviderNodeFunc
// Module is the module to add resources from.
Module *module.Tree
}
func (t *ProviderConfigTransformer) Transform(g *Graph) error {
// If no module is given, we don't do anything
if t.Module == nil {
return nil
}
// If the module isn't loaded, that is simply an error
if !t.Module.Loaded() {
return errors.New("module must be loaded for ProviderConfigTransformer")
}
// Start the transformation process
return t.transform(g, t.Module)
}
func (t *ProviderConfigTransformer) transform(g *Graph, m *module.Tree) error {
// If no config, do nothing
if m == nil {
return nil
}
// Add our resources
if err := t.transformSingle(g, m); err != nil {
return err
}
// Transform all the children.
for _, c := range m.Children() {
if err := t.transform(g, c); err != nil {
return err
}
}
return nil
}
func (t *ProviderConfigTransformer) transformSingle(g *Graph, m *module.Tree) error {
log.Printf("[TRACE] ProviderConfigTransformer: Starting for path: %v", m.Path())
// Get the configuration for this module
conf := m.Config()
// Build the path we're at
path := m.Path()
if len(path) > 0 {
path = append([]string{RootModuleName}, path...)
}
// Write all the resources out
for _, p := range conf.ProviderConfigs {
name := p.Name
if p.Alias != "" {
name += "." + p.Alias
}
v := t.Concrete(&NodeAbstractProvider{
NameValue: name,
PathValue: path,
}).(dag.Vertex)
// Add it to the graph
g.Add(v)
}
return nil
}

View File

@ -104,10 +104,10 @@ func (t *CloseProviderTransformer) Transform(g *Graph) error {
return err
}
// MissingProviderTransformer is a GraphTransformer that adds nodes
// for missing providers into the graph. Specifically, it creates provider
// configuration nodes for all the providers that we support. These are
// pruned later during an optimization pass.
// MissingProviderTransformer is a GraphTransformer that adds nodes for all
// required providers into the graph. Specifically, it creates provider
// configuration nodes for all the providers that we support. These are pruned
// later during an optimization pass.
type MissingProviderTransformer struct {
// Providers is the list of providers we support.
Providers []string
@ -160,6 +160,18 @@ func (t *MissingProviderTransformer) Transform(g *Graph) error {
}
for _, p := range pv.ProvidedBy() {
// always add the parent nodes to check, since configured providers
// may have already been added for modules.
if len(path) > 0 {
// We'll need the parent provider as well, so let's
// add a dummy node to check to make sure that we add
// that parent provider.
check = append(check, &graphNodeProviderConsumerDummy{
ProviderValue: p,
PathValue: path[:len(path)-1],
})
}
key := providerMapKey(p, pv)
if _, ok := m[key]; ok {
// This provider already exists as a configure node
@ -185,16 +197,6 @@ func (t *MissingProviderTransformer) Transform(g *Graph) error {
NameValue: p,
PathValue: path,
}).(dag.Vertex)
if len(path) > 0 {
// We'll need the parent provider as well, so let's
// add a dummy node to check to make sure that we add
// that parent provider.
check = append(check, &graphNodeProviderConsumerDummy{
ProviderValue: p,
PathValue: path[:len(path)-1],
})
}
m[key] = g.Add(v)
}
}